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

neojson

v0.0.1

Published

JSON with new expressions

Downloads

1

Readme

neoJSON

A data exchange & config format, a subset of ES2017, fully supporting JSON and JSON5 expressions, with added support for new expresssions.

// person.neojson.js
import { Person } from ".";
export default new Person({
  name: "John Doe",
  birthDate: new Date("1990-12-27"),
  roles: new Set(["admin", "moderator"]),
});

Why?

  • You can statically typecheck your config files with TypeScript/Flow (by inferring the constructors' argument types).
  • You can serialize and deserialize your own class instances.
  • You can use Dates, RegExps, Sets and Maps.

Definition

Take JSON. Add comments, single quotes, unquoted keys, trailing commas and escaped newlines. That's roughly JSON5. Add new expressions, wrap it in an export default statement, and add the necessary import statements. That's neoJSON.

In a neoJSON file, the entire expression must be wrapped in export default:

export default {
  some: "JSON value",
};

You can use new expressions for some global classes:

export default {
  date: new Date("2022-12-27"),
  regexp: new RegExp("\\s+"),
  set: new Set(["admin", "moderator"]),
  map: new Map([[1, 2]]),
};

and you can use your own classes:

import MyClass from "./my-class.js";
import { MyPoint as Point } from "./my-point.js";
export default {
  x: new MyClass("hi"),
  y: new Point({ x: 1, y: 2 }),
};

Usage

import { parse, stringify } from "neojson";

class Person {
  constructor({ name, age }) {
    this.name = name;
    this.age = age;
  }
  toNeoJSON() {
    const { name, age } = this;
    return [{ name, age }]; // the constructor arguments
  }
}

// imports per file path
const imports = { ".": { Person } };

const person = new Person({ name: "Joe", age: 42 });
const str = stringify(person, { imports });
console.log(str);
console.log(parse(str, { imports }));

The generated neoJSON string is:

import { Person } from ".";
export default new Person({ name: "Joe", age: 42 });

API

parse(str, options)

Parses a neoJSON string and returns the value. options is an object with the following properties:

  • imports (optional): an object mapping file paths to objects containing the constructors to use for new expressions. The default is {}.

stringify(value, options)

Stringifies a value to a neoJSON string. options is an object with the following properties:

  • imports (optional): an object mapping file paths to objects containing the constructors to use for new expressions. The default is {}.

Is it safe?

As long as your constructors are safe and you don't provide the Function constructor to parse, yes. The parser does not use eval, it uses Babel's parser.