Module: MongoMapper::Plugins::Serialization::Json

Defined in:
lib/mongo_mapper/plugins/serialization.rb

Defined Under Namespace

Modules: ClassMethods Classes: JsonSerializer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



58
59
60
61
# File 'lib/mongo_mapper/plugins/serialization.rb', line 58

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



79
80
81
82
# File 'lib/mongo_mapper/plugins/serialization.rb', line 79

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

#to_json(options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/mongo_mapper/plugins/serialization.rb', line 69

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