Class: RestfulX::AMF::Pure::RxAMFSerializer

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

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

#initializeRxAMFSerializer

Returns a new instance of RxAMFSerializer.



45
46
47
48
49
# File 'lib/restfulx/amf/pure/serializer.rb', line 45

def initialize()
  @stream = ""
  @string_cache = SerializerCache.new
  @object_cache = SerializerCache.new
end

Instance Method Details

#serialize_errors(errors) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/restfulx/amf/pure/serializer.rb', line 120

def serialize_errors(errors)
  @stream << AMF3_OBJECT_MARKER << AMF3_XML_DOC_MARKER
  write_vr('org.restfulx.messaging.io.ServiceErrors')
  serialize_property(errors)
  @stream << AMF3_CLOSE_DYNAMIC_OBJECT
  self
end

#serialize_models_array(records, options = {}) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/restfulx/amf/pure/serializer.rb', line 102

def serialize_models_array(records, options = {})
  @stream << AMF3_OBJECT_MARKER << AMF3_XML_DOC_MARKER
  write_vr('org.restfulx.messaging.io.ModelsCollection')
  @object_cache.cache_index += 2

  serialize_records(records, options)      
end

#serialize_property(prop) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/restfulx/amf/pure/serializer.rb', line 78

def serialize_property(prop)
  if prop.is_a?(NilClass)
    write_null
  elsif prop.is_a?(TrueClass)
    write_true
  elsif prop.is_a?(FalseClass)
    write_false
  elsif prop.is_a?(Float) || prop.is_a?(Bignum) || prop.is_a?(BigDecimal)
    write_float(prop)
  elsif prop.is_a?(Integer)
    write_integer(prop)
  elsif prop.is_a?(Symbol) || prop.is_a?(String)
    write_string(prop.to_s)
  elsif prop.is_a?(Time) || prop.is_a?(DateTime)
    write_time(prop)
  elsif prop.is_a?(Date)
    write_date(prop)
  elsif prop.is_a?(Hash)
    write_hash(prop)
  end
  
  self
end

#serialize_record(record, serializable_names = nil, options = {}, &block) ⇒ Object



128
129
130
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/restfulx/amf/pure/serializer.rb', line 128

def serialize_record(record, serializable_names = nil, options = {}, &block)
  @stream << AMF3_OBJECT_MARKER
  record_id = record.respond_to?(:unique_id) ? record.unique_id : record.object_id

  partials = {}

  if @object_cache[record_id] != nil
    write_reference(@object_cache[record_id])
  else
    # Cache object
    @object_cache.cache(record_id)

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

    # Write class name/anonymous
    class_name = RestfulX::AMF::ClassMapper.get_as_class_name(record)
    if class_name
      write_vr(class_name)
    else
      @stream << AMF3_ANONYMOUS_OBJECT
    end

    serializable_names.each do |prop|
      if prop.is_a?(Hash)
        record_name = prop[:name]
        record_value = record[record_name]
        ref_name = prop[:ref_name]
        ref_class = prop[:ref_class]
        
        if ref_class == "polymorphic"
          ref_class_name = record["#{prop[:orig_ref_name]}_type"]
          ref_class = ref_class_name.constantize
        else
          ref_class_name = ref_class.class_name
        end
        
        result_id = "#{ref_class_name}_#{record_value}" if record_value

        write_vr(ref_name)
        if result_id               
          if @object_cache[result_id]
            @stream << AMF3_OBJECT_MARKER
            write_reference(@object_cache[result_id])
          else
            partials[ref_name.to_s] = ref_class_name
            unless partial = options[:cached_instances][ref_class_name]
              options[:cached_instances][ref_class_name] = ref_class.new
              partial = options[:cached_instances][ref_class_name]
            end
            partial.id = record_value
            serialize_record(partial, ['id'])
          end
        else
          write_null
        end
      else
        write_vr(prop.to_s.camelize(:lower))
        serialize_property(record[prop])
      end
    end

    write_vr("partials")
    serialize_property(partials)

    block.call(self) if block_given?

    # Write close
    @stream << AMF3_CLOSE_DYNAMIC_OBJECT
  end
  self
end

#serialize_typed_array(records, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/restfulx/amf/pure/serializer.rb', line 110

def serialize_typed_array(records, options = {})
  @stream << AMF3_OBJECT_MARKER << AMF3_XML_DOC_MARKER
  write_vr('org.restfulx.messaging.io.TypedArray')
  @object_cache.cache_index += 1
  serialize_property(options[:attributes])
  @object_cache.cache_index += 1

  serialize_records(records, options)      
end

#to_sObject



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

def to_s
  @stream
end

#versionObject



51
52
53
# File 'lib/restfulx/amf/pure/serializer.rb', line 51

def version
  3
end

#write_vr(name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/restfulx/amf/pure/serializer.rb', line 59

def write_vr(name)
  if name == ''
    @stream << AMF3_EMPTY_STRING
  elsif @string_cache[name] != nil
    write_reference(@string_cache[name])
  else
    # Cache string
    @string_cache.cache(name)

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