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

Stringify throws if the value isn't an Object #154

Closed
tobiasandersen opened this issue Sep 15, 2017 · 5 comments
Closed

Stringify throws if the value isn't an Object #154

tobiasandersen opened this issue Sep 15, 2017 · 5 comments
Assignees
Labels
Milestone

Comments

@tobiasandersen
Copy link

tobiasandersen commented Sep 15, 2017

If I do JSON5.stringify(json) when json doesn't have Object.prototype, it will fail. I can work around it by doing something like:

if (!(json instanceof Object)) {
  json = { ...json }
}

JSON5.stringify(json)

But I feel like this library should be able to stringify JSON structures event if they aren't Objects. If you don't agree, feel free to close this.

@jordanbtucker
Copy link
Member

JSON5 is able to stringify objects, arrays, strings, numbers, Booleans, and nulls. Anything else, including functions and symbols, should return undefined. It should only throw when you have a circular reference or when the replacer argument is truthy but neither function nor array.

What are you passing into stringify that is causing it to throw, and what is the error message?

@tobiasandersen
Copy link
Author

Here's an example: https://jsfiddle.net/j1156ewj/1/

@jordanbtucker
Copy link
Member

I see where the error is now. Thanks.

I might patch this soon if it's an easy fix, but in the meantime, you could use JSONext. It supports JSON5 syntax but also supports Unicode keys and includes some ES6 features. It doesn't suffer from this bug as you can see in this updated example: https://jsfiddle.net/j1156ewj/2/

@tobiasandersen
Copy link
Author

Thanks! I've worked around it already, just thought I'd let you know.

@jordanbtucker jordanbtucker self-assigned this Sep 20, 2017
@jordanbtucker jordanbtucker added this to the v1.0.0 milestone Sep 20, 2017
@jordanbtucker
Copy link
Member

Fixed in 35269da.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants