class Synapse::Serialization::Serializer

Represents a mechanism for serializing and deserializing objects @abstract

Attributes

converter_factory[R]

@return [ConverterFactory]

revision_resolver[RW]

@return [RevisionResolver]

Public Class Methods

new(converter_factory) click to toggle source

@param [ConverterFactory] converter_factory @return [undefined]

# File lib/synapse/serialization/serializer.rb, line 14
def initialize(converter_factory)
  @converter_factory = converter_factory
end

Public Instance Methods

can_serialize_to?(representation_type) click to toggle source

@param [Class] representation_type @return [Boolean]

# 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) click to toggle source

@param [SerializedType] serialized_type @return [Class]

# 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) click to toggle source

@param [SerializedObject] serialized_object @return [Object]

# 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) click to toggle source

@param [Object] object @param [Class] representation_type @return [SerializedObject]

# 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) click to toggle source

@param [Class] type @return [SerializedType]

# 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

Protected Instance Methods

native_content_type() click to toggle source

Returns the native content type that the serializer works with

@abstract @return [Class]

# File lib/synapse/serialization/serializer.rb, line 89
def native_content_type
  raise NotImplementedError
end
perform_deserialize(content, type) click to toggle source

Deserializes the given serialized content into the given Ruby type

@abstract @param [Object] content Should be in the native content type of the serializer @param [Class] type The class type to be deserialized into @return [Object] The deserialized object

# File lib/synapse/serialization/serializer.rb, line 81
def perform_deserialize(content, type)
  raise NotImplementedError
end
perform_serialize(content) click to toggle source

Serializes the given Ruby object

@abstract @param [Object] content The original Ruby object to serialize @return [Object] Should be in the native content type of the serializer

# File lib/synapse/serialization/serializer.rb, line 71
def perform_serialize(content)
  raise NotImplementedError
end

Private Instance Methods

convert(original, source_type, target_type) click to toggle source

Converts the given content from the given source type to the given target type

@param [Object] original @param [Class] source_type @param [Class] target_type @return [Object]

# File lib/synapse/serialization/serializer.rb, line 101
def convert(original, source_type, target_type)
  converter_factory.converter(source_type, target_type).convert_content(original)
end