Skip to content

taichi-ishitani/rb_json5

Repository files navigation

Gem Version CI Maintainability codecov Inline docs

RbJSON5

JSON5 parser for Ruby

Installation

Add this line to your application's Gemfile:

gem 'rb_json5'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install rb_json5

Usage

Use RbJSON5.parse method to parse JSON5 string. You can convert property names into Symbol by setting symbolize_names optional argument to true.

require 'rb_json5'

# https://github.com/json5/json5#short-example
json5 = <<~'JSON5'
{
   // comments
   unquoted: 'and you can quote me on that',
   singleQuotes: 'I can use "double quotes" here',
   lineBreaks: "Look, Mom! \
 No \\n's!",
   hexadecimal: 0xdecaf,
   leadingDecimalPoint: .8675309, andTrailing: 8675309.,
   positiveSign: +1,
   trailingComma: 'in objects', andIn: ['arrays',],
   "backwardsCompatible": "with JSON",
 }
JSON5

RbJSON5.parse(json5)
  # =>
  # {
  #   "unquoted"=>"and you can quote me on that",
  #   "singleQuotes"=>"I can use \"double quotes\" here",
  #   "lineBreaks"=>"Look, Mom! No \\n's!",
  #   "hexadecimal"=>912559,
  #   "leadingDecimalPoint"=>0.8675309,
  #   "andTrailing"=>8675309.0,
  #   "positiveSign"=>1,
  #   "trailingComma"=>"in objects",
  #   "andIn"=>["arrays"],
  #   "backwardsCompatible"=>"with JSON"
  # }
RbJSON5.parse(json5, symbolize_names: true)
  # =>
  # {
  #   :unquoted=>"and you can quote me on that",
  #   :singleQuotes=>"I can use \"double quotes\" here",
  #   :lineBreaks=>"Look, Mom! No \\n's!",
  #   :hexadecimal=>912559,
  #   :leadingDecimalPoint=>0.8675309,
  #   :andTrailing=>8675309.0,
  #   :positiveSign=>1,
  #   :trailingComma=>"in objects",
  #   :andIn=>["arrays"],
  #   :backwardsCompatible=>"with JSON"
  # }

Contributing & Contact

Bug reports, feature requests, pull requests and questions are welcome! You can use following methods:

Note

Test-suite and JSON5 code snippets for RSpec examples are originaly from:

Copyright & License

Copyright © 2020 Taichi Ishitani. RbJSON5 is licensed under MIT License, see LICENSE for further details.

Code of Conduct

Everyone interacting in the RbJson5 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.