laktak / hjson
- суббота, 16 апреля 2016 г. в 03:11:33
HTML
Hjson, the Human JSON
A configuration file format for humans. Relaxed syntax, less mistakes, more comments.
{
# 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 ]
}
For details see hjson.org.
The Hjson syntax is a superset of JSON (see json.org) but allows you to
#
, //
or /**/
comments,""
for keys,""
for strings,,
at the end of a line andYou are also allowed to omit the root {}
braces for objects.
Simple rules to remember:
{}[],:
or space, use quotes{
or [
, use quotesKeys
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
'''
, whitespace on the first line is ignored'''
defines the head, on the following lines all whitespace up to this column is ignored'''
. 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 #hjson
in the first line.
Platform | Source | Package |
---|---|---|
JavaScript, Node.js & Browser | hjson-js | |
Java | hjson-java | |
Python | hjson-py | |
C#, .Net | hjson-cs | |
PHP | hjson-php |
Platform | Description | Source |
---|---|---|
Go | Parser and unmarshaller using a slightly different syntax | xson |
C | jzon variant, based on Hjson | jzon-c |
Please open an issue if you port Hjson to another platform/language.
Name | Source | Package |
---|---|---|
Atom | language-hjson | package |
Sublime Text / TextMate | sublime-hjson | packagecontrol.io |
Notepad++ | npp-hjson | see source |
Name | Link | Details |
---|---|---|
any-json: convert (almost) anything to JSON | see readme | |
gulp: the streaming build system | see readme | |
grunt: the JavaScript task runner | see readme | |
hjsonify: a browserify transform to require Hjson files | see readme | |
node-config: node.js application configuration | see wiki | |
nconf: hierarchical node.js configuration | nconf.file({ file: 'file.hjson', format: require('hjson').rt }); // round trips your comments |
|
rc: non-configurable configuration loader for lazy people. | var conf=require('rc')('appname', {/*defaults*/}, null, require('hjson').parse); |
All versions work on Linux, OS X & Windows and allow you to convert Hjson from/to JSON.
Install with npm i hjson -g
hjson file.json
will convert to Hjson.
hjson -j file.hjson
will convert to JSON.
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.
As Nuget does not install commandline tools
cli\bin\Release
to your path.hjsonc file.json
will convert to Hjson.
hjsonc -j file.hjson
will convert to JSON.
\n
). Also the last LF is removed to allow for better formatting.true
, false
or null
were not allowed.