JSON5 parser for Ruby
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
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"
# }
Bug reports, feature requests, pull requests and questions are welcome! You can use following methods:
Test-suite and JSON5 code snippets for RSpec examples are originaly from:
- https://github.com/json5/json5-tests
- https://github.com/json5/json5/blob/master/test/parse.js
- https://github.com/json5/json5/blob/master/test/errors.js
- https://github.com/json5/json5#short-example
Copyright © 2020 Taichi Ishitani. RbJSON5 is licensed under MIT License, see LICENSE for further details.
Everyone interacting in the RbJson5 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.