I’m trying to serialise a model object to local storage (or IndexedDB), however it contains signals.
// An example of the model I'm dealing with
export class ExampleModel {
canSerialise = "";
cantSerialise = signal("");
}
// What I'd like to be able to do
let exampleModel = new ExampleModel();
localStorage.setItem("example", JSON.stringify(exampleModel));
However this will not serialise cantSerialise
as signals are technically functions. I’d like to avoid manually writing serialisation functions for all models that just have a signal in them.
- Tried simply serialising using
JSON.stringify()
:- Desired outcome:
'{"canSerialise":"","cantSerialise":X}'
whereX
can be anything as long as it serialises back into the original value as a signal (in this casesignal("")
) - Results (as I feared/expected) in
'{"canSerialise":""}'
- Desired outcome:
- Thought about using IndexedDB but it won’t be able to serialise signals either since the structured cloning algorithms has the same limitation of nog serialising functions.
New contributor
Jarno is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.