Skip to content
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

feat: support Date object serialization, make json more consistent with JSON.stringify #58

Closed
wants to merge 2 commits into from

Conversation

skeggse
Copy link

@skeggse skeggse commented Apr 3, 2020

Fixes #56, #57.

Copy link
Owner

@mathiasbynens mathiasbynens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the README as well? E.g. here https://github.com/mathiasbynens/jsesc#jsescvalue-options

'json': true
}
),
'"2020-04-03T15:34:00.000Z"'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This depends on how we want to deal with #57, Is this what you'd expect when passing a Date/Map/Set while using json: true? Or would you want to throw an exception instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way this would need to be documented in the README under the json option.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the behavior of toJSON. I'd like to add an additional flag in a subsequent PR (or, I suppose, in this PR) to let the user override that behavior independent of the JSON mode.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could include an interpret option that accepts values like expect-json, coerce-json, and javascript to switch between the options. expect-json would throw on non-JSON-supported values; coerce-json would invoke toJSON and delegate to JSON.stringify's behavior in other cases; javascript would do its best to serialize all (non-circular) values for use in a js context.

I don't need expect-json for my own work, but coerce-json would be extremely useful. I've also been wondering if we could introduce a replacer-type option for both transforming and extending the supported values - e.g. we need strict coerce-json semantics, but also want to support the undefined value. If you're amenable to that, I'll see what that would take.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I've used jsesc is always very controlled, in that: if there's a Date object, a Map, a Set, or a BigInt somewhere in the input, and I am requesting JSON output via json: true, that's probably a mistake. I've only used jsesc in cases where I want JSON.parse(output) to produce an equivalent object again.

So I defer to you on this. If you have a use case for it, that's great!

For consistency with JSON.stringify. Backwards-compatible because JSON
parsers would break on json: true output if Map or Set included in said
output.
@skeggse skeggse changed the title feat: support Date object serialization feat: support Date object serialization, make json more consistent with JSON.stringify Apr 6, 2020
@@ -134,7 +137,7 @@ const jsesc = (argument, options) => {
}

if (!isString(argument)) {
if (isMap(argument)) {
if (!json && isMap(argument)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A potential solution for #57; Map and Set don't have toJSON methods - and even if they did, they probably wouldn't return the same type, so the expectation here is that we never process a Map or Set in a special way when targetting JSON.

@skeggse
Copy link
Author

skeggse commented Oct 15, 2020

I don't have use for this anymore, and I think I decided much of the problem I was encountering was just me using the wrong tool.

@skeggse skeggse closed this Oct 15, 2020
@skeggse skeggse deleted the support-date-objects branch October 2, 2024 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support Date objects
3 participants