Proposition hjson
Human JSON (Hjson) is a configuration file format based on the JavaScript Object Notation [RFC7159]. Its focus is to provide a reasonable alternative to plain JSON configs that are hard to edit and do not allow comments.
Copyright (c) 2016 IETF Trust and the persons identified as the document authors. All rights reserved.
Contents
1. Introduction
Human JSON (Hjson) is a configuration file format. It is a superset of the JavaScript Object Notation [RFC7159].
Hjson uses the same four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays) as JSON.
A string is a sequence of zero or more Unicode characters [UNICODE].
Note that this citation references the latest version of Unicode rather than a specific release. It is not expected that future changes in the UNICODE specification will impact the syntax of Hjson.
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
An array is an ordered sequence of zero or more values.
The terms "object" and "array" come from the conventions of JavaScript.
Hjson's design goals were
- to improve readability and editability over JSON while keeping the familiar syntax intact
- to maintain backward compatibility, so that any valid JSON is valid Hjson
- to provide a reasonable alternative to plain JSON configs Hjson SHOULD be used for data whose primary purpose is to be viewed or edited by a human. For example configuration, resource files or debug data dumps.
Hjson SHOULD NOT be used in other cases, like transfer protocols.
1.1. Syntax compared to JSON
Hjson is a superset of JSON. Its syntax allows you to
- add #, // or /**/ comments,
- omit quotes for keys,
- omit quotes for strings (terminated by LF, no escapes),
- omit braces for the root object,
- omit the comma at the end of a line
- add trailing commas and
- use multiline strings with proper whitespace handling.
Because the punctuator characters "{}[],:" are used to define the structure of the Hjson text, you need to use quotes
- if your key includes a punctuator or space
- if your string starts with a punctuator For examples see Section 14.1.
1.2. Conventions Used in This Document
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119].
The grammatical rules in this document are to be interpreted as described in [RFC5234] except where they are specifically marked as [SABNF].
SABNF is used to express grammars that are not possible in ABNF, like the block-comment, and to define terminators, like ql-end, that allow us to exclude certain sequences. These constructs allow us to generate parsers and to verify the grammar against test cases. For the general understanding of the language they can be ignored.
1.3. Specifications of Hjson
This document describes Hjson and registers the media type "application/hjson".
2. Hjson Grammar
A Hjson text is a sequence of tokens. The set of tokens includes seven structural characters, comments, strings, numbers, and three literal names.
A Hjson text is either a serialized value or a root object.
Hjson-text = ws-c ( root-object / value ) ws-c These are the seven structural characters:
begin-array = %x5B ; [ left square bracket begin-object = %x7B ; { left curly bracket end-array = %x5D ; ] right square bracket end-object = %x7D ; } right curly bracket name-separator = %x3A ; : colon comma-separator = %x2C ; , comma lf-separator = lf ; Line feed or New line
Insignificant whitespace and comments are allowed before or after any of the first six structural characters.
ws-c = *( comment / ws ) ws = *( space / tab / lf / cr ) space = %x20 ; Space tab = %x09 ; Horizontal tab lf = %x0A ; Line feed or New line cr = %x0D ; Carriage return
Comments can be specified as line or block comments.
comment = line-comment / block-comment line-comment = ( %x23 / ; # hash %x2F.2F ) ; // slash + slash
- ( tab / cr / %x20-10FFFF ) ; until lf block-comment = start-block-comment
- ( !end-block-comment anychar ) end-block-comment
start-block-comment = %x2F.2A ; /* slash + star end-block-comment = %x2A.2F ; */ star + slash anychar = tab / cr / lf / %x20-10FFFF
- SABNF
- block-comment requies the use of the operator to allow * and / in the text while stopping at */
3. Values
A Hjson value MUST be an object, array, number, or string, or one of the following three literal names:
false = %x66.61.6C.73.65 ; false null = %x6E.75.6C.6C ; null true = %x74.72.75.65 ; true
The literal names MUST be lowercase. No other literal names are allowed.
value = literal / object / array / number / string literal = ( false / null / true ) !literal-end literal-end = *( space / tab ) ( %x21-22 / %x24-2B / %x2D-2E / %x30-5A / %x5C / %x5E-7A / %x7C / %x7E-10FFFF ) ; exclude #/,[]{} ; SABNF: define literal-end to prevent matches ; that are actually a ql-string ; (like "true blue")
4. Value Separators
Values can be separated directly by a comma or indirectly by one or more linefeeds.
value-separator = ( ws-c comma-separator ws-c ) / ( *( comment / *(space / tab / cr) ) lf-separator ws-c )
Note that while Hjson allows the use of lf as a separator, the cr character is generally ignored. This should not be an issue as all modern operating systems use either lf or cr+lf as their line terminator.
5. Objects
An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A single colon comes after each name, separating the name from the value. A comma separates a value from the next name unless the value is followed by a linefeed, in which case the comma is optional.
Trailing separators are allowed. The names within an object SHOULD be unique.
object = begin-object ws-c [ member *( value-separator member ) [value-separator] ] ws-c end-object member = name ws-c name-separator ws-c value
If the Hjson text defines an object it does not have to include the
braces at the root level:
root-object = member *( value-separator member ) [value-separator]
A name can be specified as a JSON string (with quotes) or as a name without quotes.
name = json-string / keyname keyname = 1*non-punctuator-char non-punctuator-char = %x21-2B / %x2D-39 / %x3B-5A / %x5C / %x5E-7A / %x7C / %x7E-10FFFF ; any non-punctuator character (excludes ,:[]{} and ws)
For interoperability issues regarding the uniquness of names and object member ordering, see [RFC7159] Section 4.
6. Arrays
An array structure is represented as square brackets surrounding zero or more values (or elements). Elements are separated by commas or linefeeds. Trailing commas are allowed.
array = begin-array ws-c [ value *( value-separator value ) [value-separator ] ] ws-c end-array
There is no requirement that the values in an array be of the same
type.
7. Numbers
The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed.
A fraction part is a decimal point followed by one or more digits.
An exponent part begins with the letter E in upper or lower case, which may be followed by a plus or minus sign. The E and optional sign are followed by one or more digits.
Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.
number = [ minus ] int [ frac ] [ exp ] !num-end decimal-point = %x2E ; .
digit = %x30-39 ; 0-9 digit1-9 = %x31-39 ; 1-9 e = %x65 / %x45 ; e E exp = e [ minus / plus ] 1*digit frac = decimal-point 1*digit int = zero / ( digit1-9 *digit ) minus = %x2D ; - plus = %x2B ; + zero = %x30 ; 0 num-end = *( space / tab ) ( %x21-22 / %x24-2B / %x2D-2E / %x30-5A / %x5C / %x5E-7A / %x7C / %x7E-10FFFF ) ; exclude #/,[]{} ; SABNF: define num-end to prevent matches ; that are actually a ql-string ; (like "1 minute")
For limits on the range and precision of numbers see [RFC7159] Section 6.
8. Strings
Hjson allows strings to be represented in three different formats.
string = json-string / ml-string / ql-string
8.1. JSON Strings
The representation of JSON strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped:
quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).
Any character may be escaped. If the character is in the Basic Multilingual Plane (U+0000 through U+FFFF), then it may be represented as a six-character sequence: a reverse solidus, followed by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point. The hexadecimal letters A though F can be upper or lower case. So, for example, a string containing only a single reverse solidus character may be represented as "\u005C".
Alternatively, there are two-character sequence escape representations of some popular characters. So, for example, a string containing only a single reverse solidus character may be represented more compactly as "\".
To escape an extended character that is not in the Basic Multilingual Plane, the character is represented as a 12-character sequence, encoding the UTF-16 surrogate pair. So, for example, a string containing only the G clef character (U+1D11E) may be represented as "\uD834\uDD1E".
json-string = quotation-mark *char quotation-mark char = unescaped / escape ( %x22 / ; " quotation mark U+0022 %x5C / ; \ reverse solidus U+005C %x2F / ; / solidus U+002F %x62 / ; b backspace U+0008 %x66 / ; f form feed U+000C %x6E / ; n line feed U+000A %x72 / ; r carriage return U+000D %x74 / ; t tab U+0009 %x75 4hexdig ) ; uXXXX U+XXXX hexdig = %x30-39 / "A" / "B" / "C" / "D" / "E" / "F" escape = %x5C ; \ quotation-mark = %x22 ; " unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
8.2. Quoteless Strings
Hjson allows strings to be represented only by their content without quotes. A quoteless string may contain any character except the control characters (U+0000 through U+001F), excluding trailing whitespace. It cannot start with a punctuator character (,:[]{}).
It ends at the first linefeed. Strings are taken as is without escapes.
ql-string = non-punctuator-char *( !ql-end ql-char ) ql-end = *( space / tab / cr ) lf ql-char = space / tab / %x21-10FFFF ; SABNF: define ql-end to exclude trailing ; whitespace from the ql-string
A Hjson parser must still detect values (_number_, "true", "false" or "null") and parse them correctly. For example
- '3' is the number 3
- '5 times' is the string "5 times"
- 'true' is the boolean true
- '7 # minutes' is the number 7 followed by a comment
- '\s#([0-9a-fA-F]{3})' is the string "\\s#([0-9a-fA-F]{3})" Not allowing escapes inside the quoteless string allows you to specify HTML fragments, regular expressions or Windows style paths as-is, keeping them readable.
8.3. Multiline Strings
A multiline string begins and ends with three single quotes.
ml-string = ml-seq *( !ml-seq ( ml-char / ml-ignore ) ) ml-seq ml-seq = "" ml-char = space / tab / lf / %x21-10FFFF ml-ignore = cr ; SABNF: ml-string requies the use of the ; ! operator to allow ' in the text ; while stopping at
The carrige return character is generally ignored to make the parser platform independent.
Whitespace handling cannot be expressed in the ABNF grammar and is thus defined as:
- Whitespace on the first line is ignored.
- The first three single quotes define the head. On the following lines all whitespace up to the column of the first single quote is ignored.
- All other whitespace is assumed to be part of the string.
- The last newline is ignored to allow for better formatting.
9. String and Character Issues
9.1. Character Encoding
Hjson text SHALL be encoded in UTF-8. Since Hjson parsers can read JSON they may be able to read texts in other encodings (such as UTF-16 and UTF-32). Hjson texts that are encoded in UTF-8 are interoperable in the sense that they will be read successfully by the maximum number of implementations.
Implementations MUST NOT add a byte order mark to the beginning of a Hjson text. In the interests of interoperability, implementations that parse Hjson texts MAY ignore the presence of a byte order mark rather than treating it as an error.
9.2. Unicode Characters
See [RFC7159] Section 8.2.
9.3. String Comparison
See [RFC7159] Section 8.3.
10. Parsers
A Hjson parser transforms a Hjson text into another representation.
A Hjson parser MUST accept all texts that conform to the Hjson grammar. A Hjson parser MAY accept non-Hjson forms or extensions.
An implementation may set limits on the size of texts that it accepts. An implementation may set limits on the maximum depth of nesting. An implementation may set limits on the range and precision of numbers. An implementation may set limits on the length and character contents of strings.
11. Generators
A Hjson generator produces Hjson text. The resulting text MUST strictly conform to the Hjson grammar.
12. IANA Considerations
The MIME media type for Hjson text is application/hjson.
- Type name: application
- Subtype name: hjson
- Required parameters: n/a
- Optional parameters: n/a
- Encoding considerations: binary
- Security considerations: See XXXX, Section 13.
- Interoperability considerations: Described in XXXX
- Published specification: XXXX
- Applications that use this media type: Hjson has been used to provide configuration data for applications written in all of these programming languages: C#, Go, Java, JavaScript, PHP and Python
- Additional information: Magic number(s): n/a File extension(s):
- .hjson Macintosh file type code(s): TEXT
- Person & email address to contact for further information: IESG
iesg@ietf.org [1]
- Intended usage: COMMON
- Restrictions on usage: none
- Author: Christian Zangl laktak@cdak.net [2]
- Change controller: IESG iesg@ietf.org [3]
- Note: No "charset" parameter is defined for this registration.
Adding one really has no effect on compliant recipients.
13. Security Considerations
Unlike JSON, Hjson is not a subset of Javascript and cannot be parsed using "eval()".
14. Examples
This is a Hjson object:
# comments are useful # specify rate in requests/second rate: 1000 # you may also use // c style /* or block comments */ # key names do not need to be placed in # quotes unless they contain a punctuator # character {}[],: key: 1 # strings may also omit quotes if they do # not start with a punctuator text: look ma, no quotes! # quoteless strings do not use escapes # and end at the LF/newline # commas are optional commas: { one: 1 two: 2 } # trailing commas are allowed trailing: { one: 1, two: 2, } # multiline string haiku: ''' JSON I love you. But you strangle my expression. This is so much better. ''' # Hjson is a superset of JSON so you # may use any valid JSON syntax: favNumbers: [ 1, 2, 3, 6, 42 ]
14.1. JSON vs Hjson
Example of a document processor configuration, first in JSON:
{ "header": "The Foo Manual\nCopyright Bar Inc.", "source": { "include": [ "./src" ], "includePattern": ".+\\.foo(doc)?$", "excludePattern": "(^|\\/|\\\\)_" }, "templates": { "cleverLinks": false, "monospaceLinks": false } }
and in Hjson:
# define the header using a multiline string header: ''' The Foo Manual Copyright Bar Inc. ''' source: { # generate documentation for these paths include: [ ./src ] # include/exclude files # notice that no escapes are required includePattern: .+\.foo(doc)?$ excludePattern: (^|\/|\\)_ } templates: { # trailing commas are allowed cleverLinks: false, monospaceLinks: false, }
Example of a npm dependency configuration in JSON:
{ "dependencies": { "foo": "2.0.1", "bar": "*", "til": "~1.2.1", "elf": "^1.2.3" } }
and in Hjson:
{ dependencies: { // match version exactly foo: 2.0.1 // * matches any version bar: * // approximately, allows patch-level changes til: ~1.2.1 // compatible with major version elf: ^1.2.3 } }
15. Contributors
This document is based on [RFC4627] and [RFC7159] which were written by Douglas Crockford and Tim Bray. This document was constructed by extending it with the elements of the Hjson syntax.
16. References
16.1. Normative References
[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, DOI 10.17487/RFC2119, March 1997, <http://www.rfc-editor.org/info/rfc2119>.
[RFC4627] Crockford, D., "The application/json Media Type for JavaScript Object Notation (JSON)", RFC 4627, DOI 10.17487/RFC4627, July 2006, <http://www.rfc-editor.org/info/rfc4627>.
[RFC5234] Crocker, D., Ed. and P. Overell, "Augmented BNF for Syntax
Specifications: ABNF", STD 68, RFC 5234,
DOI 10.17487/RFC5234, January 2008,
<http://www.rfc-editor.org/info/rfc5234>.
[RFC7159] Bray, T., Ed., "The JavaScript Object Notation (JSON) Data Interchange Format", RFC 7159, DOI 10.17487/RFC7159, March 2014, <http://www.rfc-editor.org/info/rfc7159>.
[SABNF] Thomas, L., "Superset Augmented Backus-Naur Form", version 2.2.1, n.d., <https://github.com/ldthomas/apg- js2/blob/2fa136e00e5932d70ed1bd4786101c87afa8ca2e/ SABNF.md>.
[UNICODE] The Unicode Consortium, "The Unicode Standard", n.d., <http://www.unicode.org/versions/latest/>.