New well-known Symbol: Symbol.toJSON

Hey ECMA community,

I'm working on a library Boa that creates Proxy objects for binding to Python's object, but it fails on calling JSON.stringify with these proxy objects because it's circular as https://github.com/alibaba/pipcook/issues/146.

And I just found that JSON.stringify will call the obj.toJSON() internally, so i'm wondering if a new well-known Symbol(.toJSON) instead of the current string keyed, just like what we have achieved on toPrimitive and toStringTag?

If that makes sense, I'd love to create a proposal repository for this :)

1 Like

What would be the benefit of using a Symbol method and a string method? In both of the cases you mentioned, there was no hook before, and when one was created, Symbols were available.

SerializeJSONProperty will call method toJSON() before replacer function. So I think the string toJSON() now is a hook for JSON.stringify, right?

However in fact, there is no too much improvements in this proposal, it's to keep more consistent with others and I think Symbol might be more proper here :)

2 Likes