golden-fleece
Parse a JSON5 string (like JSON, but less strict).
Why?
For the Svelte REPL, where we want to allow arbitrary data in the bottom right-hand panel, but we also want to update the object without reformatting it as JSON.
Usage
Install it with npm install golden-fleece
and import it into your app:
;
fleece.parse(str, [options])
const ast = fleece;// { start: 0, end: 4, type: 'Literal', raw: 'true', value: true }
The returned AST is ESTree compliant.
You can optionally pass callbacks that are fired whenever a value or comment is encountered:
const ast = fleece;
fleece.evaluate(str)
const answer = fleece;answer === 42; // true
fleece.patch(str, value)
This is where it gets fun:
const str = ` number: 1, string: 'yes', object: { nested: true }, array: ['this', 'that', 'the other']`; const object = fleece;objectnumber = 42;objectarray2 = 'EVERYTHING'; fleece === `{ number: 42, string: 'yes', object: { nested: true }, array: ['this', 'that', 'EVERYTHING']}`; // true
Notice that the formatting has been preserved.
fleece.stringify(value, [options])
const object = string: 'hello' 'quoted-property': 2 array: 3 4; fleece === `{ string: "hello", "quoted-property": 2, array: [ 3, 4 ]}`; // true
To indent with spaces instead of tabs, pass spaces: n
, where n
is the number of spaces at each level of indentation.
fleece === `{ string: "hello", "quoted-property": 2, array: [ 3, 4 ]}`; // true
To prefer single-quotes to double-quotes, pass singleQuotes: true
:
fleece === `{ string: 'hello', 'quoted-property': 2, array: [ 3, 4 ]}`; // true