kawanet / msgpack-lite

Fast Pure JavaScript MessagePack Encoder and Decoder / msgpack.org[JavaScript]
https://www.npmjs.com/package/msgpack-lite
MIT License
983 stars 127 forks source link

Support toJSON? #79

Open endel opened 6 years ago

endel commented 6 years ago

I'm wondering if it's feasible to implement toJSON support on msgpack-lite?

JSON.stringify() supports handling custom toJSON methods, like:

function MyData () {}
MyData.prototype.toJSON = function() {
  return { custom: new Custom(), output: 2 };
}
function Custom() {}
Custom.prototype.toJSON = function() { return "recursive"; }

JSON.stringify(new MyData())
// => "{"custom":"recursive","output":2}"

Would be great to have the ability to customize the serialization for msgpack too.

Cheers!

kawanet commented 6 years ago

Some of Objects already support toJSON() serializing method. It sounds a good idea to divert it for us instead of building our own toMsgpack() method. Thanks.