🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

json5

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json5 - npm Package Compare versions

Comparing version

to
0.2.0

.travis.yml

49

CHANGELOG.md

@@ -0,1 +1,46 @@

### Unreleased [[code][cNew], [diff][dNew]]
[cNew]: https://github.com/aseemk/json5/tree/develop
[dNew]: https://github.com/aseemk/json5/compare/master...develop
These changes are sitting unreleased on the `develop` branch:
- (Nothing yet.)
### v0.2.0 [[code][c0.2.0], [diff][d0.2.0]]
[c0.2.0]: https://github.com/aseemk/json5/tree/v0.2.0
[d0.2.0]: https://github.com/aseemk/json5/compare/v0.1.0...v0.2.0
This release fixes some bugs and adds some more utility features to help you
express data more easily:
- **Breaking:** Negative hexadecimal numbers (e.g. `-0xC8`) are rejected now.
While V8 (e.g. Chrome and Node) supported them, it turns out they're invalid
in ES5. This has been [fixed in V8][v8-hex-fix] (and by extension, Chrome
and Node), so JSON5 officially rejects them now, too. ([#36][])
[v8-hex-fix]: http://code.google.com/p/v8/issues/detail?id=2240
[#36]: https://github.com/aseemk/json5/issues/36
- **Breaking:** Trailing decimal points in decimal numbers are allowed again.
They're allowed by ES5, and differentiating between integers and floats may
make sense on some platforms. ([#16][]; thanks [@Midar][].)
[#16]: https://github.com/aseemk/json5/issues/16
- **New:** `Infinity` and `-Infinity` are now allowed number literals.
([#30][]; thanks [@pepkin88][].)
[#30]: https://github.com/aseemk/json5/issues/30
- **New:** Plus signs (`+`) in front of numbers are now allowed, since it can
be helpful in some contexts to explicitly mark numbers as positive.
(E.g. when a property represents changes or deltas.)
- Bug fix: unescaped newlines in strings are rejected now. ([#24][]; thanks
[@Midar][].)
[#24]: https://github.com/aseemk/json5/issues/24
### v0.1.0 [[code][c0.1.0], [diff][d0.1.0]]

@@ -21,3 +66,3 @@

are disallowed by both JSON5 and JSON. (Thanks [@MaxNanasy][].)
- **Breaking:** Reject omitted elements in arrays now. These are disallowed by

@@ -58,1 +103,3 @@ both JSON5 and JSON.

[@MaxNanasy]: https://github.com/MaxNanasy
[@Midar]: https://github.com/Midar
[@pepkin88]: https://github.com/pepkin88

49

lib/json5.js

@@ -99,9 +99,20 @@ // json5.js

var number,
sign = '',
string = '',
base = 10;
if (ch === '-') {
string = '-';
next('-');
if (ch === '-' || ch === '+') {
sign = string = ch;
next(ch);
}
// support for Infinity (could tweak to allow other words):
if (ch === 'I') {
number = word();
if (typeof number !== 'number' || isNaN(number)) {
error('Unexpected word for number');
}
return (sign === '-') ? -number : number;
}
if (ch === '0') {

@@ -118,2 +129,8 @@ string += ch;

}
// https://github.com/aseemk/json5/issues/36
if (base === 16 && sign) {
error('Signed hexadecimal literal');
}
switch (base) {

@@ -127,10 +144,5 @@ case 10:

string += '.';
next('.');
if (ch < '0' || ch > '9') {
error('Trailing decimal point');
while (next() && ch >= '0' && ch <= '9') {
string += ch;
}
do {
string += ch;
next();
} while (ch && ch >= '0' && ch <= '9');
}

@@ -200,2 +212,8 @@ if (ch === 'e' || ch === 'E') {

}
} else if (ch === '\n') {
// unescaped newlines are invalid; see:
// https://github.com/aseemk/json5/issues/24
// TODO this feels special-cased; are there other
// invalid unescaped chars?
break;
} else {

@@ -315,2 +333,12 @@ string += ch;

return null;
case 'I':
next('I');
next('n');
next('f');
next('i');
next('n');
next('i');
next('t');
next('y');
return Infinity;
}

@@ -416,2 +444,3 @@ error("Unexpected '" + ch + "'");

case '-':
case '+':
case '.':

@@ -418,0 +447,0 @@ return number();

{
"name": "json5",
"version": "0.1.0",
"version": "0.2.0",
"description": "JSON for the ES5 era.",

@@ -20,2 +20,3 @@ "keywords": [

"scripts": {
"build": "./lib/cli.js -c package.json5",
"test": "mocha --ui exports --reporter spec"

@@ -22,0 +23,0 @@ },

@@ -0,1 +1,3 @@

[![Build Status](https://travis-ci.org/aseemk/json5.png)](https://travis-ci.org/aseemk/json5)
# JSON5 – Modern JSON

@@ -5,3 +7,3 @@

quoted; objects and arrays can't have trailing commas; comments aren't
supported — even though none of these is the case with regular JavaScript
supported — even though none of these are the case with regular JavaScript
today.

@@ -31,9 +33,19 @@

These are the new features of JSON5's syntax. All of these are optional, and
all of these are part of ES5 JavaScript.
These are the new features of JSON5's syntax. **All of these are optional**,
and **all of these are part of ES5 JavaScript**.
- Object keys don't need to be quoted if they're valid [identifiers](
### Objects
- Object keys can be unquoted if they're valid [identifiers](
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables). Yes, even reserved keywords are valid unquoted keys in ES5 [[§11.1.5](http://es5.github.com/#x11.1.5), [§7.6](http://es5.github.com/#x7.6)].
*[TODO: Unicode characters and escape sequences aren't yet supported in this implementation.]*
- Objects can have trailing commas.
### Arrays
- Arrays can have trailing commas.
### Strings
- Strings can be single-quoted.

@@ -44,9 +56,17 @@

- Objects and arrays can have trailing commas.
### Numbers
- Numbers can be hexadecimal (base 16). (Note that signed hexadecimals are not
allowed by ES5, nor are hexadecimal floats.)
- Numbers can begin or end with a (leading or trailing) decimal point.
- Numbers can include `Infinity` and `-Infinity`.
- Numbers can begin with an explicit plus (`+`) sign.
### Comments
- Both inline (single-line) and block (multi-line) comments are allowed.
- Numbers can be hexadecimal (base 16), and they can also begin with a leading
decimal (e.g. `.5`).
## Example

@@ -58,9 +78,9 @@

while: true,
this: 'is a\
multi-line string',
this: 'is a \
multi-line string',
// this is an inline comment
here: 'is another', // inline comment
/* this is a block comment

@@ -71,3 +91,5 @@ that continues on another line */

half: .5,
delta: +10,
to: Infinity, // and beyond!
finally: 'a trailing comma',

@@ -105,4 +127,2 @@ oh: [

var str = JSON5.stringify(obj);
console.log(obj);
console.log(str);
```

@@ -134,10 +154,2 @@

cd json5
make
make test
```
If your system doesn't have Make, this should work in place of `make [test]`:
```
./lib/cli.js -c package.json5
npm install

@@ -147,13 +159,10 @@ npm test

Make is used to auto-generate the package.json file that npm requires from our
package.json5 file. Just re-run `make` (or `./lib/cli.js -c package.json5`) on
changes to package.json5.
As the `package.json5` file states, be sure to run `npm run build` on changes
to `package.json5`, since npm requires `package.json`.
Feel free to [file issues](https://github.com/aseemk/json5/issues) and submit
[pull requests](https://github.com/aseemk/json5/pulls) — contributions are
welcome.
welcome. If you do submit a pull request, please be sure to add or update
corresponding test cases, and ensure that `npm test` continues to pass.
If you submit a pull request, please be sure to add or update corresponding
test cases, and ensure that `make test` (or `npm test`) continues to pass.
## License

@@ -160,0 +169,0 @@

@@ -42,16 +42,24 @@ // parse.js

case '.json':
assert.deepEqual(parseJSON5(), parseJSON());
assert.deepEqual(parseJSON5(), parseJSON(),
'Expected parsed JSON5 to equal parsed JSON.');
break;
case '.json5':
assert.throws(parseJSON); // test validation
assert.deepEqual(parseJSON5(), parseES5());
assert.throws(parseJSON, // test validation
'Test case bug: expected JSON parsing to fail.');
assert.deepEqual(parseJSON5(), parseES5(),
'Expected parsed JSON5 to equal parsed ES5.');
break;
case '.js':
assert.throws(parseJSON); // test validation
assert.doesNotThrow(parseES5); // test validation
assert.throws(parseJSON5);
assert.throws(parseJSON, // test validation
'Test case bug: expected JSON parsing to fail.');
assert.doesNotThrow(parseES5, // test validation
'Test case bug: expected ES5 parsing not to fail.');
assert.throws(parseJSON5,
'Expected JSON5 parsing to fail.');
break;
case '.txt':
assert.throws(parseES5); // test validation
assert.throws(parseJSON5);
assert.throws(parseES5, // test validation
'Test case bug: expected ES5 parsing to fail.');
assert.throws(parseJSON5,
'Expected JSON5 parsing to fail.');
break;

@@ -58,0 +66,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet