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

#initialize(params = {}) ⇒ AMF3Serializer

Returns a new instance of AMF3Serializer.



20
21
22
23
24
25
26
# File 'lib/amf/pure/serializer.rb', line 20

def initialize(params = {})
  @stream = ""
  @string_cache = SerializerCache.new :string
  @object_cache = SerializerCache.new :object
  @serializable_names = params[:serializable_names]
  @opts = params[:options]
end

Instance Attribute Details

#streamObject

Returns the value of attribute stream.



17
18
19
# File 'lib/amf/pure/serializer.rb', line 17

def stream
  @stream
end

#string_cacheObject (readonly)

Returns the value of attribute string_cache.



18
19
20
# File 'lib/amf/pure/serializer.rb', line 18

def string_cache
  @string_cache
end

Instance Method Details

#serialize(obj, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/amf/pure/serializer.rb', line 32

def serialize(obj, &block)
  if obj.is_a?(NilClass)
    write_null
  elsif obj.is_a?(TrueClass)
    write_true
  elsif obj.is_a?(FalseClass)
    write_false
  elsif obj.is_a?(Float)
    write_float(obj)
  elsif obj.is_a?(Integer)
    write_integer(obj)
  elsif obj.is_a?(Symbol) || obj.is_a?(String)
    write_string(obj.to_s)
  elsif obj.is_a?(Time)
    write_date(obj)
  elsif obj.is_a?(Array)
    write_array(obj, &block)
  elsif obj.is_a?(Hash) || obj.is_a?(Object)
    write_object(obj, &block)
  end
  @stream
end

#versionObject



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

def version
  3
end

#write_array(array) ⇒ Object



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

def write_array(array)
  @stream << AMF3_ARRAY_MARKER
  if @object_cache[array] != nil
    write_reference(@object_cache[array])
  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 << AMF3_CLOSE_DYNAMIC_ARRAY
    array.each do |elem|
      if elem.respond_to?(:to_amf)
        @stream << elem.to_amf(@opts)
      else
        serialize(elem)
      end
    end
    
    block.call(self) if block_given?
  end
end

#write_date(date) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/amf/pure/serializer.rb', line 91

def write_date(date)
  @stream << AMF3_DATE_MARKER
  if @object_cache[date] != nil
    write_reference(@object_cache[date])
  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_falseObject



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

def write_false
  @stream << AMF3_FALSE_MARKER
end

#write_float(float) ⇒ Object



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

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

#write_integer(int) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/amf/pure/serializer.rb', line 72

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

#write_nullObject



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

def write_null
  @stream << AMF3_NULL_MARKER
end

#write_object(obj, &block) ⇒ Object



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
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/amf/pure/serializer.rb', line 132

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

    # Always serialize things as dynamic objects
    @stream << AMF3_DYNAMIC_OBJECT

    if obj.is_a?(Hash)
      @stream << AMF3_ANONYMOUS_OBJECT
      
      # Stringify keys to make it easier later on and allow sorting
      obj.each do |k,v| 
        write_utf8_vr(k.to_s)
        serialize(obj[k])
      end
    else
      # Write class name/anonymous
      class_name = AMF::ClassMapper.get_as_class_name(obj)
      if class_name
        write_utf8_vr(class_name)
      else
        @stream << AMF3_ANONYMOUS_OBJECT
      end
    
      @serializable_names.sort.each do |name|
        write_utf8_vr(name.to_s.camelize(:lower))
        serialize(obj.send(name))
      end
    end

    block.call(self) if block_given?

    # Write close
    @stream << AMF3_CLOSE_DYNAMIC_OBJECT
  end
end

#write_reference(index) ⇒ Object



55
56
57
58
# File 'lib/amf/pure/serializer.rb', line 55

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

#write_string(str) ⇒ Object



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

def write_string(str)
  @stream << AMF3_STRING_MARKER
  write_utf8_vr(str)
end

#write_trueObject



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

def write_true
  @stream << AMF3_TRUE_MARKER
end

#write_utf8_vr(str) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/amf/pure/serializer.rb', line 173

def write_utf8_vr(str)
  if str == ''
    @stream << AMF3_EMPTY_STRING
  elsif @string_cache[str] != nil
    write_reference(@string_cache[str])
  else
    # Cache string
    @string_cache.add_obj(str)

    # Build AMF string
    header = str.length << 1 # make room for a low bit of 1
    header = header | 1 # set the low bit to 1
    @stream << pack_integer(header)
    @stream << str
  end
end