tidy_json
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', f: ['I', 'II', 'III', ['i.', 'ii.', 'iii.', { 'ichi': "\u{4e00}", 'ni': "\u{4e8c}", 'san': "\u{4e09}", 'yon': "\u{56db}" }]], c: {}, b: 'dos', e: [[]] }
@b = { z: { iv: 4, ii: 'duos', iii: 3, i: 'one' }, b: ['two', 3, '<abbr title="four">IV</abbr>'], a: 1, g: [{ none: [] }], f: %w[x y z] }
end
end
my_jsonable = Jsonable.new
# => #<Jsonable:0x0055b2aa0ff660 @a={:a=>"uno", :f=>["I", "II", "III", ["i.", "ii.", "iii.", {:ichi=>"一", :ni=>"二", :san=>"三", :yon=>"四"}]], :c=>{}, :b=>"dos", :e=>[[]]}, @b={:z=>{:iv=>4, :ii=>"duos", :iii=>3, :i=>"one"}, :b=>["two", 3, "<abbr title=\"four\">IV</abbr>"], :a=>1, :g=>[{:none=>[]}], :f=>["x", "y", "z"]}>
JSON.parse my_jsonable.stringify
# => {"class"=>"Jsonable", "a"=>{"a"=>"uno", "f"=>["I", "II", "III", ["i.", "ii.", "iii.", {"ichi"=>"一", "ni"=>"二", "san"=>"三", "yon"=>"四"}]], "c"=>{}, "b"=>"dos", "e"=>[[]]}, "b"=>{"z"=>{"iv"=>4, "ii"=>"duos", "iii"=>3, "i"=>"one"}, "b"=>["two", 3, "<abbr title=\"four\">IV</abbr>"], "a"=>1, "g"=>[{"none"=>[]}], "f"=>["x", "y", "z"]}}
puts my_jsonable.to_tidy_json(indent: 4, sort: true)
# {
# "a": {
# "a": "uno",
# "b": "dos",
# "c": {},
# "e": [
# []
# ],
# "f": [
# "I",
# "II",
# "III",
# [
# "i.",
# "ii.",
# "iii.",
# {
# "ichi": "一",
# "ni": "二",
# "san": "三",
# "yon": "四"
# }
# ]
# ]
# },
# "b": {
# "a": 1,
# "b": [
# "two",
# 3,
# "<abbr title=\"four\">IV</abbr>"
# ],
# "f": [
# "x",
# "y",
# "z"
# ],
# "g": [
# {
# "none": []
# }
# ],
# "z": {
# "i": "one",
# "ii": "duos",
# "iii": 3,
# "iv": 4
# }
# },
# "class": "Jsonable"
# }
# => nil
Dependencies
Runtime
- json ~> 2.2