Class: JsonKit::Helper

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

Instance Method Summary collapse

Constructor Details

#initializeHelper

Returns a new instance of Helper.



4
5
6
# File 'lib/json_kit/helper.rb', line 4

def initialize
  @hash_helper = HashKit::Helper.new
end

Instance Method Details

#from_json(json, klass = nil, transforms = []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/json_kit/helper.rb', line 19

def from_json(json, klass = nil, transforms = [])

  hash = JSON.load(json)

  if klass != nil
    return @hash_helper.from_hash(hash, klass, transforms)
  else
    return @hash_helper.symbolize(hash)
  end

end

#to_json(obj) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/json_kit/helper.rb', line 8

def to_json(obj)
  if [String, Fixnum, Numeric, Date, DateTime, Time, Integer].include?(obj.class)
    return obj
  elsif obj.is_a?(Hash)
    return JSON.dump(obj)
  else
    hash = @hash_helper.to_hash(obj)
    return JSON.dump(hash)
  end
end