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

@johnls/json5

v2.3.0

Published

JSON for humans.

Downloads

9

Readme

JSON5 – JSON for Humans

This project is a fork of JSON5. See that project for the base documentation.

This fork adds the ability to parse the JSON5 into a node tree that includes type, line and column information on each element of the JSON5 text. This is useful for applications that do further semantic processing on the JSON5 and want to report error locations back to the user. You can also simplify a node tree back down into plain Javascript objects.

Details

JSON5 adds features to JSON that make it more enjoyable to use JSON for data and script files that are edited by humans.

The JSON5.parse method is great when you just want to read or write JSON5 with only basic syntax error checking. However, this is not ideal when using JSON5 as a storage format for more complicated data. This is because the line, type and offset information generated during the parsing of the JSON5 file is lost which means you cannot give helpful error messages to your users. Basically, JSON5.parse only provides a check for valid JSON5, no for valid application content.

This library adds an additional parsing mode to help when doing application level validation of JSON5. Use it by making calling JSON5.parse("...", {wantNodes: true}). This will generate an object with nodes instead of just plain Javascript object.

Each node object consists of the following properties:

{
  type: "object|array|null|string|numeric|boolean|null",
  value: [...]|{...}|"..."|true|false|999|null,
  line: 999,
  column: 999,
}

In addition, this library provides a JSON5.simplify() method. If you pass this method a node tree, it will simplify it back into a plain Javascript object, stripping out the node information. Any nodes in the node tree that do not have type and value properties will be copied over verbatim, including their child properties.

Examples

Let's say you use the JSON5 format to store an array of items for you application. For your application data to be valid, the items in the array must all be strings and must all be in ascending order. You read in a valid JSON5 file into an object and start going through the array. You find an entry that is out of order. Unfortunately, with the basic JSON5.parse method you can no longer tell the user what line the error occurs on because you do not have that information. But with the file parsed into nodes, you can now display a line and column information to your users so that they can find and fix the problem.

Here is an example of the node treed for the following simple JSON5 document:

{
  // A comment
  a: 1,
  b: {
    c: 2,
    d: 3
  },
  e:[1, 2]
}

Calling JSON5.parse(..., { wantNodes: true }) on this generates:

{
  "type": "object",
  "value": {
    "a": {
      "type": "numeric",
      "value": 1,
      "line": 3,
      "column": 6
    },
    "b": {
      "type": "object",
      "value": {
        "c": {
          "type": "numeric",
          "value": 2,
          "line": 5,
          "column": 8
        },
        "d": {
          "type": "numeric",
          "value": 3,
          "line": 6,
          "column": 8
        }
      },
      "line": 4,
      "column": 6
    },
    "e": {
      "type": "array",
      "value": [
        {
          "type": "numeric",
          "value": 1,
          "line": 8,
          "column": 6
        },
        {
          "type": "numeric",
          "value": 2,
          "line": 8,
          "column": 9
        }
      ],
      "line": 8,
      "column": 5
    }
  },
  "line": 1,
  "column": 1
}

As you can see, this is quite a bit more verbose, but accessing the data is really just a matter of using the .value properties. For example, if you put the results of the parse() call in a variable called obj, then instead of obj.a.c to get the value 2, you would use obj.value.a.value.c.

Calling JSON5.simplify on the above node tree will return an object matching the original plain Javascript object.

Future Things

  • The nodes should include the JSON5 comments so that they can be manipulated. Would need to differentiate end-of-line comments vs. full line comments probably.
  • If we support pulling comments into nodes, then it will could be possible to "round-trip" the JSON5 to a node tree and back again preserving comments. We would need a special version of stringify to do this.