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

complex_object = Jsonable.new
puts JSON.parse complex_object.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 complex_object.to_tidy_json
# {
# "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