npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json5-writer

v0.2.0

Published

Comment-preserving JSON / JSON5 parser

Downloads

410,022

Readme

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. Alias for .toJSON5().

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.