Module: Representable::JSON

Extended by:
Hash::ClassMethods
Includes:
Hash
Included in:
Collection
Defined in:
lib/representable/json.rb,
lib/representable/json/hash.rb,
lib/representable/json/collection.rb

Overview

Brings #to_json and #from_json to your object.

Defined Under Namespace

Modules: ClassMethods, Collection, Hash

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hash::ClassMethods

collection_representer_class

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/representable/json.rb', line 11

def self.included(base)
  base.class_eval do
    include Representable # either in Hero or HeroRepresentation.
    extend ClassMethods # DISCUSS: do that only for classes?
    register_feature Representable::JSON
  end
end

Instance Method Details

#from_json(data, *args) ⇒ Object

Parses the body as JSON and delegates to #from_hash.



28
29
30
31
# File 'lib/representable/json.rb', line 28

def from_json(data, *args)
  data = MultiJson.load(data)
  from_hash(data, *args)
end

#to_json(*args) ⇒ Object

Returns a JSON string representing this object.



34
35
36
# File 'lib/representable/json.rb', line 34

def to_json(*args)
  MultiJson.dump to_hash(*args)
end