GithubHelp home page GithubHelp logo

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

laktak avatar namusyaka avatar nicolasleger avatar tereci avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

hjson-rb's Issues

hjson-rb does not work with ruby 3

[1] pry(main)> require 'hjson'
=> true
[2] pry(main)> Hjson.parse "1"
ArgumentError: wrong number of arguments (given 2, expected 1)
from /home/app/.rvm/gems/ruby-3.0.1/gems/hjson-0.1.2/lib/hjson/ast/parser.rb:41:in `initialize'
[3] pry(main)> RUBY_VERSION
=> "3.0.1"

I understand it is because of separation of positional and keyword arguments in Ruby 3.0. Sorry I don't know Ruby well to fix it and submit a pull request.

shadowing outer local variable

When I try to load hjson I get the following warning:

/var/lib/gems/2.3.0/gems/hjson-0.1.0/lib/hjson/ast/nodes/string.rb:48: warning: shadowing outer local variable - uffff

Here's the entire ruby script:

#!/usr/bin/ruby -w
require 'hjson'

Yes, my script turns on warnings. Still, it seems like a warning worth heeding.

Here are some specs:

Operating system Ubuntu Linux 16.04.1
Kernel and CPU Linux 4.4.0-78-generic on x86_64
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]

Please let me know if there is other information that would help.

hjson-rb produces deprecation warning on Ruby 2.4 related to usage of Fixnum

Ruby 2.4 deprecated Fixnum and Bignum in favor of Integer. It seems that hjson-rb needs to replace references to Fixnum with a reference to Integer. After simple replacement of the Fixnum string the library works without warnings and parses the hjson files correctly (it passes all tests as well), but I'm not sure about backward compatibility.

Example:

$ ruby Compiler.rb colors.hjson -o colors.dat
/home/antek/.gem/ruby/2.4.0/gems/hjson-0.1.0/lib/hjson/ast/parser.rb:56: warning: constant ::Fixnum is deprecated

My Ruby version:

$ ruby --version
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

Related links:

  1. https://bugs.ruby-lang.org/issues/12005
  2. http://blog.bigbinary.com/2016/11/18/ruby-2-4-unifies-fixnum-and-bignum-into-integer.html

Relax dependency requirement on JSON

Hi,
I am trying to migrate from JSON to HJSON for config files of my app and I am running into an issue with JSON gem compatibility as HJSON gem requires JSON ~> 2.2.0 while some of the other gems I am using are locked down on older versions of the JSON gem.

Would it be possible to relax the current requirement to a less strict that allows for older versions? I tried to build this gem locally with:
spec.add_runtime_dependency 'json', '>= 1.7.5'
and it seems to be working just fine. I can create a pull request if that would help.

Gem out of date

Rubygems has the initial version which was last updated August 07, 2016 and does not contain any of the patches since. Can you please bump the version and submit it?

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.