I a few years back I was playing around with blitzjs and stumbled upon a package that was part of their ecosystem: SuperJSON. It's a library that takes JSON.stringify
& JSON.parse
to the next level. It let's you put in data like Date/Map/Set
and properly serialize it unlike the native JSON methods.
I largely forgot about it until a few months ago when I started working on a bills tracking app in my free time (I know, I know). I ran into an issue with react-navigation
where I wanted to pass some data to a screen via params but couldn't easily because that data was a Date
.
My old workaround was to simply send it as a ISO string and then parse back on the other side. Totally works but ends up with a lot of extra boilerplate code every time it's required. With SuperJSON
, I can send it as an actual date and just call serialize
and deserialize
on either end and get exactly what I want.
Only problem is that typescript gets to be a bit of a pain and honestly I wish I could hook into react-navigation
and override their default serialization implementation with one run by SuperJSON. I've dug some into their codebase but it's a labyrinth and I haven't had much luck.
So for now, I'll just keep adding it in user land myself, but if anyone knows of a good way or could put up a PR that would be amazing!