module Representable::JSON

Brings to_json and from_json to your object.

Public Class Methods

included(base) click to toggle source
# File lib/representable/json.rb, line 16
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

Public Instance Methods

from_json(data, *args) click to toggle source

Parses the body as JSON and delegates to from_hash.

# File lib/representable/json.rb, line 37
def from_json(data, *args)
  data = MultiJson.load(data)
  from_hash(data, *args)
end
Also aliased as: parse
parse(data, *args)
Alias for: from_json
render(*args)
Alias for: to_json
to_json(*args) click to toggle source

Returns a JSON string representing this object.

# File lib/representable/json.rb, line 43
def to_json(*args)
  MultiJson.dump to_hash(*args)
end
Also aliased as: render