redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

Avoiding double encoded JSON

Open scarlac opened this issue 4 years ago • 1 comments

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?

scarlac avatar Sep 09 '20 18:09 scarlac

  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,

BARTlab avatar Feb 20 '24 12:02 BARTlab