Git Product home page Git Product logo

json5-writer's Introduction

json5-writer

Comment-preserving JSON / JSON5 parser

json5-writer provides an API for parsing JSON and JSON5 without losing comments or formatting. It does so by transforming JSON5 into a JavaScript AST and using jscodeshift to update values.

Example

const json5Writer = require('json5-writer')
const config = fs.readFileSync('config.json5', 'utf-8')
const writer = json5Writer.load(config)
writer.write({
  'eat honey': { cooldown: 3 },
  speak: { cooldown: 2 },
  bear: { actions: ['eat honey', 'speak'] },
})
fs.writeFileSync('config.json5', writer.toSource(), 'utf-8')

config.json5 diff

 {
   // actions
   'eat honey': {
-    cooldown: 4,
+    cooldown: 3,
   },
+
+  'speak': {
+    cooldown: 2,
+  },

   // Note: A day without a friend is like a pot without a single drop of honey left inside.

   // entities
   'bear':  {
-    actions: [ 'eat honey' ],
-    canSpeak: true,
+    actions: ['eat honey', 'speak'],
   },
 }

Installation

npm install --save json5-writer

Usage

const writerInstance = json5Writer.load(jsonStr) // get a writer instance for the given JSON / JSON5 string
writerInstance.write(objectOrArray) // update jsonStr, preserving comments
const ast = writerInstance.ast // directly access the AST with jscodeshift API
const newJson5 = writerInstance.toSource(options) // get the modified JSON5 string
const newJson = writerInstance.toJSON(options) // get the modified JSON string

.write(value)

Updates the JSON / JSON5 string with the new value. Any field or property that doesn't exist in value is removed.

To keep an existing value, use undefined:

const writer = json5Writer.load(`[{ name: 'Noah' }, { name: 'Nancy' }]`)
writer.write([{ name: undefined, age: 28 }, undefined ])
write.toSource() // [{ name: 'Noah', age: 28 }, { name: 'Nancy' }]

.ast

Directly access the JSON5-turned-JavaScript AST, wrapped in the jscodeshift API.

const j = require('jscodeshift')
const writer = json5Writer.load('[1, 2, 3, 4]')
writer.ast.find(j.Literal).forEach(path => {
  if (path.value.value % 2 === 0) path.value.value = 0
})
write.toSource() // [1, 0, 3, 0]

.toSource(options)

Get the modified JSON5 string.

options control what is output. By default, single quotes and trailing commas are enabled and key quote usage is inferred.

.toSource({ quote: 'single', trailingComma: true, quoteKeys: undefined })
  • quoteKeys controls whether object keys are quoted. It can have three different values:
    • false - no object keys will have quotes
    • true - all object keys will have quotes
    • undefined - object key quote usage is inferred [default]
  • quote can be either single or double

View the remaining options here.

.toJSON(options)

Same as .toSource(options) but with quote: 'double', trailingComma: false, quoteKeys: true by default.

json5-writer's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.