Module: MongoMapper::Serialization

Defined in:
lib/mongo_mapper/serialization.rb,
lib/mongo_mapper/serializers/json_serializer.rb

Defined Under Namespace

Modules: ClassMethods Classes: JsonSerializer, Serializer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
# File 'lib/mongo_mapper/serializers/json_serializer.rb', line 3

def self.included(base)
  base.cattr_accessor :include_root_in_json, :instance_writer => false
  base.extend ClassMethods
end

Instance Method Details

#from_json(json) ⇒ Object



18
19
20
21
# File 'lib/mongo_mapper/serializers/json_serializer.rb', line 18

def from_json(json)
  self.attributes = ActiveSupport::JSON.decode(json)
  self
end

#to_json(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/mongo_mapper/serializers/json_serializer.rb', line 8

def to_json(options={})
  apply_to_json_defaults(options)

  if include_root_in_json
    "{#{self.class.json_class_name}: #{JsonSerializer.new(self, options).to_s}}"
  else
    JsonSerializer.new(self, options).to_s
  end
end