In Files

YALTools::ProcJson

Description

YALTools::ProcJson module is a collection of methods related to the JSON processing.

In the future, each method will be migrated into corresponding class method.

Public Class Methods

exclude_value_from_json(json, labels) click to toggle source

Please see the following example.

exclude_value_from_json({“_id”=>“x”, “_rev”=>“y”, “name”=>“z”}, [“_rev”]) #=> {“_id”=>“x”,“name”=>“z”}

    # File yalt/procjson.rb, line 29
29:     def exclude_value_from_json(json, labels)
30:       ret = {}
31:       json.each do |k,v|
32:         ret[k] = v
33:         ret.delete(k) if labels.index(k.to_s) != nil
34:       end
35:       return ret
36:     end
grep_json(json, pattern_list, regexp_flag, ignore_case_flag) click to toggle source

returns true or false.

It is a wrapper method against the grep_json_p method. This method handles the multiple patterns as pattern_list.

     # File yalt/procjson.rb, line 110
110:     def grep_json(json, pattern_list, regexp_flag, ignore_case_flag)
111:       ret = false
112:       ret_flags = Array.new(pattern_list.size).map do |i| false ; end
113:       pattern_list.each_index do |i|
114:         ret_flags[i] = grep_json_p(json.clone, pattern_list[i].clone, regexp_flag, ignore_case_flag)
115:       end
116:       ret = true if ret_flags.index(false) == nil
117:       return ret
118:     end
grep_json_p(json, pattern, regexp_flag, ignore_case_flag) click to toggle source

returns true or false. If the json contains the pattern sequence, then it returns true.

Examples

  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2"], false, false) #=> true
  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","K3","value"], false, false) #=> true

  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","k3"], false, false) #=> false

  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","k3"], true, false) #=> true
  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","k3"], false, true) #=> true

  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","^K"], true, true) #=> true
     # File yalt/procjson.rb, line 53
 53:     def grep_json_p(json, pattern, regexp_flag, ignore_case_flag)
 54:       ret = false
 55:       go_next = true
 56:       rest_json = {}
 57:       pat  = (pattern != nil) ? pattern.shift : nil
 58:       ks = json.kind_of?(Hash) ? json.keys : [json]  ## [json] means the leaf of json tree.
 59: 
 60:       ## check the pattern
 61:       case regexp_flag
 62:       when true
 63:         ks.each do |k|
 64:           case ignore_case_flag
 65:           when true
 66:             if k =~ /#{pat}/
 67:               ret = true
 68:               rest_json = json[k]
 69:             end
 70:           when false
 71:             if k =~ /#{pat}/
 72:               ret = true 
 73:               rest_json = json[k]
 74:             end
 75:           end
 76:         end
 77:       when false
 78:         case ignore_case_flag
 79:         when true
 80:           ks.each do |k|
 81:             if k.upcase == pat.upcase
 82:               ret = true
 83:               rest_json = json[k]
 84:             end
 85:           end
 86:         when false
 87:           if ks.index(pat) != nil
 88:             ret = true
 89:             rest_json = json[pat]
 90:           end
 91:         end
 92:       end
 93:       
 94:       return ret if pattern == []
 95: 
 96:       if json.kind_of?(Hash)
 97:         ret = grep_json_p(rest_json, pattern, regexp_flag, ignore_case_flag)
 98:       else
 99:         ret = false
100:       end
101:       return ret
102:     end
select_value_from_json(json, labels) click to toggle source

Please see the following example.

select_value_from_json({“_id”=>“x”, “_rev”=>“y”, “name”=>“z”}, [“_id”,“name”]) #=> {“_id”=>“x”,“name”=>“z”}

    # File yalt/procjson.rb, line 17
17:     def select_value_from_json(json, labels)
18:       ret = {}
19:       labels.each do |label|
20:         ret[label] = json[label] if json.has_key?(label)
21:       end
22:       ret
23:     end

Private Instance Methods

exclude_value_from_json(json, labels) click to toggle source

Please see the following example.

exclude_value_from_json({“_id”=>“x”, “_rev”=>“y”, “name”=>“z”}, [“_rev”]) #=> {“_id”=>“x”,“name”=>“z”}

    # File yalt/procjson.rb, line 29
29:     def exclude_value_from_json(json, labels)
30:       ret = {}
31:       json.each do |k,v|
32:         ret[k] = v
33:         ret.delete(k) if labels.index(k.to_s) != nil
34:       end
35:       return ret
36:     end
grep_json(json, pattern_list, regexp_flag, ignore_case_flag) click to toggle source

returns true or false.

It is a wrapper method against the grep_json_p method. This method handles the multiple patterns as pattern_list.

     # File yalt/procjson.rb, line 110
110:     def grep_json(json, pattern_list, regexp_flag, ignore_case_flag)
111:       ret = false
112:       ret_flags = Array.new(pattern_list.size).map do |i| false ; end
113:       pattern_list.each_index do |i|
114:         ret_flags[i] = grep_json_p(json.clone, pattern_list[i].clone, regexp_flag, ignore_case_flag)
115:       end
116:       ret = true if ret_flags.index(false) == nil
117:       return ret
118:     end
grep_json_p(json, pattern, regexp_flag, ignore_case_flag) click to toggle source

returns true or false. If the json contains the pattern sequence, then it returns true.

Examples

  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2"], false, false) #=> true
  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","K3","value"], false, false) #=> true

  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","k3"], false, false) #=> false

  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","k3"], true, false) #=> true
  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","k3"], false, true) #=> true

  grep_json_p({"k1"=>{"k2"=>{"K3"=>"value"}}}, ["k1","k2","^K"], true, true) #=> true
     # File yalt/procjson.rb, line 53
 53:     def grep_json_p(json, pattern, regexp_flag, ignore_case_flag)
 54:       ret = false
 55:       go_next = true
 56:       rest_json = {}
 57:       pat  = (pattern != nil) ? pattern.shift : nil
 58:       ks = json.kind_of?(Hash) ? json.keys : [json]  ## [json] means the leaf of json tree.
 59: 
 60:       ## check the pattern
 61:       case regexp_flag
 62:       when true
 63:         ks.each do |k|
 64:           case ignore_case_flag
 65:           when true
 66:             if k =~ /#{pat}/
 67:               ret = true
 68:               rest_json = json[k]
 69:             end
 70:           when false
 71:             if k =~ /#{pat}/
 72:               ret = true 
 73:               rest_json = json[k]
 74:             end
 75:           end
 76:         end
 77:       when false
 78:         case ignore_case_flag
 79:         when true
 80:           ks.each do |k|
 81:             if k.upcase == pat.upcase
 82:               ret = true
 83:               rest_json = json[k]
 84:             end
 85:           end
 86:         when false
 87:           if ks.index(pat) != nil
 88:             ret = true
 89:             rest_json = json[pat]
 90:           end
 91:         end
 92:       end
 93:       
 94:       return ret if pattern == []
 95: 
 96:       if json.kind_of?(Hash)
 97:         ret = grep_json_p(rest_json, pattern, regexp_flag, ignore_case_flag)
 98:       else
 99:         ret = false
100:       end
101:       return ret
102:     end
select_value_from_json(json, labels) click to toggle source

Please see the following example.

select_value_from_json({“_id”=>“x”, “_rev”=>“y”, “name”=>“z”}, [“_id”,“name”]) #=> {“_id”=>“x”,“name”=>“z”}

    # File yalt/procjson.rb, line 17
17:     def select_value_from_json(json, labels)
18:       ret = {}
19:       labels.each do |label|
20:         ret[label] = json[label] if json.has_key?(label)
21:       end
22:       ret
23:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.