Rxova
Skip to content

Serializer

How a value becomes text, for the two paths that cannot use structured clone.

BroadcastChannel carries structured clone, so a Date arrives a Date and a Map a Map. The storage-event transport and disk persistence carry text, and JSON is a strictly poorer format: a Date comes back a string, a Map comes back {}, an undefined property is simply gone. Same library, same call, different answer depending on which transport happened to be available — which is the kind of difference that is discovered in production.

The seam exists so the two can be made to agree. It is deliberately not a bundled dependency: devalue costs 3.4 kB brotlied and superjson 3.6 kB, against a whole-library budget of 7.3 kB. Charging every user 47% for a fidelity most of them do not need would be the wrong default. So the default is JSON — free, and now loud — and anything better is one line away.

import * as devalue from 'devalue';
localStorageAdapter('settings', {
serializer: { stringify: devalue.stringify, parse: devalue.parse },
});
parse(text): unknown;
ParameterType
textstring

unknown


stringify(value): string;
ParameterType
valueunknown

string