JSON format can be said to be the most popular data transmission format at present, which is widely used in front and back end communication, especially in SPA application. JSON data is transmitted through HTTP protocol, which has the advantages of small volume, easy serialization, good readability and so on. (Of course, these advantages are relative; for example, small size is relative to XML, and much larger compared to Protobuf.)

JSON is great, but there are still two big pain points for developers:

  • Can’t add comments
  • The serialized key is double quoted (bigger)

How to Add comments

The current standard is not to add comments, if you want to add, only curve to save the country, for example, I do this:

{
  "----------base----------": "Generic module variable Definitions"."common": {
    "object_not_exit": "${id} does not exist!"."invalid_username_or_password": "Incorrect user name or password!"
  },
  "----------sms----------": "SMS module related variables"."sms": {
    "template_missing_parameters": "Template missing variables!"."param_length_limit": "Parameter exceeds length limit!"}}Copy the code

There are generally three ways to summarize:

  • Use the convention key as the comment field:

    As to / /, _comment, # # # # #, – (set) # or – the number of key as the annotation.

  • Use the same key as the comment:

    That is, for each key, you use it twice, the first time for the comment, the second time for the actual attribute.

  • Annotate key with field key prefixed:

    Common prefixes are #, _, etc.

Can you remove the double quotes in key

The serialized key is double quoted, for example:

const obj = { name: 'keliq'.age: 12 }
console.log(JSON.stringify(obj))
// {"name":"keliq","age":12}
Copy the code

If you take a closer look, you can see that the key of the object is not double quoted, but after serialization, both sides of the object are quoted, which causes the number of characters to increase.

  • Why do I double quote key?
  • Can we get rid of the double quotes in key?

Here’s a historical context:

In ECMAScript 3, reserved words cannot be used as the key of an object. For example:

{function: 0} // Syntax error
{if: 0} // Syntax error
{true: 0} // Syntax error
Copy the code

You can only add double or single quotes to keys:

{"function": 0} // Ok
{"if": 0} // Ok
{"true": 0} // Ok
Copy the code

However, after ES5, reserved words are also available as keys, so it would be nice to represent JSON objects as JavaScript objects without backward compatibility, except for the middle Spaces, line breaks, etc.

Powerful JSON5

It has everything you want! This is the JSON5 standard, which has the following features:

object

  • The key of an object can be exactly the same as the key of a JavaScript object
  • There can be a comma at the end

An array of

  • There can be a comma at the end

string

  • Strings can be in single quotes
  • Strings can be backquoted
  • Strings can use escape characters

digital

  • Numbers can be in hexadecimal
  • Numbers can begin or end with a dot
  • Numbers can represent plus infinity, minus infinity, and NaN.
  • Numbers can start with a plus sign

comments

  • Single-line and multi-line comments are supported

The blank space

  • Allow extra Spaces

As you can see, JSON5 is much more powerful than JSON and is a superset of JSON, just as TypeScript is compared to JavaScript. Installation method:

npm install json5
# yarn add json5
Copy the code

Example serialization:

const JSON5 = require('json5')
const obj = {
  name: 'keliq'.age: 12,}const res = JSON5.stringify(obj)
console.log(res) // {name:'keliq',age:12}
Copy the code

Example of deserialization:

const JSON5 = require('json5')
const json5str = ` / / single-line comments {name: 'keliq', / / this is the name age: 12, / * * /} this is age `
console.log(JSON5.parse(json5str))
Copy the code

See here, can not help but sigh, this is JSON should look like! What do you say?