Skip to main content
Version: Current

dateReviver

@rotorsoft/act-root


@rotorsoft/act-root / act/src / dateReviver

Function: dateReviver()

dateReviver(_key, value): unknown

Defined in: libs/act/src/utils.ts:159

JSON.parse reviver that turns ISO-8601 strings back into Dates.

JSON has no date type, so a Date committed into an event's data, meta, or pii serializes to a string. Reading it back as a string would silently break reducers that call .getTime() โ€” and would do so on some adapters and not others. Every adapter therefore revives dates on read, and this is the single definition they share, so the behavior can't drift between them (#1198).

Adapters must apply it to every JSON column they read, including decrypted pii payloads โ€” a value's runtime type must not depend on which column it sits in or whether encryption is enabled (#1365, #1370). Third-party adapters need it to pass the store TCK's Date round-trip cases.

The trade-off is deliberate: a string that happens to look like a timestamp is revived into a Date. That is the price of dates surviving a JSON round trip at all, and it is applied uniformly.

Parametersโ€‹

_keyโ€‹

string

Unused; present to satisfy the JSON.parse reviver shape.

valueโ€‹

unknown

The parsed value.

Returnsโ€‹

unknown

A Date when value is an ISO-8601 string, otherwise value.

Exampleโ€‹

JSON.parse('{"at":"2024-03-01T10:20:30.000Z"}', dateReviver);
// โ†’ { at: Date }