Class: Delineate::Serializers::JsonSerializer
- Inherits:
-
MapSerializer
- Object
- MapSerializer
- Delineate::Serializers::JsonSerializer
- Defined in:
- lib/delineate/serializers/json_serializer.rb
Overview
AttributeMap serializer that handles JSON as the external data format.
Instance Method Summary collapse
-
#serialize(options = {}) ⇒ Object
Returns the record’s mapped attributes as a JSON string.
-
#serialize_in(json_string, options = {}) ⇒ Object
Takes a record’s attributes represented as a JSON string, and returns a hash suitable for direct assignment to the record’s collection of attributes.
Methods inherited from MapSerializer
#initialize, #serializable_record
Constructor Details
This class inherits a constructor from Delineate::MapSerializer
Instance Method Details
#serialize(options = {}) ⇒ Object
Returns the record’s mapped attributes as a JSON string. The specified options are passed to the JSON.encode() function.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/delineate/serializers/json_serializer.rb', line 9 def serialize( = {}) hash = super() if [:root] == true hash = { @record.class.model_name.element.to_sym => hash } elsif [:root] hash = { [:root].to_sym => hash } end opts = () opts.delete(:root) hash.to_json(opts) end |
#serialize_in(json_string, options = {}) ⇒ Object
Takes a record’s attributes represented as a JSON string, and returns a hash suitable for direct assignment to the record’s collection of attributes. For example:
s = ActiveRecord::AttributeMap::JsonSerializer.new(record, :api)
record.attributes = s.serialize_in(json_string)
30 31 32 |
# File 'lib/delineate/serializers/json_serializer.rb', line 30 def serialize_in(json_string, = {}) super(ActiveSupport::JSON.decode(json_string), ) end |