glenjamin / transit-immutable-js

Transit serialisation for Immutable.js
MIT License
250 stars 31 forks source link

Serializing es6 classes by calling toJSON #37

Open Parez opened 7 years ago

Parez commented 7 years ago

I wish I could put my redux state as is to the toJSON method without worrying that all data structures inside have to belong to immutable.js. Maybe I'm missing something, but at the moment I'm getting an error when I try to serialize Map<string, Item>, where Item is a basic class with a constructor. The thing is that JSON.stringify does the job of serializing es6 classes just fine by only including it's properties and ignoring methods. I would expect that the library would handle non-immutable objects the same way as JSON.stringify does - then it would be a nice neat extension for normal serialization.

glenjamin commented 7 years ago

If you JSON stringify an ES6 Map and then try and parse it back, what do you end up with?

The transit library that powers this one expect every class to be registered so it can encode and decode without loss of detail.

You can use https://github.com/glenjamin/transit-immutable-js#transitwithextrahandlersarray-handlers--transit to include additional handlers, supporting ES6 Maps out of the box would require a change to transit-js itself.