HanSON is JSON with multiline strings and comments. Includes parser and converter.
HanSON is an extension of JSON that fixes those shortcomings with four simple additions to the JSON spec:
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;
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;;
All code and documentation has been dedicated to the public domain: http://unlicense.org/