joshnuss / svelte-persisted-store

A Svelte store that persists to localStorage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Map Serialization to localStorageStore

MrVauxs opened this issue · comments

The current implementation fails to save Maps due to how JS stringifies it into JSON (Any Map becomes an empty object {}).

The following answer shows how one can do go around this issue: https://stackoverflow.com/a/28918362/12227966

While I can use a custom serializer, it would be nice to see the default serializer be able to support more types.

My own implementation:

const serializer = {
		parse: (data) => {
			return new Map(JSON.parse(data));
		},
		stringify: (data) => {
			return JSON.stringify([...data]);
		}
	}

What type of pull request would this be?

Enhancement

Hi @MrVauxs!

Have you tried passing devalue as the serializer?
That should handle Map.

Please re-open if that doesn't work.

Hi @MrVauxs!

Have you tried passing devalue as the serializer? That should handle Map.

Please re-open if that doesn't work.

I have tested devalue and it returns DevalueError: Cannot stringify arbitrary non-POJOs. I see it supports Maps, so I am guessing its something with my use-case...