"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/jju/README.md" (17 Oct 2016, 8542 Bytes) of archive /windows/misc/atom-windows.zip:

As a special service "Fossies" has tried to format the requested source page into HTML format (assuming markdown format). Alternatively you can here view or download the uninterpreted source code file. A member file download can also be achieved by clicking within a package contents listing on the according byte size field.

jju - a set of utilities to work with JSON / JSON5 documents

npm version badge travis badge downloads badge


npm install jju


This module provides following functions:

  1. jju.parse() parses json/json5 text and returns a javascript value it corresponds to
  2. jju.stringify() converts javascript value to an appropriate json/json5 text
  3. jju.tokenize() parses json/json5 text and returns an array of tokens it consists of (see demo)
  4. jju.analyze() parses json/json5 text and tries to guess indentation, quoting style, etc.
  5. jju.update() changes json/json5 text, preserving original formatting as much as possible (see demo)

All functions are able to work with a standard JSON documents. jju.parse() and jju.stringify() are better in some cases, but slower than native JSON.parse() and JSON.stringify() versions. Detailed description see below.

jju.parse() function

``javascript /* * Main syntax: * *text- text to parse, type: String *options` - parser options, type: Object */ jju.parse(text[, options])

// compatibility syntax jju.parse(text[, reviver]) ```


```javascript // ‘ignore’ will cause reserved keys to be ignored: parse(‘{hasOwnProperty: 1}’, {reserved_keys: ‘ignore’}) == {} parse(‘{hasOwnProperty: 1, x: 2}’, {reserved_keys: ‘ignore’}).hasOwnProperty(‘x’) == true

// ‘throw’ will cause SyntaxError in these cases: parse(‘{hasOwnProperty: 1}’, {reserved_keys: ‘throw’}) == SyntaxError

// ‘replace’ will replace reserved keys with new ones: parse(‘{hasOwnProperty: 1}’, {reserved_keys: ‘throw’}) == {hasOwnProperty: 1} parse(‘{hasOwnProperty: 1, x: 2}’, {reserved_keys: ‘ignore’}).hasOwnProperty(‘x’) == TypeError ```

jju.stringify() function

``javascript /* * Main syntax: * *value- value to serialize, type: * *options` - serializer options, type: Object */ jju.stringify(value[, options])

// compatibility syntax jju.stringify(value[, replacer [, indent]) ```


jju.tokenize() function

javascript /* * Main syntax: * * `text` - text to tokenize, type: String * `options` - parser options, type: Object */ jju.tokenize(text[, options])

Options are the same as for the jju.parse function.

Return value is an array of tokens, where each token is an object:

You can check tokenizer for yourself using this demo.

jju.analyze() function

javascript /* * Main syntax: * * `text` - text to analyze, type: String * `options` - parser options, type: Object */ jju.analyze(text[, options])

Options are the same as for the jju.parse function.

Return value is an object defining a programming style in which the document was written.

jju.update() function

javascript /* * Main syntax: * * `text` - original text, type: String * `new_value` - new value you want to set * `options` - parser or stringifier options, type: Object */ jju.update(text, new_value[, options])

If you want to update a JSON document, here is the general approach:

```javascript // here is your original JSON document: var input = ‘{“foo”: “bar”, “baz”: 123}’

// you need to parse it first: var json = jju.parse(input, {mode: ‘json’}) // json is { foo: ‘bar’, baz: 123 }

// then you can change it as you like: json.foo = ‘quux’ json.hello = ‘world’

// then you run an update function to change the original json: var output = jju.update(input, json, {mode: ‘json’}) // output is ‘{“foo”: “quux”, “baz”: 123, “hello”: “world”}’ ```

Look at this demo to test various types of json.

Advantages over existing JSON libraries

In a few cases it makes sense to use this module instead of built-in JSON methods.

Parser: - better error reporting with source code and line numbers

In case of syntax error, JSON.parse does not return any good information to the user. This module does:

``` $ node -e ‘require(“jju”).parse(“[1,1,1,1,invalid]”)’

SyntaxError: Unexpected token ‘i’ at 0:9 [1,1,1,1,invalid] ^ ```

This module is about 5 times slower, so if user experience matters to you more than performance, use this module. If you’re working with a lot of machine-generated data, use JSON.parse instead.

Stringifier: - util.inspect-like pretty printing

This module behaves more smart when dealing with object and arrays, and does not always print newlines in them:

$ node -e 'console.log(require("./").stringify([[,,,],,,[,,,,]], {mode:"json"}))' [ [null, null, null], null, null, [null, null, null, null] ]

JSON.stringify will split this into 15 lines, and it’s hard to read.

Yet again, this feature comes with a performance hit, so if user experience matters to you more than performance, use this module. If your JSON will be consumed by machines, use JSON.stringify instead.

As a rule of thumb, if you use “space” argument to indent your JSON, you’d better use this module instead.