Powerful of JSON5 2020
It has everything you want! This is the JSON5 standard, which has the following characteristics:
Powerful of JSON5
It has everything you want! This is the JSON5 standard, which has the following characteristics:
Object
- The object key can be exactly the same as the object key in JavaScript
- There can be a comma at the end
Array
- There can be a comma at the end
String
- Strings can be in single quotes
- Strings can use backticks
- Strings can use escape characters
digital
- Numbers can be hexadecimal
- Numbers can start or end with a dot
- Numbers can represent positive infinity, negative infinity, and NaN.
- Numbers can start with a plus sign
comment
- Support single-line and multi-line comments
Space
- Allow extra spaces
As you can see, JSON5 is much more powerful than JSON, a superset of JSON, just like TypeScript compared to JavaScript. The installation method is:
npm install json5
Serialization example:
const JSON5 = require('json5')
const obj = {
name: 'keliq',
age: 12,
}
const res = JSON5.stringify(obj)
console.log(res) // {name:'keliq',age:12}
Deserialization example:
const JSON5 = require('json5')
const json5str = `// one line comment
{
name:'keliq', // Name
age:12, /*The age*/ }`
console.log(JSON5.parse(json5str))