Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

hjson

hjson1.1mMIT3.2.2TypeScript support: definitely-typed

A user interface for JSON.

json, comments, config, hjson, parser, serializer, human

readme

hjson-js

Build Status NPM version License

Hjson, a user interface for JSON

Hjson Intro

JSON is easy for humans to read and write... in theory. In practice JSON gives us plenty of opportunities to make mistakes without even realizing it.

Hjson is a syntax extension to JSON. It's NOT a proposal to replace JSON or to incorporate it into the JSON spec itself. It's intended to be used like a user interface for humans, to read and edit before passing the JSON data to the machine.

{
  # specify rate in requests/second (because comments are helpful!)
  rate: 1000

  // prefer c-style comments?
  /* feeling old fashioned? */

  # did you notice that rate doesn't need quotes?
  hey: look ma, no quotes for strings either!

  # best of all
  notice: []
  anything: ?

  # yes, commas are optional!
}

The JavaScript implementation of Hjson is based on JSON-js. For other platforms see hjson.github.io.

Install from npm

npm install hjson

Usage

var Hjson = require('hjson');

var obj = Hjson.parse(hjsonText);
var text2 = Hjson.stringify(obj);

To keep comments intact see API.

From the Commandline

Install with npm install hjson -g.

Usage:
  hjson [OPTIONS]
  hjson [OPTIONS] INPUT
  hjson (-h | --help | -?)
  hjson (-V | --version)

INPUT can be in JSON or Hjson format. If no file is given it will read from stdin.
The default is to output as Hjson.

Options:
  (-j | -json)  output as formatted JSON.
  (-c | -json=compact)  output as JSON.
Options for Hjson output:
  -sl         output the opening brace on the same line
  -quote      quote all strings
  -quote=all  quote keys as well
  -js         output in JavaScript/JSON compatible format
              can be used with -rt and // comments
  -rt         round trip comments
  -nocol      disable colors
  -cond=n     set condense option (default 60, 0 to disable)

Domain specific formats are optional extensions to Hjson and can be enabled with the following options:
  +math: support for Inf/inf, -Inf/-inf, Nan/naN and -0
  +hex: parse hexadecimal numbers prefixed with 0x
  +date: support ISO dates

Sample:

  • run hjson -j test.hjson > test.json to convert to JSON
  • run hjson test.json > test.hjson to convert to Hjson
  • run hjson test.json to view colorized output

API

The API is the same for the browser and node.js version.

NOTE that the DSF api is considered experimental

Hjson.parse(text, options)

This method parses JSON or Hjson text to produce an object or array.

  • text: the string to parse as JSON or Hjson
  • options: object
    • keepWsc: boolean, keep white space and comments. This is useful if you want to edit an hjson file and save it while preserving comments (default false)

Hjson.stringify(value, options)

This method produces Hjson text from a JavaScript value.

  • value: any JavaScript value, usually an object or array.
  • options: object
    • keepWsc: boolean, keep white space. See parse.
    • condense: integer, will try to fit objects/arrays onto one line. Default 0 (off).
    • bracesSameLine: boolean, makes braces appear on the same line as the key name. Default false.
    • emitRootBraces: boolean, show braces for the root object. Default true.
    • quotes: string, controls how strings are displayed. (setting separator implies "strings")
      • "min": no quotes whenever possible (default)
      • "keys": use quotes around keys
      • "strings": use quotes around string values
      • "all": use quotes around keys and string values
    • multiline: string, controls how multiline strings are displayed. (setting quotes implies "off")
      • "std": strings containing \n are shown in multiline format (default)
      • "no-tabs": like std but disallow tabs
      • "off": show in JSON format
    • separator: boolean, output a comma separator between elements. Default false
    • space: specifies the indentation of nested structures. If it is a number, it will specify the number of spaces to indent at each level. If it is a string (such as '\t' or ' '), it contains the characters used to indent at each level.
    • eol: specifies the EOL sequence (default is set by Hjson.setEndOfLine())
    • colors: boolean, output ascii color codes
    • serializeDeterministically: boolean, when serializing objects into hjson, order the keys based on their UTF-16 code units order. Default false.

Hjson.endOfLine(), .setEndOfLine(eol)

Gets or sets the stringify EOL sequence ('\n' or '\r\n'). When running with node.js this defaults to os.EOL.

Hjson.rt { parse, stringify }

This is a shortcut to roundtrip your comments when reading and updating a config file. It is the same as specifying the keepWsc option for the parse and stringify functions.

Hjson.version

The version number.

require-hook

Require a config file directly.

require("hjson/lib/require-config");
var cfg=require("./config.hjson");

modify & keep comments

You can modify a Hjson file and keep the whitespace & comments intact (round trip). This is useful if an app updates its config file.

// parse, keep whitespace and comments
// (they are stored in a non enumerable __COMMENTS__ member)
var data = Hjson.rt.parse(text);

// modify like you normally would
data.foo = "text";

// convert back to Hjson
console.log(Hjson.rt.stringify(data));

Build

To run all tests and create the bundle output, first install the dev dependencies with npm i and then run npm run build.

History

see history.md

changelog

hjson-js History

  • v3.2.1
    • fix stringifying with comments
  • v3.2.0
    • add sortProps option to deterministically sort object properties
  • v3.1.1
    • set exit code for cli tool
  • v3.1.0
    • add condense mode for stringify
  • v3.0.2
    • fix stringify for keys containing single quotes
  • v3.0.1
    • add option to turn off legacy support for omitting root braces
  • v3.0.0
    • add support for single quoted strings
  • v2.4.3
    • fix throw error on string containing newline
  • v2.4.2
    • add error hint for incorrect single quotes
  • v2.4.1
    • fix tty colors
  • v2.4.0
    • new stringify options:
      • quotes for keys and strings (SamVerschueren)
      • comma separator (SamVerschueren)
      • multiline strings output (hmalphettes)
  • v2.3.1
    • add comments api (merge/extract)
  • v2.3.0
    • improved comment round trip
  • v2.2.0
    • stringify will always emit root braces
  • v2.1.0
    • add DSF (domain specific formats), experimental
    • refactor, use browserify for output (see bundle)
    • fix emit root braces by default (was off)
  • v2.0.8 (not released)
  • v2.0.7
    • added stringify color option (CLI)
  • v2.0.6
    • show better messages for parsing errors on a root object without braces
    • include error hint when a missing closing } or ] is part of a string
  • v2.0.5
    • fix stringify for strings staring with a punctuator char
  • v2.0.4
    • move to hjson org
  • v2.0.3
    • fix stringify regression
    • add test
  • v2.0.2
    • remove obsoletes
  • v2.0.1
    • simpler stringify
  • v2.0.0
    • add stricter check for {}[],: at the start of a quoteless string
  • v1.8.4
    • fix multiline stringify
  • v1.8.3
    • fix stringify for key names containing comments/control characters
  • v1.8.2
    • better parse for single JSON values
  • v1.8.1
    • detect EOF when looking for a key name
  • v1.7.6
    • fix trailing whitespace in keyname
  • v1.7.4
    • fix trailing space in quoteless strings
    • fix default braces
  • v1.7.3
    • fix root check
    • better error messages
  • v1.7.2
    • fixed stringify, see hjson/hjson#29
    • optional root braces, see hjson/hjson#28
  • v1.6.3
    • added stringify options
  • v1.6.1
    • fixed stringify with options===null
  • v1.6.0
    • Added rt (roundtrip) shortcut.
  • v1.5.0
    • Added support for the simplified syntax for keys. Previously only alphanumeric keys were allowed without quotes.
    • Fixed multiline strings: OS/file independent (EOL is always \n). Also the last LF is removed.
  • v1.4.0
    • Changed the browser interface to match the node api (which didn't change).
    • Fixed parse for leading zeros ("00") and trailing comments.
    • Fixed stringify for /**/ and //
    • Added more test cases.
  • v1.3.0
    • Added support for the simplified syntax.
  • v1.2.0
    • Added old fashioned /**/ comments.
    • Fixed the missing EOL (cli only).
  • v1.1.0
    • add // support
  • v1.0.2
    • stringify bug fixes
  • v1.0.0
    • Switched to v1 for semver.
    • Adds editing support via the { keepWsc: true } option.
    • Removes stringify(value, replacer, space) replacer support
    • You can still use this syntax but replacer will no longer be called. This was removed in favor of editing support and because replacer provided an incomplete solution.