Class: JsonKit::Helper
- Inherits:
-
Object
- Object
- JsonKit::Helper
- Defined in:
- lib/json_kit/helper.rb
Instance Method Summary collapse
- #from_json(json, klass = nil, transforms = []) ⇒ Object
-
#initialize ⇒ Helper
constructor
A new instance of Helper.
- #to_json(obj) ⇒ Object
Constructor Details
#initialize ⇒ Helper
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
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/json_kit/helper.rb', line 22 def from_json(json, klass = nil, transforms = []) obj = JSON.parse(json) if [String, Numeric, Date, DateTime, Time, Integer, TrueClass, FalseClass].include?(obj.class) return json end if obj.is_a?(Array) return obj.map do |i| if i.is_a?(Hash) if klass != nil @hash_helper.from_hash(i, klass, transforms) else i.is_a?(Hash) @hash_helper.symbolize(i) end else i end end else if klass != nil return @hash_helper.from_hash(obj, klass, transforms) else return @hash_helper.symbolize(obj) end end end |
#to_json(obj) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/json_kit/helper.rb', line 8 def to_json(obj) if [String, Numeric, Date, DateTime, Time, Integer, TrueClass, FalseClass].include?(obj.class) return obj elsif obj.is_a?(Hash) return JSON.dump(obj) elsif obj.is_a?(Array) array = convert_array(obj) return JSON.dump(array) else hash = @hash_helper.to_hash(obj) return JSON.dump(hash) end end |