Class: Synapse::Serialization::Serializer Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/serialization/serializer.rb

Overview

This class is abstract.

Represents a mechanism for serializing and deserializing objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(converter_factory) ⇒ undefined

Parameters:



14
15
16
# File 'lib/synapse/serialization/serializer.rb', line 14

def initialize(converter_factory)
  @converter_factory = converter_factory
end

Instance Attribute Details

#converter_factoryConverterFactory (readonly)

Returns:



7
8
9
# File 'lib/synapse/serialization/serializer.rb', line 7

def converter_factory
  @converter_factory
end

#revision_resolverRevisionResolver

Returns:



10
11
12
# File 'lib/synapse/serialization/serializer.rb', line 10

def revision_resolver
  @revision_resolver
end

Instance Method Details

#can_serialize_to?(representation_type) ⇒ Boolean

Parameters:

  • representation_type (Class)

Returns:

  • (Boolean)


40
41
42
# File 'lib/synapse/serialization/serializer.rb', line 40

def can_serialize_to?(representation_type)
  converter_factory.has_converter?(native_content_type, representation_type)
end

#class_for(serialized_type) ⇒ Class

Parameters:

Returns:

  • (Class)


46
47
48
49
50
51
52
# File 'lib/synapse/serialization/serializer.rb', line 46

def class_for(serialized_type)
  begin
    serialized_type.name.constantize
  rescue
    raise UnknownSerializedTypeError, 'Unknown serialized type %s' % serialized_type.name
  end
end

#deserialize(serialized_object) ⇒ Object

Parameters:

Returns:

  • (Object)


31
32
33
34
35
36
# File 'lib/synapse/serialization/serializer.rb', line 31

def deserialize(serialized_object)
  content = convert(serialized_object.content, serialized_object.content_type, native_content_type)
  type = class_for(serialized_object.type)

  perform_deserialize(content, type)
end

#serialize(object, representation_type) ⇒ SerializedObject

Parameters:

  • object (Object)
  • representation_type (Class)

Returns:



21
22
23
24
25
26
27
# File 'lib/synapse/serialization/serializer.rb', line 21

def serialize(object, representation_type)
  content = perform_serialize(object)
  content = convert(content, native_content_type, representation_type)
  type = type_for(object.class)

  SerializedObject.new(content, representation_type, type)
end

#type_for(type) ⇒ SerializedType

Parameters:

  • type (Class)

Returns:



56
57
58
59
60
61
62
# File 'lib/synapse/serialization/serializer.rb', line 56

def type_for(type)
  if @revision_resolver
    SerializedType.new(type.to_s, @revision_resolver.revision_of(type))
  else
    SerializedType.new(type.to_s)
  end
end