TidyJson

Build Status cci_build_status_badge Gem Version

A mixin providing (recursive) JSON serialization and pretty printing.

Installation

$ gem install tidy_json

Or, in your Gemfile:

source 'https://rubygems.org'
# ...
gem 'tidy_json'
# ...

Example

require 'tidy_json'

class Jsonable
  attr_reader :a, :b
  def initialize
    @a = { a: 'uno', b: 'dos', c: ['I', 'II', 'III', ['i.', 'ii.', 'iii.', { 'ichi': "\u{4e00}", 'ni': "\u{4e8c}", 'san': "\u{4e09}", 'yon': "\u{56db}" }]] }
    @b = { a: 1, b: ['two', 3, '<abbr title="four">IV</abbr>'] }
  end
end

my_jsonable = Jsonable.new

JSON.parse my_jsonable.stringify
# => {"class"=>"Jsonable", "a"=>{"a"=>"uno", "b"=>"dos", "c"=>["I", "II", "III", ["i.", "ii.", "iii.", {"ichi"=>"一", "ni"=>"二", "san"=>"三", "yon"=>"四"}]]}, "b"=>{"a"=>1, "b"=>["two", 3, "<abbr title=\"four\">IV</abbr>"]}}

puts my_jsonable.to_tidy_json(indent: 4)
# {
#     "class": "Jsonable",
#     "a": {
#        "a": "uno",
#        "b": "dos",
#        "c": [
#            "I",
#            "II",
#            "III",
#            [
#                "i.",
#                "ii.",
#                "iii.",
#                {
#                    "ichi": "一",
#                    "ni": "二",
#                    "san": "三",
#                    "yon": "四"
#                }
#            ]
#        ]
#    },
#    "b": {
#        "a": 1,
#        "b": [
#            "two",
#            3,
#            "<abbr title=\"four\">IV</abbr>"
#        ]
#    }
# }
# => nil

Dependencies

Runtime

Building

License

MIT