Code Monkey home page Code Monkey logo

Comments (10)

aseemk avatar aseemk commented on September 25, 2024

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.

from json5.

mohsen1 avatar mohsen1 commented on September 25, 2024

Oh! Sorry

from json5.

aseemk avatar aseemk commented on September 25, 2024

No worries. =)

from json5.

rlidwka avatar rlidwka commented on September 25, 2024

{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

from json5.

aeisenberg avatar aeisenberg commented on September 25, 2024

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.

from json5.

rlidwka avatar rlidwka commented on September 25, 2024

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

from json5.

aseemk avatar aseemk commented on September 25, 2024

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.

from json5.

rlidwka avatar rlidwka commented on September 25, 2024

(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.

from json5.

aseemk avatar aseemk commented on September 25, 2024

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

from json5.

jordanbtucker avatar jordanbtucker commented on September 25, 2024

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.

from json5.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.