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
-
#from_json(data, *args) ⇒ Object
(also: #parse)
Parses the body as JSON and delegates to #from_hash.
-
#to_json(*args) ⇒ Object
(also: #render)
Returns a JSON string representing this object.
Methods included from Hash::ClassMethods
collection_representer_class, format_engine
Class Method Details
.generate(object) ⇒ Object
8 9 10 |
# File 'lib/representable/json.rb', line 8 def self.generate(object) ::JSON.dump(object) end |
.included(base) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/representable/json.rb', line 23 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 |
.parse(data) ⇒ Object
12 13 14 15 16 |
# File 'lib/representable/json.rb', line 12 def self.parse(data) return if data.nil? || data.strip.empty? ::JSON.parse(data) end |
Instance Method Details
#from_json(data, *args) ⇒ Object Also known as: parse
Parses the body as JSON and delegates to #from_hash.
44 45 46 47 |
# File 'lib/representable/json.rb', line 44 def from_json(data, *args) data = Representable::JSON.parse(data) from_hash(data, *args) end |
#to_json(*args) ⇒ Object Also known as: render
Returns a JSON string representing this object.
50 51 52 |
# File 'lib/representable/json.rb', line 50 def to_json(*args) Representable::JSON.generate to_hash(*args) end |