HJSON:構成を追加可能なJSON拡張

3618 ワード

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 times is 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
    .
  • 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 #hjson in 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
    
    
      org.hjson
      hjson
      1.0.0
    

    (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.