Call toJSON() on non-ext objects when it exists#188
Conversation
Codecov Report
@@ Coverage Diff @@
## main #188 +/- ##
=======================================
Coverage 98.13% 98.14%
=======================================
Files 16 16
Lines 964 968 +4
Branches 206 208 +2
=======================================
+ Hits 946 950 +4
Misses 18 18
Continue to review full report at Codecov.
|
|
Thank you for your effort, but I'm not sure it's useful for everyone, because |
|
I guess my argument would be that JavaScript has a native way for objects to define how they should be serialized. It’s hard for me to wrap my head around why you don’t think it’s reasonable to use that in situations where a custom extension hasn’t been defined. |
|
Just want to include a few links here of popular libraries that implement The important thing to bear in mind is that the receiver of some https://github.com/Automattic/mongoose/blob/master/lib/document.js#L3794 |
|
Hmm, still I'm not sure I rather think that I can add another option to make warnings about objects which seems not to be serializable. That is, their prototypes are not |
|
I still think you’re not understanding what Moment does not include that for “formatting” purposes. It has a
I don’t understand what you see as the risk to merging this. You don’t lose any functionality, and you make it easier for users to serialize arbitrary non-POJOs - because, again, |

Currently,
@msgpack/msgpackdoes not automatically call.toJSON()on objects which define that method.Since JSON and msgpack have significant overlap in semantics and intent, and toJSON() is a signal from an object that says "this is how I want to be serialized", I believe it makes sense to use
toJSON()when it exists (after exhausting possible extensions).The concrete use-case for me was
mongodb.ObjectId– which returns a simple string fromtoJSON(), but otherwise looks like:Effectively, this change helps msgpack more closely match the behavior of
JSON.stringify().It wasn't entirely clear to me where these tests should go. There's not much in the suite for testing the encode side. Most of the tests seem to sort of implicitly test encoding while calling themselves decode tests, so I've stuck with that convention here.