Class: Lobster::Serialization::SerializationEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/lobster/serialization/serialization_engine.rb

Overview

Handles marshaling and de-marshaling complex objects consisting of multiple types. This class uses a combination of Serializer instances to serialize and de-serialize objects. The default serializer is included by default. JSON is used to store marshaled information about objects.

Constant Summary collapse

OPTS =

Serialization options for JSON parsing and generation.

{
    :quirks_mode     => true, # Allow any top-level object in JSON.
    :symbolize_names => true  # Use symbols for keys.
}

Instance Method Summary collapse

Constructor Details

#initializeSerializationEngine

Creates the engine. The engine will start with the default serializer.



22
23
24
25
26
# File 'lib/lobster/serialization/serialization_engine.rb', line 22

def initialize
  @serializers = {
      Object => Serializer.new
  }
end

Instance Method Details

#add_serializer(serializer, type) ⇒ nil

Include another serializer for marshaling and de-marshaling objects.

Parameters:

  • serializer (Serializer)

    New serializer to include.

  • type (Class)

    Type the serializer can process.

Returns:

  • (nil)


32
33
34
35
36
37
# File 'lib/lobster/serialization/serialization_engine.rb', line 32

def add_serializer(serializer, type)
  fail ArgumentError unless serializer.kind_of?(Serializer)
  fail ArgumentError unless type.is_a?(Class)

  @serializers[type] = serializer
end

#deserialize(json_str, type, *args) ⇒ Object

Creates an object from its string representation.

Parameters:

  • json_str (String)

    JSON string containing the object's contents.

  • type (Class)

    Type of object contained in the string.

  • args

    Extra arguments to pass to the first serializer.

Returns:

  • (Object)

    Object constructed the contents of the string.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lobster/serialization/serialization_engine.rb', line 51

def deserialize(json_str, type, *args)
  fail ArgumentError unless json_str.is_a?(String)
  fail ArgumentError unless type.is_a?(Class)

  begin
    obj = JSON.parse(json_str, OPTS)
    deserialize_inner(obj, type, *args)
  rescue JSON::ParserError
    fail FormatError
  end
end

#deserialize_inner(obj, type, *args) ⇒ Object

Used internally for de-serializing nested objects. Only Serializer and its sub-classes should cal this method.

Parameters:

  • obj (Object)

    Simple object to de-serialize.

  • type (Class)

    Type of object to de-serialize to.

  • args

    Extra arguments to pass to the serializer.

Returns:

  • (Object)

    De-serialized object as the given type.



78
79
80
81
# File 'lib/lobster/serialization/serialization_engine.rb', line 78

def deserialize_inner(obj, type, *args)
  serializer = pick_serializer(type)
  serializer.deserialize(obj, type, self, *args)
end

#serialize(obj) ⇒ String

Creates a string representation of the object.

Parameters:

  • obj (Object)

    Object to serialize.

Returns:

  • (String)

    JSON string containing the object's contents.



42
43
44
# File 'lib/lobster/serialization/serialization_engine.rb', line 42

def serialize(obj)
  serialize_inner(obj).to_json(OPTS)
end

#serialize_inner(obj) ⇒ Object

Used internally for serializing nested objects. Only Serializer and its sub-classes should call this method.

Parameters:

  • obj (Object)

    Object to serialize.

Returns:

  • (Object)

    Simpler object produced by a serializer.



67
68
69
70
# File 'lib/lobster/serialization/serialization_engine.rb', line 67

def serialize_inner(obj)
  serializer = pick_serializer(obj.class)
  serializer.serialize(obj, self)
end