Module: FIRM::Serializable::JSON::SerializeClassMethods

Included in:
ID, SerializeClassMethods
Defined in:
lib/firm/serializer/json.rb

Overview

extend serialization class methods

Instance Method Summary collapse

Instance Method Details

#json_create(object) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/firm/serializer/json.rb', line 209

def json_create(object)
  data = object['data']
  # deserializing (anchor) object or alias
  if object.has_key?('*id')
    if Serializable::Aliasing.restored?(self, object['*id'])
      # resolving an already restored anchor for this alias
      Serializable::Aliasing.resolve_anchor(self, object['*id'])
    else
      # in case of cyclic references JSON will restore aliases before the anchors
      # so in this case we allocate an instance here and register it as
      # the anchor; when the anchor is restored it will re-use this instance to initialize & restore
      # the properties
      Serializable::Aliasing.restore_anchor(object['*id'], self.allocate)
    end
  else
    instance = if object.has_key?('&id')
                 anchor_id = object['&id'] # extract anchor id
                 if Serializable::Aliasing.restored?(self, anchor_id)
                   # in case of cyclic references an alias will already have restored the anchor instance
                   # (default constructed); retrieve that instance here for deserialization of properties
                   Serializable::Aliasing.resolve_anchor(self, anchor_id)
                 else
                   # restore the anchor here with a newly allocated instance
                   Serializable::Aliasing.restore_anchor(anchor_id, self.allocate)
                 end
               else
                 self.allocate
               end
    instance.__send__(:init_from_serialized, data)
            .__send__(:from_serialized, data)
            .__send__(:finalize_from_serialized)
  end
end