class JsonParse

Constants

VERSION

Public Class Methods

new(val, rootkey = nil, throwmissing = false) click to toggle source
# File lib/json_parse.rb, line 80
def initialize(val, rootkey = nil, throwmissing = false) #, jsonstring = '{"root": {"name":"Josh Kramer", "age":60, "children": [{"name": "jack", "age": 21},{"name": "sally","age":14}],  "pets": ["ruby","buddy","fluffy"], "address": {"city" : "Bainbridge Island", "state" : "WA"}}}')
  @source = val
  if val.nil?
    @h = {}
    @rootkey = nil
    return
  end
  @throwmissing = throwmissing
  @rootkey = rootkey
  if val.is_a?(JsonParse)
    @h = val.hash
    @rootkey = val.rootkey

  end

  if val.is_a?(Array)
    retval = []
    @rootkey = rootkey
    val.each do |item|
      if item.is_a?(Hash) || item.is_a?(Array)
        retval.push(JsonParse.new(item))
      else
        retval.push(item);
      end

    end
    @h = retval
  end
  if val.is_a?(Hash)
    @h = val.symbolize_keys!
    _discard_root #
  end
  if val.is_a?(String) #if it sa string, it should be a json string
    _load(val)
    @jsonstring = val
  end

  if @h.nil?
    raise "an unusable object was provided to the initializer"
  end

end

Public Instance Methods

[](index) click to toggle source
# File lib/json_parse.rb, line 158
def [](index)
  if @h.is_a?(Array)
    return @h[index]
  end
  nil

end
_check_for_keyword(keyword) click to toggle source
# File lib/json_parse.rb, line 202
def _check_for_keyword(keyword)
  mykeys = []
  if @h.is_a?(Hash)
    mykeys = @h.keys
  end
  mykeys.include?(keyword.to_sym)
end
_parse_json(jsonString) click to toggle source

@return (Hash) @param [String] jsonString

# File lib/json_parse.rb, line 213
def _parse_json(jsonString)
  v = ActiveSupport::JSON.decode(jsonString)
end
_symbolize(h) click to toggle source
# File lib/json_parse.rb, line 186
def _symbolize(h)
  if self._check_for_keyword("_symbolize")
    return _value("_symbolize")
  end

  h.symbolize_keys!
  h.each do |k, v|
    if v.is_a?(Hash)
      self._symbolize(v)
    end
  end
end
_value(key) click to toggle source
# File lib/json_parse.rb, line 217
def _value(key)
  if @h.is_a?(Array)
    return @h[key]
  end
  if @h.key?(key.to_sym)
    if @h[key].is_a?(Hash) || @h[key].is_a?(Array)
      return JsonParse.new(@h[key], key)
    else
      retval = @h[key]

      return retval
    end
  end
end
add(key, value) click to toggle source
# File lib/json_parse.rb, line 76
def add(key, value)
  @h[key.to_sym] = value
end
contains_key(key = nil) click to toggle source
# File lib/json_parse.rb, line 148
def contains_key(key = nil)
  if key == nil && self._check_for_keyword("contains_key")
    return _value(:contains_key)
  end
  @h.keys.include?(key)
end
count() click to toggle source
# File lib/json_parse.rb, line 169
def count
  if self._check_for_keyword("hash")
    return _value(:hash)
  end
  _count
end
count!() click to toggle source
# File lib/json_parse.rb, line 166
def count!
  _count
end
each(&block) click to toggle source
# File lib/json_parse.rb, line 33
def each(&block)
  if self._check_for_keyword("each")
    return _value(:each)
  end
  _each(&block)
end
each!(&block) click to toggle source
# File lib/json_parse.rb, line 40
def each!(&block)
  _each(&block)
end
get_by_key(symbol) click to toggle source

@return [JsonParse] @param [Object] symbol

# File lib/json_parse.rb, line 29
def get_by_key(symbol)
  method_missing(symbol.to_sym, nil)
end
hash() click to toggle source
# File lib/json_parse.rb, line 179
def hash
  if self._check_for_keyword("hash")
    return _value(:hash)
  end
 _hash
end
hash!() click to toggle source
# File lib/json_parse.rb, line 176
def hash!
  _hash
end
json_string() click to toggle source
# File lib/json_parse.rb, line 135
def json_string
  if self._check_for_keyword("json_string")
    return _value(:json_string)
  end
  @h.to_json
end
json_string!() click to toggle source
# File lib/json_parse.rb, line 142
def json_string!
  _json_string
end
keys() click to toggle source
# File lib/json_parse.rb, line 59
def keys
  if self._check_for_keyword("keys")
    return self._value(:keys)
  end
  _keys
end
keys!() click to toggle source
# File lib/json_parse.rb, line 55
def keys!
  _keys
end
length() click to toggle source
# File lib/json_parse.rb, line 44
def length
  if self._check_for_keyword("length")
    return _value(:length)
  end
  @h.length
end
length!() click to toggle source
# File lib/json_parse.rb, line 51
def length!
  return _length
end
method_missing(m, *args) click to toggle source
Calls superclass method
# File lib/json_parse.rb, line 13
def method_missing(m, *args)

  #looks for top level keys and returns the value
  a = args
  if !(self.keys!.include?(m))
    if @throwmissing
      super
    else
      return nil
    end
  end
  self._value(m)
end
rootkey() click to toggle source
# File lib/json_parse.rb, line 124
def rootkey
  if self._check_for_keyword("rootkey")
    return _value(:rootkey)
  end
  _rootkey
end
rootkey!() click to toggle source
# File lib/json_parse.rb, line 130
def rootkey!
  _rootkey
end
source() click to toggle source
# File lib/json_parse.rb, line 69
def source
  if self._check_for_keyword("keys")
    return self._value(:keys)
  end
  _source
end
source!() click to toggle source
# File lib/json_parse.rb, line 66
def source!
  _source
end

Private Instance Methods

_count() click to toggle source
# File lib/json_parse.rb, line 267
def _count
  @h.length
end
_discard_root() click to toggle source
# File lib/json_parse.rb, line 288
def _discard_root()

  h2 = {}

  if @h.length == 1
    @rootkey = @h.keys[0]

    if @h.values[0].is_a?(Hash)
      @h.values[0].each do |k, v|
        h2[k] = v
      end
    end
  end
  if h2.length > 0
    @h = h2

  end
end
_each(&block) click to toggle source
# File lib/json_parse.rb, line 253
def _each(&block)
  if block_given?
    @h.each(&block)
  else
    return nil
  end
end
_hash() click to toggle source
# File lib/json_parse.rb, line 264
def _hash
  @h
end
_json_string() click to toggle source
# File lib/json_parse.rb, line 281
def _json_string
  @h.to_json
end
_keys() click to toggle source
# File lib/json_parse.rb, line 240
def _keys
  if @h.is_a?(Hash)
    return @h.keys
  end
  if @h.is_a?(Array)
    return "[]"
  else
    return nil if @h.nil?
    raise "expecting hash or array, got #{@h.class.name}"
  end

end
_length() click to toggle source
# File lib/json_parse.rb, line 235
def _length
  return @h.length
end
_load(jsonstring) click to toggle source
# File lib/json_parse.rb, line 271
def _load(jsonstring)
  if jsonstring.nil? && self._check_for_keyword("load")
    return _value(:load)
  end
  # var [Hash] @h
  @h = _parse_json(jsonstring)
  self._symbolize(@h)
  self._discard_root
end
_rootkey() click to toggle source
# File lib/json_parse.rb, line 284
def _rootkey
  @rootkey.to_s
end
_source() click to toggle source
# File lib/json_parse.rb, line 261
def _source
  @source
end