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

JSON5.parse("{1:true}"); #10

Closed
mohsen1 opened this issue Jun 2, 2012 · 10 comments
Closed

JSON5.parse("{1:true}"); #10

mohsen1 opened this issue Jun 2, 2012 · 10 comments

Comments

@mohsen1
Copy link

mohsen1 commented Jun 2, 2012

JSON5.parse("{1:true}"); <== error

@aseemk
Copy link
Member

aseemk commented Jun 2, 2012

Yep — this in invalid JS too:

> {1:true}
SyntaxError: Unexpected token :

Unquoted keys have the same rules as variables ("identifiers"). Here's a reference:

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables

I'll update the readme to be clearer about this.

@aseemk aseemk closed this as completed Jun 2, 2012
aseemk added a commit that referenced this issue Jun 2, 2012
@mohsen1
Copy link
Author

mohsen1 commented Jun 2, 2012

Oh! Sorry

@aseemk
Copy link
Member

aseemk commented Jun 2, 2012

No worries. =)

@rlidwka
Copy link
Contributor

rlidwka commented Oct 4, 2013

{1:true} is a valid JS object.

An example of this notation can be found on the link you posted in the "String literals" chapter:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Object_literals

@aeisenberg
Copy link
Collaborator

This is currently working for me. From the node prompt:

> require('./json5').parse(require('./json5').stringify({1:true}))
{ '1': true }

If there's something unexpected, then please raise a new issue.

ps- you are correct that {1:true} is valid JS.

@rlidwka
Copy link
Contributor

rlidwka commented Oct 4, 2013

require('./json5').parse('{1:true}') doesn't work. I don't know whether it counts as unexpected or not.

@aseemk
Copy link
Member

aseemk commented Oct 5, 2013

Yes, you're right @rlidwka:

> {1:true}
SyntaxError: Unexpected token :
> ({1:true})
Object {1: true}

(Interestingly, the first case gives that syntax error in Chrome, but not Node.)

That we reject this is currently expected, because I was under the impression that ES5's spec on object keys applies the same rules for unquoted keys as it does to variable names (hence the link I shared above).

If it's valid ES5 though, perhaps we should allow this... It'd be nice to confirm that this is allowed per ES5 though, not just an "it-so-happens" behavior of existing implementations.

@aseemk aseemk reopened this Oct 5, 2013
@rlidwka
Copy link
Contributor

rlidwka commented Oct 5, 2013

(Interestingly, the first case gives that syntax error in Chrome, but not Node.)

It can happen because first "{" is treated as a beginning of a block (as in {var x=0;}), not as an object.

If you want to test such kind of things reliably, add parentheses around the statement. This: ({1:true}) should work everywhere.

@aseemk
Copy link
Member

aseemk commented Oct 5, 2013

Yep, I'm aware of the ambiguity, I was just surprised that two different instances of the same JS implementation treat the ambiguity differently.

@jordanbtucker
Copy link
Member

Closing as JSON5 will not support numeric literals as property names until there is enough demand for it. #55 is now the official issue regarding this.

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

No branches or pull requests

5 participants