Temporal.PlainYearMonth.prototype.toJSON()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimentell: Dies ist eine experimentelle Technologie
Überprüfen Sie die Browser-Kompatibilitätstabelle sorgfältig vor der Verwendung auf produktiven Webseiten.

Die Methode toJSON() von Temporal.PlainYearMonth-Instanzen gibt einen String zurück, der dieses Jahr-Monat im selben RFC 9557-Format darstellt, wie ihn der Aufruf von toString() erzeugt. Sie soll implizit durch JSON.stringify() aufgerufen werden.

Syntax

js
toJSON()

Parameter

Keine.

Rückgabewert

Ein String, der das angegebene Datum im RFC 9557-Format darstellt, einschließlich der Kalenderannotation, wenn diese nicht "iso8601" ist.

Beschreibung

Die Methode toJSON() wird automatisch von JSON.stringify() aufgerufen, wenn ein Temporal.PlainYearMonth-Objekt serialisiert wird. Diese Methode ist im Allgemeinen dafür vorgesehen, Temporal.PlainYearMonth-Objekte während der JSON-Serialisierung nützlich zu serialisieren, die dann mit der Funktion Temporal.PlainYearMonth.from() als Wiederbeleber von JSON.parse() deserialisiert werden können.

Beispiele

Verwendung von toJSON()

js
const ym = Temporal.PlainYearMonth.from({ year: 2021, month: 8 });
const ymStr = ym.toJSON(); // '2021-08'
const ym2 = Temporal.PlainYearMonth.from(ymStr);

JSON-Serialisierung und -Parsing

Dieses Beispiel zeigt, wie Temporal.PlainYearMonth ohne zusätzlichen Aufwand als JSON serialisiert und wie es wieder geparst werden kann.

js
const ym = Temporal.PlainYearMonth.from({ year: 2021, month: 8 });
const ymStr = JSON.stringify({ event: ym }); // '{"event":"2021-08"}'
const obj = JSON.parse(ymStr, (key, value) => {
  if (key === "event") {
    return Temporal.PlainYearMonth.from(value);
  }
  return value;
});

Spezifikationen

Specification
Temporal proposal
# sec-temporal.plainyearmonth.prototype.tojson

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
toJSON
Experimental

Legend

Tip: you can click/tap on a cell for more information.

No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.
User must explicitly enable this feature.

Siehe auch