class Synapse::Serialization::Serializer
Represents a mechanism for serializing and deserializing objects @abstract
Attributes
@return [ConverterFactory]
@return [RevisionResolver]
Public Class Methods
@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
@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
@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
@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
@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
@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
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
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
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
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