Class: Delineate::Serializers::JsonSerializer

Inherits:
MapSerializer show all
Defined in:
lib/delineate/serializers/json_serializer.rb

Overview

AttributeMap serializer that handles JSON as the external data format.

Instance Method Summary collapse

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(options = {})
  hash = super()

  if options[:root] == true
    hash = { @record.class.model_name.element.to_sym => hash }
  elsif options[:root]
    hash = { options[:root].to_sym => hash }
  end
  opts = remove_serializer_class_options(options)
  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, options = {})
  super(ActiveSupport::JSON.decode(json_string), options)
end