js-cid icon indicating copy to clipboard operation
js-cid copied to clipboard

What is the purpose of `toJSON()`?

Open mikeal opened this issue 7 years ago • 1 comments

I'm a little confused by the toJSON() method.

First of all, we have doc comments for a typedef that doesn't actually exist called "SerializedCID". The docs claim toJSON() returns this but that isn't actually true.

In our toJSON() method we return 3 properties, one of which is a Buffer and, as a result, won't be property decodable from the JSON representation.

As I started to think about what toJSON() should do I realized that the most useful thing may be returning {"/": this.baseEncodedString()}. This touches again on the discussion we're having about a canonical JSON form for links but it does seem appropriate.

mikeal avatar Sep 13 '18 18:09 mikeal

I agree. toJSON() should return something that is properly serializable with JSON.stringify(). I buffer is not.

vmx avatar Sep 14 '18 09:09 vmx