- Notifications
You must be signed in to change notification settings - Fork0
json-next gem - read generation y / next generation json versions (HanSON, SON, JSONX/JSON11, etc.) with comments, unquoted keys, multi-line strings, trailing commas, optional commas, and more
License
json-next/json-next
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
json-next gem - read generation y / next generation json versions (HanSON, SON, JSONX/JSON11, etc.) with comments, unquoted keys, multi-line strings, trailing commas, optional commas, and more
- home ::github.com/json-next/json-next
- bugs ::github.com/json-next/json-next/issues
- gem ::rubygems.org/gems/json-next
- rdoc ::rubydoc.info/gems/json-next
HanSON - JSON for Humans by Tim Jansen et al
HanSON is an extension of JSON with a few simple additions to the spec:
- quotes for strings are optional if they follow JavaScript identifier rules.
- 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 inregular JSON.
- for single-line strings, single quotes (
''
) are supported in addition to double quotes (""
) - you can use JavaScript comments, both single line (
//
) and multi-line comments (/* */
), in all places where JSON allows whitespace. - Commas after the last list element or object property will be ignored.
Example:
{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.`},{// You can single-quote strings too: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]}
UseHANSON.convert
to convert HanSON text to ye old' JSON text:
{"listName":"Sesame Street Monsters","content": [ {"name":"Cookie Monster","background":"Cookie Monster used to be a\n ... to eat\"healthy\" food." }, {"name":"Herry Monster","background":"Herry Monster is a furry blue monster with a purple nose.\n ... today." } ]}
UseHANSON.parse
instead ofJSON.parse
to parse text to ruby hash / array / etc.:
{"listName"=>"Sesame Street Monsters","content"=>[{"name"=>"Cookie Monster","background"=>"Cookie Monster used to be a\n ... to eat\"healthy\" food."},{"name"=>"Herry Monster","background"=>"Herry Monster is a furry blue monster with a purple nose.\n ... today."}]}
SON - Simple Object Notation by Aleksander Gurin et al
Simple data format similar to JSON, but with some minor changes:
- comments starts with
#
sign and ends with newline (\n
) - comma after an object key-value pair is optional
- comma after an array item is optional
JSON is compatible with SON in a sense thatJSON data is also SON data, but not vise versa.
Example:
{ # Personal information "name": "Alexander Grothendieck" "fields": "mathematics" "main_topics": [ "Etale cohomology" "Motives" "Topos theory" "Schemes" ] "numbers": [1 2 3 4] "mixed": [1.1 -2 true false null]}
UseSON.convert
to convert SON text to ye old' JSON text:
{"name":"Alexander Grothendieck","fields":"mathematics","main_topics": ["Etale cohomology","Motives","Topos theory","Schemes" ],"numbers": [1,2,3,4],"mixed": [1.1,-2,true,false,null]}
UseSON.parse
instead ofJSON.parse
to parse text to ruby hash / array / etc.:
{"name"=>"Alexander Grothendieck","fields"=>"mathematics","main_topics"=>["Etale cohomology","Motives","Topos theory","Schemes"],"numbers"=>[1,2,3,4],"mixed"=>[1.1, -2,true,false,nil]}
JSON with Extensions or JSON v1.1 (a.k.a. JSON11 or JSON XI or JSON II)
Includes all JSON extensions from HanSON:
- quotes for strings are optional if they follow JavaScript identifier rules.
- 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 inregular JSON.
- for single-line strings, single quotes (
''
) are supported in addition to double quotes (""
) - you can use JavaScript comments, both single line (
//
) and multi-line comments (/* */
), in all places where JSON allows whitespace. - Commas after the last list element or object property will be ignored.
Plus all JSON extensions from SON:
- comments starts with
#
sign and ends with newline (\n
) - comma after an object key-value pair is optional
- comma after an array item is optional
Plus some more extra JSON extensions:
- unquoted strings following the JavaScript identifier rules can use the dash (
-
) too e.g. allows common keys such ascore-js
,babel-preset-es2015
,eslint-config-jquery
and others
Example:
{ # use shell-like (or ruby-like) comments listName: "Sesame Street Monsters" # note: comments after key-value pairs are optional content: [ { 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.` }, { // You can single-quote strings too: 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 */ ]}
UseJSONX.convert
(orJSONXI.convert
orJSON11.convert
orJSONII.convert
) to convert JSONX text to ye old' JSON text:
{"listName":"Sesame Street Monsters","content": [ {"name":"Cookie Monster","background":"Cookie Monster used to be a\n ... to eat\"healthy\" food." }, {"name":"Herry Monster","background":"Herry Monster is a furry blue monster with a purple nose.\n ... today." } ]}
UseJSONX.parse
(orJSONXI.parse
orJSON11.parse
orJSONII.parse
) instead ofJSON.parse
to parse text to ruby hash / array / etc.:
{"listName"=>"Sesame Street Monsters","content"=>[{"name"=>"Cookie Monster","background"=>"Cookie Monster used to be a\n ... to eat\"healthy\" food."},{"name"=>"Herry Monster","background"=>"Herry Monster is a furry blue monster with a purple nose.\n ... today."}]}
require'json/next'text1=<<TXT{ listName: "Sesame Street Monsters", // note that listName needs no quotes content: [ { 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.` }, { // You can single-quote strings too: 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 ]}TXTppHANSON.parse(text1)# note: is the same as JSON.parse( HANSON.convert( text ))
resulting in:
{"listName"=>"Sesame Street Monsters","content"=>[{"name"=>"Cookie Monster","background"=>"Cookie Monster used to be a\n ... to eat\"healthy\" food."},{"name"=>"Herry Monster","background"=>"Herry Monster is a furry blue monster with a purple nose.\n ... today."}]}
and
text2=<<TXT{ # Personal information "name": "Alexander Grothendieck" "fields": "mathematics" "main_topics": [ "Etale cohomology" "Motives" "Topos theory" "Schemes" ] "numbers": [1 2 3 4] "mixed": [1.1 -2 true false null]}TXTppSON.parse(text2)# note: is the same as JSON.parse( SON.convert( text ))
resulting in:
{"name"=>"Alexander Grothendieck","fields"=>"mathematics","main_topics"=>["Etale cohomology","Motives","Topos theory","Schemes"],"numbers"=>[1,2,3,4],"mixed"=>[1.1, -2,true,false,nil]}
and
text3=<<TXT{ # use shell-like (or ruby-like) comments listName: "Sesame Street Monsters" # note: comments after key-value pairs are optional content: [ { 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.` }, { // You can single-quote strings too: 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 */ ]}TXTppJSONX.parse(text3)# note: is the same as JSON.parse( JSONX.convert( text ))ppJSONXI.parse(text3)# note: is the same as JSON.parse( JSONXI.convert( text ))ppJSON11.parse(text3)# note: is the same as JSON.parse( JSON11.convert( text ))ppJSONII.parse(text3)# note: is the same as JSON.parse( JSONII.convert( text ))
resulting in:
{"listName"=>"Sesame Street Monsters","content"=>[{"name"=>"Cookie Monster","background"=>"Cookie Monster used to be a\n ... to eat\"healthy\" food."},{"name"=>"Herry Monster","background"=>"Herry Monster is a furry blue monster with a purple nose.\n ... today."}]}
See theAwesome JSON (What's Next?) collection / page.
Thejson-next
scripts are dedicated to the public domain.Use it as you please with no restrictions whatsoever.
Post them to thewwwmake forum. Thanks!
About
json-next gem - read generation y / next generation json versions (HanSON, SON, JSONX/JSON11, etc.) with comments, unquoted keys, multi-line strings, trailing commas, optional commas, and more