当前位置:网站首页>About the configuration and use of json5 in nodejs

About the configuration and use of json5 in nodejs

2022-04-23 14:11:00 Senzhiqianshou

Preface

JSON5 yes JSON An extended subset of . Therefore, it is fully compatible with old JSON, And it has its own unique characteristics :

  • Support comments
  • Double quotation marks are not required , You can use single quotes
  • The key of a key value pair can be enclosed without double quotation marks
  • Allow the last element to be followed by a comma

for instance , natural json It looks like this :

[
  {
    
    "name": "Frank",
    "age": 16,
    "gender": "male",
    "isAdult": false
  },
  {
    
    "name": "Bob",
    "age": 22,
    "gender": "male",
    "isAdult": true
  }
]

Use json5 It became like this :

/* This is a json5 test */
[
  {
    
    name: "Frank",// full name 
    age: 16,// Age 
    gender: "male",
    isAdult: false
  },
  {
    
    name: "Bob",
    age: 22,
    gender: "male",
    isAdult: true
  },
]

This greatly expands JSON Readability .

How to use

Now let's talk about how to work in a NodeJS Used in the project JSON5.

1、 install JSON5 Dependence

npm install json5 --save

2、 New suffix json5 The file of , Let's build a new one ‘tt.json5’

json5
Fill in the above

3、 Read

We installed json5 In fact, there are parse and stringify Two methods , These two methods , about js For the students of development , I can't be more familiar with . In fact, we often use parse This deserialization method .stringify Words , Use your own JSON Can also be realized .

import parse = require('./parse')
import stringify = require('./stringify')

export {
    parse, stringify}

nodeJS You can use your own fs Read the file :

const fs=require('fs')
const JSON5 =require('json5')
fs.open('./tt.json5','r+',(err,d)=>{
    
    fs.readFile('./tt.json5','utf-8',(err,data)=>{
    
        console.log(JSON5.parse(data))
    })
})

What I read from the file is string, So we call parse Method to deserialize .
If you don't want to use require, Instead, I want to use it import introduce , It's OK .

(1) If node Version of is less than 13

To be precise , Less than 13.2.0 Words , Self installation required babel transcoding , Related configuration reference :
Node.js Use Babel Perfect configuration ES6+

(2) If node The version is greater than 13

Need to be in package.json The inside contains name attribute ( Generally the first ) Of object Increase in

"type": "module"

Otherwise it will be reported :

SyntaxError: Cannot use import statement outside a module

Now you can happily introduce :

import * as fs from 'fs'
import JSON5 from 'json5'
fs.open('./tt.json5','r+',(err,d)=>{
    
    console.log(d)
    fs.readFile('./tt.json5','utf-8',(err,data)=>{
    
        console.log(JSON5.parse(data))
    })
})

Be careful :

import JSON5 from 'json5'

there JSON5 It's equivalent to you taking an alias , Even if you write AA It's OK .

thus , Usage introduction end .

版权声明
本文为[Senzhiqianshou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231405036787.html