Comments (10)
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.
Oh! Sorry
from json5.
No worries. =)
from json5.
{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.
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.
require('./json5').parse('{1:true}')
doesn't work. I don't know whether it counts as unexpected or not.
from json5.
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.
(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.
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.
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)
- Explain use cases front and center HOT 3
- Module '"node_modules/json5/lib/index"' has no default export HOT 1
- Support Integers outside the range `[-(2**53)+1, (2**53)-1]` HOT 6
- SyntaxError when require()ing JSON5 file in Jest test HOT 1
- Prototype Pollution in JSON5 HOT 11
- json5 latest is now 1.0.2 on npmjs - intentional? HOT 3
- Support Template Literals using backticks? HOT 1
- Provide `exports` config in `packages.json`
- Multiline JSON doesn't multiline HOT 1
- add key property support ? HOT 1
- Add `comma-dangle` option HOT 7
- Online JSON5 Editor(Formatter) is misleading? HOT 2
- Use .substring() instead of the deprecated .substr()
- Cannot stringify "\u0000"
- transitive dependency 'minimist' needs to be updated by rebuilding HOT 1
- question: commented unreachable code HOT 10
- JSON5.stringify() option: `trailingCommas` HOT 3
- Focus on ESNext HOT 1
- CLI should process consecutive JSONs HOT 1
- migrate to ESModules HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json5.