Firefox Tomorrow

web api instance method

PressureRecord: toJSON() method

View on MDN ↗

Limited availabilityAvailable in workersSecure context

The toJSON() method is a serializer; it returns a JSON representation of the PressureRecord object.

Syntax

toJSON()

Parameters

None.

Return value

A JSON object that is the serialization of the PressureRecord object.

Examples

Using the toJSON method

In this example, calling lastRecord.toJSON() returns a JSON representation of the PressureRecord object.

function callback(records) {
  const lastRecord = records[records.length - 1];
  console.log(lastRecord.toJSON);
}

try {
  const observer = new PressureObserver(callback);
  await observer.observe("cpu", {
    sampleInterval: 1000, // 1000ms
  });
} catch (error) {
  // report error setting up the observer
}

This would log a JSON object like so:

{
  "source": "cpu",
  "state": "fair",
  "time": 1712052746385.347
}

To get a JSON string, you can use JSON.stringify(lastRecord) directly; it will call toJSON() automatically.

Specifications

SpecificationsStandards references are available on the canonical MDN page.

Browser compatibility

Browser compatibilityCompatibility data is available on the canonical MDN page.

See also