Skip to content

Instantly share code, notes, and snippets.

@chenglou
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenglou/3727637cba297e839642 to your computer and use it in GitHub Desktop.
Save chenglou/3727637cba297e839642 to your computer and use it in GitHub Desktop.
Serialization order problem
'use strict';
var transit = require('transit-js');
var I = require('immutable');
var wCache = I.Map();
var wid = 0;
var listHandler = transit.makeWriteHandler({
tag: function(v) {
if (wCache.get(v) != null) {
return 'cacheL';
}
return 'List';
},
rep: function(v) {
var id = wCache.get(v);
if (id != null) {
return id;
}
wCache = wCache.set(v, wid++);
// shallow. Let transit convert each cell recursively
return v.toArray();
},
stringRep: function() {return null;},
});
var writer = transit.writer('json', {
handlers: transit.map([
I.List, listHandler,
])
});
var rCache = I.Map();
var rid = 0;
var reader = transit.reader('json', {
handlers: {
List: function(v) {
console.log('----------', rid, v);
var ret = I.List(v);
rCache = rCache.set(rid++, ret);
return ret;
},
cacheL: function(v) {
console.log('cache', v);
return rCache.get(v);
},
}
});
var obj = I.List([
I.List([1]),
I.List([1]),
]);
var res = writer.write(obj);
var res2 = reader.read(res);
console.log('r', res);
console.log();
console.log('o', obj);
console.log('w', res2);
// console.log(obj.get(1));
// console.log(res2.get(1));
// console.log(obj.equals(res2));
console.log();
console.log(wCache);
console.log(rCache);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment