-
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
bikeshedding the mechanism for serialization #18
Comments
Yes, I intend to bring up this precise possibility during the update. |
As mentioned during the plenary, I'm wondering if a well-known symbol is actually not acceptable to allow usage in
|
There was a preference for use of a function in the TC39 plenary. |
@mhofman See #19 for reusability. As for the mechanism, if we do choose a function, I see at least two possibilities:
There seemed to be a slight preference for option 1. |
@michaelficarra expressed that preference, but I prefer option 2, and I think I convinced him of my position in matrix. |
+1 from me for 2 though I remain unconvinced by the per- |
I raised this point in matrix, but want to ensure its captured here as well: If we have a mechanism to define a custom serialization result (be it a per-invocation function, symbol, or a global function serializer(value) {
if (typeof value === "object" && value !== null) {
if (Array.isArray(value)) { ... }
// naive serializer, doesn't have a way to test whether `value` is a custom serialization result
const result = {};
for (const key of Object.keys(value)) { ... }
return result; // uh oh, we've lost the custom serializer result
}
...
} I suggested on matrix that we consider a // assumes custom serialization result is an exotic object
// with a null prototype, no members, and an internal slot
const isRawString = o =>
typeof o === "object" &&
o !== null &&
Object.getPrototypeOf(o) === null && // do not serialize objects w/prototypes
Object.getOwnPropertyNames(o).length === 0 && // do not serialize arbitrary properties
Object.getOwnSymbolNames(o).length === 0 && // do not serialize arbitrary properties
!['{', '['].includes(JSON.stringify(o)[0]); My concern is that such a test is fairly complex and easy to get wrong, and having the specification include a helper would be more reliable (since we can simply check if the value is an Object that has the required internal slot). |
In #12 (comment), it's proposed that replacer functions would be provided a unique-per-invocation
rawTag
symbol which would be used likeI like the design goals but the design itself is somewhat clunky to use. Can I propose instead providing a per-invocation function which would perform the marking? That is:
I think that ends up being a lot nicer to use while accomplishing the same goals. It's also more in line with what I've seen in the ecosystem when the problem of marking particular values arises in other context - e.g. the
tag
function in this library.Under the hood I'm fine if the implementation of
raw
is to return an object with a particular unique-per-invocation symbol-named property, though it's probably nicer to have it return some new opaque object (and have theJSON.stringify
throw if it encounters such an opaque object produced from something other than the current invocation'sraw
).The text was updated successfully, but these errors were encountered: