1

HJSON.md

HJSON为JSON文件添加可配置的配置语法,譬如评论、省略Key与字符串的双引号、省略末尾的逗号等等。


{
  # 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!

  # Obviously you can always use standard JSON syntax as well:
  favNumbers: [ 1, 2, 3, 6, 42 ]
}

Syntax

  • Keys

You only need to add quotes if the key name includes whitespace or any of these characters: {}[],:.

  • Strings

When you omit quotes the string ends at the newline. Preceding and trailing whitespace is ignored as are escapes.

A value that is a number, true, false or null in JSON is parsed as a value. E.g. 3 is a valid number while 3 timesis a string.

Naturally a quoteless string cannot start with { or [.

Use quotes to have your string handled like in JSON. This also allows you to specify a comment after the string.

  • Multiline Strings

  • Start with triple quotes ''', whitespace on the first line is ignored

  • ''' defines the head, on the following lines all whitespace up to this column is ignored

  • all other whitespace is assumed to be part of the string.

  • ends with triple quotes '''. The last newline is ignored to allow for better formatting.

A multiline string is OS and file independent. The line feed is always \n.

  • Commas

Commas are optional at the end of a line. You only need commas to specify multiple values on one line (e.g. [1,2,3]).

Trailing commas are ignored.

  • Comments

# and // start a single line comment.

/* starts a multiline comment that ends with */.

  • Root braces

Can be omitted for objects.

  • Mime Type

text/hjson (pending)

  • File extension

.hjson

  • Header

Hjson does not have a header but if you want to indicate the file type (in an rc file or in a database) you can use #hjsonin the first line.

Implementation

NodeJS

Install with npm i hjson -g

hjson file.json will convert to Hjson. hjson -j file.hjson will convert to JSON.

Java


<dependency>
  <groupId>org.hjson</groupId>
  <artifactId>hjson</artifactId>
  <version>1.0.0</version>
</dependency>

(1)Convert


// convert Hjson to JSON
String jsonString = JsonValue.readHjson(readerOrHjsonString).toString();

// convert JSON to Hjson
String hjsonString = JsonValue.readHjson(readerOrJSONString).toString(Stringify.HJSON);

(2)Read


JsonObject jsonObject = JsonValue.readHjson(string).asObject();
JsonArray jsonArray = JsonValue.readHjson(reader).asArray();

Python

Install with pip install hjson

python -m hjson.tool file.json will convert to Hjson. python -m hjson.tool -j file.hjson will convert to JSON.

.NET

As Nuget does not install commandline tools

  • please build from source

  • and add cli\bin\Release to your path.

hjsonc file.json will convert to Hjson. hjsonc -j file.hjson will convert to JSON.


王下邀月熊_Chevalier
22.4k 声望8.5k 粉丝

爱代码 爱生活 希望成为全栈整合师