Class: AMF::Pure::AMF3Serializer

Inherits:
Object
  • Object
show all
Includes:
WriteIOHelpers
Defined in:
lib/amf/pure/serializer.rb

Overview

AMF3 implementation of serializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WriteIOHelpers

#byte_order, #byte_order_little?, #pack_double, #pack_int16_network, #pack_int8, #pack_integer, #pack_word32_network

Constructor Details

#initializeAMF3Serializer

Returns a new instance of AMF3Serializer.



26
27
28
29
# File 'lib/amf/pure/serializer.rb', line 26

def initialize
  @string_cache = SerializerCache.new
  @object_cache = SerializerCache.new
end

Instance Attribute Details

#string_cacheObject (readonly)

Returns the value of attribute string_cache.



24
25
26
# File 'lib/amf/pure/serializer.rb', line 24

def string_cache
  @string_cache
end

Instance Method Details

#serialize(obj, stream = "") ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/amf/pure/serializer.rb', line 35

def serialize obj, stream = ""
  if obj.respond_to?(:to_amf)
    stream << obj.to_amf(self)
  elsif obj.is_a?(NilClass)
    write_null stream
  elsif obj.is_a?(TrueClass)
    write_true stream
  elsif obj.is_a?(FalseClass)
    write_false stream
  elsif obj.is_a?(Float)
    write_float obj, stream
  elsif obj.is_a?(Integer)
    write_integer obj, stream
  elsif obj.is_a?(Symbol) || obj.is_a?(String)
    write_string obj.to_s, stream
  elsif obj.is_a?(Time)
    write_date obj, stream
  elsif obj.is_a?(Array)
    write_array obj, stream
  elsif obj.is_a?(Hash) || obj.is_a?(Object)
    write_object obj, stream
  end
  stream
end

#versionObject



31
32
33
# File 'lib/amf/pure/serializer.rb', line 31

def version
  3
end

#write_array(array, stream) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/amf/pure/serializer.rb', line 112

def write_array array, stream
  stream << AMF3_ARRAY_MARKER
  if @object_cache[array] != nil
    write_reference @object_cache[array], stream
  else
    # Cache array
    @object_cache.add_obj array

    # Build AMF string
    header = array.length << 1 # make room for a low bit of 1
    header = header | 1 # set the low bit to 1
    stream << pack_integer(header)
    stream << CLOSE_DYNAMIC_ARRAY
    array.each do |elem|
      serialize elem, stream
    end
  end
end

#write_date(date, stream) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/amf/pure/serializer.rb', line 96

def write_date date, stream
  stream << AMF3_DATE_MARKER
  if @object_cache[date] != nil
    write_reference @object_cache[date], stream
  else
    # Cache date
    @object_cache.add_obj date

    # Build AMF string
    date.utc unless date.utc?
    seconds = (date.to_f * 1000).to_i
    stream << pack_integer(AMF3_NULL_MARKER)
    stream << pack_double(seconds)
  end
end

#write_false(stream) ⇒ Object



73
74
75
# File 'lib/amf/pure/serializer.rb', line 73

def write_false stream
  stream << AMF3_FALSE_MARKER
end

#write_float(float, stream) ⇒ Object



86
87
88
89
# File 'lib/amf/pure/serializer.rb', line 86

def write_float float, stream
  stream << AMF3_DOUBLE_MARKER
  stream << pack_double(float)
end

#write_integer(int, stream) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/amf/pure/serializer.rb', line 77

def write_integer int, stream
  if int < MIN_INTEGER || int > MAX_INTEGER # Check valid range for 29 bits
    write_float int.to_f, stream
  else
    stream << AMF3_INTEGER_MARKER
    stream << pack_integer(int)
  end
end

#write_null(stream) ⇒ Object



65
66
67
# File 'lib/amf/pure/serializer.rb', line 65

def write_null stream
  stream << AMF3_NULL_MARKER
end

#write_object(obj, stream) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/amf/pure/serializer.rb', line 131

def write_object obj, stream
  stream << AMF3_OBJECT_MARKER
  if @object_cache[obj] != nil
    write_reference @object_cache[obj], stream
  else
    # Cache object
    @object_cache.add_obj obj

    # Always serialize things as dynamic objects
    stream << DYNAMIC_OBJECT

    # Write class name/anonymous
    class_name = ClassMapper.get_as_class_name obj
    if class_name
      write_utf8_vr class_name, stream
    else
      stream << ANONYMOUS_OBJECT
    end

    # Write out properties
    props = ClassMapper.props_for_serialization obj
    props.sort.each do |key, val| # Sort props until Ruby 1.9 becomes common
      write_utf8_vr key.to_s, stream
      serialize val, stream
    end

    # Write close
    stream << CLOSE_DYNAMIC_OBJECT
  end
end

#write_reference(index, stream) ⇒ Object



60
61
62
63
# File 'lib/amf/pure/serializer.rb', line 60

def write_reference index, stream
  header = index << 1 # shift value left to leave a low bit of 0
  stream << pack_integer(header)
end

#write_string(str, stream) ⇒ Object



91
92
93
94
# File 'lib/amf/pure/serializer.rb', line 91

def write_string str, stream
  stream << AMF3_STRING_MARKER
  write_utf8_vr str, stream
end

#write_true(stream) ⇒ Object



69
70
71
# File 'lib/amf/pure/serializer.rb', line 69

def write_true stream
  stream << AMF3_TRUE_MARKER
end