Git Product home page Git Product logo

json-next / json-next Goto Github PK

View Code? Open in Web Editor NEW
32.0 2.0 0.0 29 KB

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: Creative Commons Zero v1.0 Universal

Ruby 100.00%
hanson json json-next hson son jsonx jsonxi json11 json-extensions opendata

json-next's Introduction

json-next

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

Usage - HANSON.parse, SON.parse, JSONX.parse

HanSONSONJSONX

HanSON

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 in regular 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 quotes
  content: [
    {
      name: "Cookie Monster",
      /* Note the template quotes and unescaped regular quotes in the next string */
      background: `Cookie Monster used to be a
monster 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
   ]
}

Use HANSON.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."
    }
  ]
}

Use HANSON.parse instead of JSON.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

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 that JSON 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]
}

Use SON.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]
}

Use SON.parse instead of JSON.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]    
}

JSONX

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 in regular 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 as core-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 a
monster 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  */
   ]
}

Use JSONX.convert (or JSONXI.convert or JSON11.convert or JSONII.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."
    }
  ]
}

Use JSONX.parse (or JSONXI.parse or JSON11.parse or JSONII.parse) instead of JSON.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."
    }
  ]
}

Live Examples

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 a
monster 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
   ]
}
TXT

pp HANSON.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]
}
TXT

pp SON.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 a
monster 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  */
   ]
}
TXT

pp JSONX.parse( text3 )   # note: is the same as JSON.parse( JSONX.convert( text ))
pp JSONXI.parse( text3 )  # note: is the same as JSON.parse( JSONXI.convert( text ))
pp JSON11.parse( text3 )  # note: is the same as JSON.parse( JSON11.convert( text ))
pp JSONII.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."
    }
  ]
}

More JSON Formats

See the Awesome JSON (What's Next?) collection / page.

License

The json-next scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.

Questions? Comments?

Post them to the wwwmake forum. Thanks!

json-next's People

Contributors

geraldb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

json-next's Issues

warning: character class has duplicated range

Hi. I'd like to submit back a small modification to pattern.rb in the json/next gem.

When I was using JSONX.parse I kept getting this warning:

/var/lib/gems/2.3.0/gems/json-next-1.2.1/lib/json/next/parser/jsonx.rb: warning: character class has duplicated range
/var/lib/gems/2.3.0/gems/json-next-1.2.1/lib/json/next/parser/jsonx.rb:77: warning: character class has duplicated range

That warning is pointing out that in regular expressions, \w already covers _, so you don't need to have both in the pattern. Personally, it seems silly to make a whole warning about that - a little regex redundancy is hardly worth taking up anybody's time. But warnings can be annoying. I edited pattern.rb so that the three instances of this:

\\w_

are changed to just this:

\\w

That made the warning go away. Of course, I don't know if it will make other warnings or errors appear. But hopefully this can fix this small problem.

:-)
Miko

infinite loop when trying to convert JSONX with extra comma

I was parsing a JSON document that had an extra comma, and when trying to clean it up it produced an infinite loop and threw a format error.

input = " {\"foo\":\"bar\",,}"
JSONX.convert(input)

throws

!! format error: object literal - expected colon (:) - rest is >>}<<
!! format error: string literal - expected opening quote (") - rest is >>}<<
!! format error: object literal - expected colon (:) - rest is >>}<<
!! format error: string literal - expected opening quote (") - rest is >>}<<
!! format error: object literal - expected colon (:) - rest is >>}<<
!! format error: string literal - expected opening quote (") - rest is >>}<<
!! format error: object literal - expected colon (:) - rest is >>}<<
!! format error: string literal - expected opening quote (") - rest is >>}<<
!! format error: object literal - expected colon (:) - rest is >>}<<
!! format error: string literal - expected opening quote (") - rest is >>}<<
!! format error: object literal - expected colon (:) - rest is >>}<<
!! format error: string literal - expected opening quote (") - rest is >>}<<
!! format error: object literal - expected colon (:) - rest is >>}<<
!! format error: string literal - expected opening quote (") - rest is >>}<<
!! format error: object literal - expected colon (:) - rest is >>}<<
!! format error: string literal - expected opening quote (") - rest is >>}<<
!! format error: object literal - expected colon (:) - rest is >>}<<
!! format error: string literal - expected opening quote (") - rest is >>}<<```
endlessly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.