Transit serialisation for Immutable.js
npm install transit-immutable-js
You must also be using immutable
for this to be any use.
I have chosen to apply very broad npm peerDependencies for simplicity, please check that the versions you have pulled in actually work.
var transit = require('transit-immutable-js');
var Immutable = require('immutable');
var m = Immutable.Map({with: "Some", data: "In"});
var str = transit.toJSON(m);
console.log(str)
// ["~#cmap",["with","Some","data","In"]]
var m2 = transit.fromJSON(str);
console.log(Immutable.is(m, m2));
// true
This library also manages to preserve objects which are a mixture of plain javascript and Immutable.
var obj = {
iMap: Immutable.Map().set(Immutable.List.of(1, 2, 3), "123"),
iList: Immutable.List.of("a", "b", "c"),
array: [ "javascript", 4, "lyfe" ]
}
console.log(transit.fromJSON(transit.toJSON(obj)));
// { iMap: Map { [1,2,3]: "123" },
// iList: List [ "a", "b", "c" ],
// array: [ 'javascript', 4, 'lyfe' ] }
Convert an immutable object into a JSON representation
Convert a JSON representation back into an immutable object
Create a modified version of the transit API that deeply applies the provided filter function to all immutable collections before serialising. Can be used to exclude entries.