HanSON is JSON with multiline strings and comments. Includes parser and converter.
- HanSON is JSON with comments, multi-line strings and unquoted property names.
- Supports backticks as quotes (``) for multi-line strings.
- You can use either double-quotes ("") or single-quotes ('') for single-line strings.
- Commas after the last list element or property will be ignored.
- Every JSON string is valid HanSON.
- HanSON can easily be converted to real JSON.
HanSON is an extension of JSON that fixes those shortcomings with four simple additions to the JSON spec:
- you can alternatively use backticks, as in ES6's template string literal, as quotes for strings. A backtick-quoted string may span several lines and you are not required to escape regular quote characters, only backticks. Backslashes still need to be escaped, and all other backslash-escape sequences work like in regular JSON.
- for single-line strings, single quotes ('') are supported in addition to double quotes ("")
- Commas after the last list element or object property will be ignored.
listName: "Sesame Street Monsters" // note that listName needs no quotescontent:name: "Cookie Monster"/* Note the template quotes and unescaped regular quotes in the next string */background: `Cookie Monster used to be amonster that ate everything, especially cookies.These days he is forced to eat "healthy" food.`name: "Herry Monster"background: `Herry Monster is a furry blue monster with a purple nose.He's mostly retired today.`// don't worry, the trailing comma will be ignored
hanson is a command-line converter that will convert HanSON files to JSON. It is a Nodes.js package that can be installed using npm:
npm install -g hanson
After installation, convert a single file like this:
hanson input.hson output.json
You can also convert multiple files using the -m options. It will automatically change the file extension to .json:
hanson -m input1.hson input2.hson input3.hson input4.hson input5.hson
hanson.js is a simple library for Node.js that provides you with a HanSON object which works pretty much like the JSON object, with the only difference being that hanson.parse() will accept HanSON.
var hanson = require'hanson';var obj = hansonparsehansonSrc;
hanson.stringify() will currently write regular JSON and just invokes JSON.stringify(), but future versions may pretty-print the output and use triple-quotes for multi-line strings instead of '\n'.
There's also a toJSON() function that can convert your HanSON source into JSON:
var hanson = require'hanson';var json = hansonparsehansonSrc;
- If you have configuration or descriptor files (like package.json), you can write them as HanSON and convert them with the command line tool or the Grunt task.
- You can, of course, extend your application to accept HanSON files.
Want to use HanSON in your program, without including any libraries? Use this function to convert HanSON to JSON. It returns a JSON string that can be read using JSON.parse().
var UNESCAPE_MAP = '\\"': '"' "\\`": "`" "\\'": "'" ;var ML_ESCAPE_MAP = '\n': '\\n' "\r": '\\r' "\t": '\\t' '"': '\\"';return UNESCAPE_MAPr || r;return inputreplace/`*`|'*'|"*"|\/\*[^]*?\*\/|\/\/.*\n?/g // pass 1: remove commentsif scharAt0 == '/'return '';elsereturn s;replace/||``|''|"*"|/g // pass 2: requoteif lonelyCommareturn '';else if identifier != nullreturn '"' + identifier + '"';else if multilineQuote != nullreturn '"' + multilineQuotereplace/\\./g unescapeQuotesreplace/[\n\r\t"]/g return ML_ESCAPE_MAPr; + '"';else if singleQuote != nullreturn '"' + singleQuotereplace/\\./g unescapeQuotesreplace/"/g '\\"' + '"';elsereturn s;;
- August 14, 2013: First release (0.1.0)
- August 15, 2013: Replaced triple-quotes with backticks (1.0.0, backward-incompatible change)
- August 19, 2013: Added support for single-quotes (1.1.0)
- August 19, 2013: Fixed removing commas followed by comments, support \r and \t in multiline quotes (1.1.1)
All code and documentation has been dedicated to the public domain: http://unlicense.org/