Class: JSONTrim

Inherits:
Object
  • Object
show all
Defined in:
lib/jsontrim.rb

Overview

takes a JSON object, removes specified subtrees and trims arrays returns a pretty printed string

before = <<-EOF
{ "foo": {
    "one": { "a": 1, "b": {"c": 2} },
    "two": "value",
    "three": { "b": [1,2,3] }},
  "bar": [1,2,3,4,5] }
EOF

blacklist = [
  "foo:!two",   # delete the value before['foo']['two']
  "foo:*:!b",   # delete all the keys called "b" nested two levels under "foo"
  "bar:+"       # only keep the first element of the foo-list
]

after = JSONTrim.cut(before, :ignore => blacklist)

 {
   "foo": {
     "two": ...,
     "three": {
       "b": [ ... ]
     },
     "one": {
       "a": 1,
       "b": { ... }
     }
   },
   "bar": [
     1,
     ...
   ]
 }

Constant Summary collapse

VERSION =
"0.1.4"

Class Method Summary collapse

Class Method Details

.cut(obj, opts = {}) ⇒ Object

prune the given JSON-string or ruby Hash according to the rules given in opts



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jsontrim.rb', line 45

def self.cut(obj, opts = {})
  obj = JSON.parse(obj) if String === obj

  (opts[:ignore] || []).each do |ignore_str|
    ignoring = ignore_str.split(":")

    obj = cutr(obj, ignoring)
  end

  js = JSON.pretty_generate(obj)
  js = js.gsub('"IGN_HSH"', "{ ... }")
  js = js.gsub('"IGN_ARY"', "[ ... ]")
  js = js.gsub('"IGN"', "...")
end

.cutr(data, rules) ⇒ Object

recursively prune the current ruby object with the given rules replace pruned elements with marker strings

cutr( [1,2,3],         ["+"] )       #=> [ 1, "IGN" ]
cutr( {"a"=>{"b"=>3}}, ["!a"] )      #=> { "a" => "IGN_HSH" }
cutr( {"a"=>{"b"=>3}}, ["a", "!b"])  #=> { "a" => { "b" => "IGN" }}


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jsontrim.rb', line 67

def self.cutr(data, rules)
  key, *rest = rules

  if key =~ /^!(.*)/
    name = $1

    data[name] = case data[name]
                   when Hash then "IGN_HSH"
                   when Array then "IGN_ARY"
                   else "IGN"
                 end if data[name]
  elsif key =~ /^\+$/
    if Array === data
      data = [data.first, "IGN"]
    end
  elsif key =~ /^\*$/
    if Array === data
      data = data.map { |elem| cutr(elem, rest) }
    elsif Hash === data
      data = data.inject({}) do |h,(k,v)| h[k] = cutr(v, rest); h end
    end
  else
    name = key

    if data[name] and not rest.empty?
      data[name] = cutr(data[name], rest)
    end
  end

  data
end