Hjson, the Human JSON written in Ruby

Build Status gem License

A configuration file format for humans. Relaxed syntax, fewer mistakes, more comments for Ruby.

Installation

Add this line to your application's Gemfile:

gem 'hjson'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hjson

Usage

You can use Hjson as JSON.parse of standard library.

Please check it out.

require 'hjson'

hjson = "// for your config\n// use #, // or /**/ comments,\n// omit quotes for keys\nkey: 1\n// omit quotes for strings\nstring: contains everything until LF\n// omit commas at the end of a line\ncool: {\n  foo: 1\n  bar: 2\n}\n// allow trailing commas\nlist: [\n  1,\n  2,\n]\n// and use multiline strings\nrealist:\n  '''\n  My half empty glass,\n  I will fill your empty half.\n  Now you are half full.\n  '''\n"

Hjson.parse(hjson)

Comments

Hjson allows you to use comments in your JSON.

require 'hjson'

hjson ="{\n  # specify rate in requests/second\n  \"rate\": 1000\n\n  // prefer c-style comments?\n  /* feeling old fashioned? */\n}\n"

Hjson.parse(hjson) #=> {"rate"=>1000}

Quotes

You don't need to quote keyname.

require 'hjson'

hjson ="{\n  key: \"value\"\n}\n"

Hjson.parse(hjson) #=> {"key"=>"value"}

Commas

You can forget the comma at the end, Hjson recognizes the end automatically.

require 'hjson'

hjson ="{\n  one: 1\n  two: 2\n  three: 4 # oops\n}\n"

Hjson.parse(hjson) #=> {"one"=>1, "two"=>2, "three"=>4}

Quoteless

Hjson makes quotes for strings optional as well.

require 'hjson'

hjson ="{\n  text: look ma, no quotes!\n\n  # To make your life easy, put the next\n  # value or comment on a new line.\n  # It's also easier to read!\n}\n"

Hjson.parse(hjson) #=> {"text"=>"look ma, no quotes!"}

Escapes

You don't need to escape in unquoted strings.

require 'hjson'

hjson = "{\n  # write a regex without escaping the escape\n  regex: ^\\d*\\.{0,1}\\d+$\n\n  # quotes in the content need no escapes\n  inject: <div class=\"important\"></div>\n\n  # inside quotes, escapes work\n  # just like in JSON\n  escape: \"\\\\\\\\ \\n \\t\\\\\"\"\n}\n"

Hjson.parse(hjson)
#=> {"regex"=>"^d*.{0,1}d+$",
 "inject"=>"<div class=\"important\"></div>",
 "escape"=>"\\ \n \t\""}

Multiline

Hjson allows you to use ''' for writing multiline strings.

require 'hjson'

hjson ="{\n  haiku:\n    '''\n    JSON I love you.\n    But strangled is my data.\n    This, so much better.\n    '''\n}\n"

Hjson.parse(hjson) #=> {"haiku"=>"JSON I love you.\nBut strangled is my data.\nThis, so much better."}

Braces

You can omit the braces for the root object.

require 'hjson'

hjson ="// this is a valid config file\njoke: My backslash escaped!\n"

Hjson.parse(hjson) #=> {"joke"=>"My backslash escaped!"}

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/namusyaka/hjson. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.