hjson vs json5 vs toml vs yaml
Data Serialization Formats
hjsonjson5tomlyamlSimilar Packages:

Data Serialization Formats

Data serialization formats are essential in web development for configuration files, data interchange, and APIs. They allow developers to represent complex data structures in a human-readable format, making it easier to manage and manipulate data. Each of these formats has unique features that cater to different needs, from simplicity and readability to support for complex data types. Choosing the right format can significantly impact the ease of development, maintenance, and performance of applications.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
hjson0432-275 years agoMIT
json507,106235 kB40-MIT
toml0318124 kB1715 days agoMIT
yaml01,650685 kB3125 days agoISC

Feature Comparison: hjson vs json5 vs toml vs yaml

Readability

  • hjson:

    HJSON is designed for human readability, allowing comments and a more relaxed syntax. This makes it easier for users to understand and edit configuration files without worrying about strict formatting rules.

  • json5:

    JSON5 improves upon JSON by allowing comments and more flexible syntax, making it easier for developers to document their configurations directly within the files, enhancing readability without sacrificing structure.

  • toml:

    TOML emphasizes simplicity and clarity, using a clear key-value pair structure that is easy to read and write. Its design is focused on being straightforward, making it ideal for configuration files.

  • yaml:

    YAML is known for its readability, using indentation to represent structure, which makes it visually appealing and easy to understand. It supports complex data structures while remaining accessible.

Syntax Flexibility

  • hjson:

    HJSON allows for a more lenient syntax compared to JSON, enabling features like unquoted strings and trailing commas. This flexibility reduces the likelihood of syntax errors, especially for non-technical users.

  • json5:

    JSON5 extends JSON by allowing trailing commas, unquoted keys, and multi-line strings, providing a more forgiving syntax that can accommodate different coding styles and preferences.

  • toml:

    TOML uses a straightforward syntax that is easy to parse, with support for various data types like arrays and tables. It maintains a balance between simplicity and expressiveness, making it versatile for configuration needs.

  • yaml:

    YAML's syntax is highly flexible, allowing for both block and inline styles, as well as the use of anchors and references to reduce redundancy. This flexibility is beneficial for complex configurations.

Data Structure Support

  • hjson:

    HJSON supports basic data structures like objects and arrays, making it suitable for simple configurations. It does not support complex types as extensively as other formats.

  • json5:

    JSON5 supports all standard JSON data types and adds flexibility with comments and trailing commas, making it a good choice for configurations that require a mix of data types.

  • toml:

    TOML is designed for hierarchical data structures, supporting tables, arrays, and nested tables, which makes it ideal for configuration files that require organization and clarity.

  • yaml:

    YAML excels in representing complex data structures, including nested dictionaries and lists, making it suitable for configurations that need to capture intricate relationships between data.

Use Cases

  • hjson:

    HJSON is particularly useful for configuration files where human readability is a priority, such as in projects where non-developers may need to edit settings.

  • json5:

    JSON5 is a great choice for JavaScript applications that require configuration files, as it maintains compatibility with JSON while providing additional flexibility.

  • toml:

    TOML is widely used in configuration files for applications, especially in the Rust ecosystem, due to its simplicity and clear structure, making it easy to manage settings.

  • yaml:

    YAML is commonly used in configuration files for applications and services, such as Docker and Kubernetes, due to its ability to represent complex data structures and its readability.

Community and Ecosystem

  • hjson:

    HJSON has a smaller community compared to others but is appreciated for its simplicity and ease of use, especially in specific use cases like configuration files.

  • json5:

    JSON5 benefits from a supportive community and is often used in JavaScript projects, making it a popular choice among developers familiar with JSON.

  • toml:

    TOML has gained traction in the Rust community and is increasingly adopted in other programming environments, supported by a growing ecosystem of libraries and tools.

  • yaml:

    YAML has a large and active community, with extensive support across various programming languages and frameworks, making it a well-established choice for configuration and data serialization.

How to Choose: hjson vs json5 vs toml vs yaml

  • hjson:

    Choose HJSON if you need a more human-friendly JSON format that allows comments and is less strict about syntax, making it easier for non-developers to read and write configuration files.

  • json5:

    Select JSON5 if you want to extend JSON with additional features like comments, trailing commas, and more flexible syntax, while still maintaining a close relationship with standard JSON.

  • toml:

    Opt for TOML if you prefer a clear and simple configuration format that is easy to read and write, especially for hierarchical data, making it suitable for configuration files in applications.

  • yaml:

    Use YAML if you require a highly readable format that supports complex data structures, including lists and dictionaries, and if you need features like anchors and references to avoid duplication.

README for hjson

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