Class: RocketAMF::Pure::Serializer
- Inherits:
-
Object
- Object
- RocketAMF::Pure::Serializer
- Includes:
- WriteIOHelpers
- Defined in:
- lib/rocketamf/pure/serializer.rb
Overview
Pure ruby serializer for AMF0 and AMF3
Instance Attribute Summary collapse
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize(class_mapper) ⇒ Serializer
constructor
Pass in the class mapper instance to use when serializing.
-
#serialize(version, obj) ⇒ Object
Serialize the given object using AMF0 or AMF3.
-
#write_array(arr) ⇒ Object
Helper for writing arrays inside encode_amf.
-
#write_object(obj, props = nil, traits = nil) ⇒ Object
Helper for writing objects inside encode_amf.
Methods included from WriteIOHelpers
#byte_order, #byte_order_little?, #pack_double, #pack_int16_network, #pack_int8, #pack_integer, #pack_word32_network
Constructor Details
#initialize(class_mapper) ⇒ Serializer
Pass in the class mapper instance to use when serializing. This enables better caching behavior in the class mapper and allows one to change mappings between serialization attempts.
12 13 14 15 16 |
# File 'lib/rocketamf/pure/serializer.rb', line 12 def initialize class_mapper @class_mapper = class_mapper @stream = "" @depth = 0 end |
Instance Attribute Details
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
7 8 9 |
# File 'lib/rocketamf/pure/serializer.rb', line 7 def stream @stream end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
7 8 9 |
# File 'lib/rocketamf/pure/serializer.rb', line 7 def version @version end |
Instance Method Details
#serialize(version, obj) ⇒ Object
Serialize the given object using AMF0 or AMF3. Can be called from inside encode_amf, but make sure to pass in the proper version or it may not be possible to decode. Use the serializer version attribute for this.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rocketamf/pure/serializer.rb', line 21 def serialize version, obj raise ArgumentError, "unsupported version #{version}" unless [0,3].include?(version) @version = version # Initialize caches if @depth == 0 if @version == 0 @ref_cache = SerializerCache.new :object else @string_cache = SerializerCache.new :string @object_cache = SerializerCache.new :object @trait_cache = SerializerCache.new :string end end @depth += 1 # Perform serialization if @version == 0 amf0_serialize(obj) else amf3_serialize(obj) end # Cleanup @depth -= 1 if @depth == 0 @ref_cache = nil @string_cache = nil @object_cache = nil @trait_cache = nil end return @stream end |
#write_array(arr) ⇒ Object
Helper for writing arrays inside encode_amf. It uses the current AMF version to write the array.
58 59 60 61 62 63 64 |
# File 'lib/rocketamf/pure/serializer.rb', line 58 def write_array arr if @version == 0 amf0_write_array arr else amf3_write_array arr end end |
#write_object(obj, props = nil, traits = nil) ⇒ Object
Helper for writing objects inside encode_amf. It uses the current AMF version to write the object. If you pass in a property hash, it will use it rather than having the class mapper determine properties. For AMF3, you can also specify a traits hash, which can be used to reduce serialized data size or serialize things as externalizable.
71 72 73 74 75 76 77 |
# File 'lib/rocketamf/pure/serializer.rb', line 71 def write_object obj, props=nil, traits=nil if @version == 0 amf0_write_object obj, props else amf3_write_object obj, props, traits end end |