redux-persist
redux-persist copied to clipboard
Avoiding double encoded JSON
Question: How would we avoid double encoding the stored JSON?
Example file contents:
{"loading":"false","loaded":"\"2020-09-09T18:22:24.623Z\"" ....
What I expect to be stored:
{"loading":false,"loaded":"2020-09-09T18:22:24.623Z" ....
Notice how both values are strings, because serialize
(JSON.stringify()
) is called on it. After reading the source, it seems clear why it's happening: redux-persist iterates over modified keys and always runs serialize on changed keys.
My question is: How do we disable this behavior, so we only encode once / is it even possible with the current way it's designed?
storage: {
...storage,
setItem: (key: string, item: string): Promise<void> => {
return new Promise((resolve): void => {
resolve(storage.setItem(key, JSON.stringify(item)));
});
},
getItem: (key: string): Promise<string | null> => {
return new Promise((resolve): void => {
resolve(storage.getItem(key).then((value) => value ? JSON.parse(value) : null));
});
},
},
serialize: false,
// @ts-ignore
deserialize: false,