16Jan/112
HydrateJS: Smarter Javascript Serialization
Finally, here it is: HydrateJS.
It's a library that helps you serialize proper Javascript objects, more than just hash-like objects like JSON.stringify can handle. Anyway, I put a lot of time into the documentation inside the library as well as on the Github Pages link, so check those out! There is also a full suite of specs.
For some of the technical challenges, see my previous post.
Let me know what you think in the comments, and if you're using it, feature requests, etc.
March 2nd, 2011 - 10:54
I am trying to use HydrateJS to serialize an object with circular references and functions. I modified the source as follows for it to serialize functions:
1)
case “function”:
//throw new Error(“can’t serialize functions”);
//break;
return uneval(eval(input));
2)
case “function”:
//return this.errorHandler(new Hydrate.NonPrototypeFunctionError(input, name));
return uneval(eval(input));
It currently serializes my object just fine.
However when I try to deserialize it I get the following error:
No Hydrate resolver found — you should specify one in the Hydrate constructor!
I’m not sure if that’s caused by my changes or if I’m doing something wrong:
var serialized = Hydrate.prototype.stringify(object);
var deserialized = Hydrate.prototype.parse(serialized);
March 6th, 2011 - 20:42
You have to instantiate Hydrate — that’s how you configure it (specifying an error handler, migrations, and the resolver). The empty constructor is fine: new Hydrate(). Check out the specs for some examples: https://github.com/nanodeath/HydrateJS/blob/master/spec/HydrateSpec.js
FYI, the resolver is how strings representing the classes are mapped back into actual classes after deserialization.