GithubHelp home page GithubHelp logo

isabella232 / hjson-rb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hjson/hjson-rb

0.0 0.0 0.0 31 KB

Hjson for Ruby

Home Page: https://hjson.github.io/

License: MIT License

Ruby 100.00%

hjson-rb's Introduction

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 = <<HJSON
// for your config
// use #, // or /**/ comments,
// omit quotes for keys
key: 1
// omit quotes for strings
string: contains everything until LF
// omit commas at the end of a line
cool: {
  foo: 1
  bar: 2
}
// allow trailing commas
list: [
  1,
  2,
]
// and use multiline strings
realist:
  '''
  My half empty glass,
  I will fill your empty half.
  Now you are half full.
  '''
HJSON

Hjson.parse(hjson)

Comments

Hjson allows you to use comments in your JSON.

require 'hjson'

hjson =<<HJSON
{
  # specify rate in requests/second
  "rate": 1000

  // prefer c-style comments?
  /* feeling old fashioned? */
}
HJSON

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

Quotes

You don't need to quote keyname.

require 'hjson'

hjson =<<HJSON
{
  key: "value"
}
HJSON

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

Commas

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

require 'hjson'

hjson =<<HJSON
{
  one: 1
  two: 2
  three: 4 # oops
}
HJSON

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

Quoteless

Hjson makes quotes for strings optional as well.

require 'hjson'

hjson =<<HJSON
{
  text: look ma, no quotes!

  # To make your life easy, put the next
  # value or comment on a new line.
  # It's also easier to read!
}
HJSON

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

Escapes

You don't need to escape in unquoted strings.

require 'hjson'

hjson = <<HJSON
{
  # write a regex without escaping the escape
  regex: ^\d*\.{0,1}\d+$

  # quotes in the content need no escapes
  inject: <div class="important"></div>

  # inside quotes, escapes work
  # just like in JSON
  escape: "\\\\ \n \t\\""
}
HJSON

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 =<<HJSON
{
  haiku:
    '''
    JSON I love you.
    But strangled is my data.
    This, so much better.
    '''
}
HJSON

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 =<<HJSON
// this is a valid config file
joke: My backslash escaped!
HJSON

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.

hjson-rb's People

Contributors

namusyaka avatar laktak avatar nicolasleger avatar tereci avatar

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.