-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reusability of raw serialization mechanism #19
Comments
Using a symbol would impose a cost (an additional Add a new This also requires a cost (checking for presence of an internal slot) on every serialization, but the cost is much lower. I kind of like this approach. It (as well as the well-known symbol approach, by contrast to the per-invocation approaches) allows you to do stuff like class Rational {
// constructor etc elided
#num = 1n;
#denom = 1n;
toJSON() {
return { type: 'rational', num: JSON.rawString(String(this.#num)), denom: JSON.rawString(String(this.#denom)) };
}
} which, while it is a new capability, seems like it is OK? |
toJSON
provides a capability available to any object for controlling its serialization, and a well-known Symbol would extend that capability to include raw output (e.g.,JSON.stringify({toJSON(){ return {[Symbol.rawJSON]: longStringOfDigits}; }})
).Alternatively, the mechanism for raw output could be limited to each individual invocation of
JSON.stringify
or even to each individual invocation of a replacer function, either of which would have the potential benefit of coupling it to specifically-expressed author intent.The text was updated successfully, but these errors were encountered: