multiformats / js-cid

CID implementation in JavaScript
MIT License
97 stars 39 forks source link

What is the purpose of `toJSON()`? #57

Open mikeal opened 5 years ago

mikeal commented 5 years ago

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.

vmx commented 5 years ago

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