JSON5 is a superset of JSON that extends the syntax by introducing some ECMAScript 5.1 features to reduce some of the limitations of the JSON format. At the same time, maintain compatibility with existing JSON formats.

JSON5 is more like an object in JavaScript, but it is not an official JSON extension, so you need JSON5 as the file extension.

Difference from JSON

Object Objects

  • The key name of the object can be any ES5.1 identifier.
  • Objects can end with a comma.

An array of Arrays

  • Arrays can end with a comma.

Strings Strings

  • Strings can be represented by single quotes.
  • Strings can occupy more than one line of text, with a “newline”.
  • A string can include escape characters.

Digital Numbers

  • You can use hexadecimal numbers.
  • A number can start/end with a decimal point.
  • The number can be Infinity / -infinity/NaN. See the IEEE754
  • Numbers can begin with an unambiguous plus sign “+”.

Comments Comments

  • Single-line/multi-line comments are allowed

The whitespace character White Space

  • Allow extra whitespace

For example, 🌰

{ // comments unquoted: 'and you can quote me on that', singleQuotes: 'I can use "double quotes" here', lineBreaks: "Look, Mom! \ No \\n's!" , hexadecimal: 0xdecaf, leadingDecimalPoint: .8675309, andTrailing: 8675309., positiveSign: +1, trailingComma: 'in objects', andIn: ['arrays',], "backwardsCompatible": "with JSON", }Copy the code

The installation

Node.js

npm install json5Copy the code
const JSON5 = require('json5')Copy the code

Browser environment

< script SRC = "https://unpkg.com/json5@ ^ 1.0.0" > < / script >Copy the code

This generates a global JSON5 variable.

API

The JSON5 API is JSON-compatible.

JSON5.parse()

Parse a JSON5 string and build a JavaScript value or object from the string. If the reviver function is specified, the parsed JavaScript value (parsed value) will undergo a transformation before it is finally returned (return value).

grammar

JSON5.parse(text[, reviver])Copy the code

parameter

Text: String to parse.

Reviver: If specified as a function, the parsed JavaScript value (the parsed value) will undergo a transformation before it is finally returned (the return value).

The return value

Corresponds to a JavaScript value or object.

JSON5.stringify()

Converts a JavaScript value to a JSON5 string, providing an optional replacement function, or specifying some of the attributes to export.

grammar

JSON5.stringify(value[, replacer[, space]])
JSON5.stringify(value[, options])Copy the code

parameter

Value: JavaScript value to be converted

Replacer: If it is a function, the default conversion function is replaced. If it is an array of strings/numbers, the result will contain only the attributes specified by the array. If null or not provided, all attributes are included in the result.

Space: can be a string or a number with a maximum length of 10 characters. If numeric, it indicates how many Spaces are used to represent whitespace. If it is a string, whitespace is replaced with that string.

Options: an object containing the following properties:

  • replacerAnd:replacerThe parameters are consistent.
  • spaceAnd:spaceThe parameters are consistent.
  • quote: The reference character used to indicate the sequence number string.

The return value

JSON5 The character string.

Node.js require() JSON5 files

In Node.js, you can support importing json5 files by adding the following statements.

require('json5/lib/register')Copy the code

Then import the json5 file via node.js’s require statement.

const config = require('./config.json5')Copy the code

The command line CLI

JSON5 provides a command-line tool to convert JSON5 files to JSON format and validate the syntax of JSON5 files.

The installation

npm install --global json5Copy the code

use

json5 [options] <file>Copy the code

If no file argument is provided, standard input (STDIN) is used.

Options: -s, –space: the number of Spaces to indent. T means TAB indent. -o, –out-file [file]: Specifies the output file. If empty, standard output (STDOUT) is used. -v, –validate: verifies JSON5 syntax. -v, –version: indicates the version number. -h, –help: indicates the help information.