Talking about JSON5 Solving the Two Pain Points of JSON
- 2021-10-15 09:28:33
- OfStack
Can I get rid of double quotation marks in key
Powerful JSON5
Object array string numeric comment space
JSON format can be said to be the most popular data transmission format at present, which is widely used in front-end and back-end communication, especially in SPA application, JSON data is transmitted through HTTP protocol, which has the advantages of small size, easy serialization and good readability. (Of course, these advantages are relative. For example, small size is relative to XML, and if compared with protobuf, it is much larger.)
Although JSON is good, there are still two major pain points that annoy developers:
After serialization, key is enclosed in double quotation marks (the volume becomes larger)
How do I add a comment
The current standard is that you can't add annotations. If you want to add them, you can only save the nation by curves. For example, I did this:
{
"----------base----------": " Universal module variable definition ",
"common": {
"object_not_exit": " Object ${id} It doesn't exist! ",
"invalid_username_or_password": " Wrong username or password! "
},
"----------sms----------": " SMS module related variables ",
"sms": {
"template_missing_parameters": " Template is missing variables! ",
"param_length_limit": " Parameter exceeds length limit! "
}
}
To sum up, there are three methods:
1. Use the convention key as the comment field:
For example, key with//, _ comment, # # # # #,---(# or the number of-is self-determined) as annotations.
2. Use the duplicate name key as a comment:
That is, each key is used twice, the first time for annotation and the second time for actual attributes.
3. Annotate key with the field key prefixed:
Commonly used prefixes are #, _, etc.
Can I get rid of double quotation marks in key
The serialized key is enclosed in double quotation marks, for example:
const obj = { name: 'keliq', age: 12 }
console.log(JSON.stringify(obj))
// {"name":"keliq","age":12}
If you look closely, you can find that the key of the object does not have double quotation marks, but after serialization, both sides are quoted, resulting in more characters, and then the problem comes:
Why put key in double quotation marks? Can you remove the double quotation marks in key?Let's talk about a historical background first:
In ECMAScript 3 Reserved words cannot be used as objects in key For example:
{function: 0} // Syntax error
{if: 0} // Syntax error
{true: 0} // Syntax error
Can only be given key Add double or single quotation marks:
{"function": 0} // Ok
{"if": 0} // Ok
{"true": 0} // Ok
However, after ES5, reserved words can also be used as key. Therefore, without considering backward compatibility, it would be nice to express JSON objects as 1-mode 1-like JavaScript objects, just delete the middle spaces and newlines!
Powerful JSON5
It has everything you want! This is the JSON5 standard, which has the following features:
Object
The key of the object can be exactly the same as the key of the JavaScript
You can have a comma at the end
Array
You can have a comma at the end
String
Strings can be quoted in single quotes Strings can be quoted in reverse quotes Strings can use escape characters
Figures
Numbers can be hexadecimal Numbers can begin or end with dots Numbers can represent positive infinity, negative infinity and NaN. Numbers can begin with a plus sign
Comments
Support single-line and multi-line comments
Space
Allow extra spaces
It can be seen that JSON5 is much stronger than JSON and is a superset of JSON, just like TypeScript compared with JavaScript. The installation method is:
npm install json5
# Or yarn add 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}
Sample deserialization:
const JSON5 = require('json5')
const json5str = `// Single-line comment
{
name:'keliq', // This is the name
age:12, /* This is the age */
}`
console.log(JSON5.parse(json5str))
Seeing this, I can't help but sigh that this is what JSON should look like! What do you say?