Class: RbVmomi::OldDeserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbvmomi/deserialization.rb

Constant Summary collapse

NS_XSI =
'http://www.w3.org/2001/XMLSchema-instance'

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ OldDeserializer

Returns a new instance of OldDeserializer.



160
161
162
# File 'lib/rbvmomi/deserialization.rb', line 160

def initialize conn
  @conn = conn
end

Instance Method Details

#demangle_array_type(x) ⇒ Object



233
234
235
236
237
238
239
240
# File 'lib/rbvmomi/deserialization.rb', line 233

def demangle_array_type x
  case x
  when 'AnyType' then 'anyType'
  when 'DateTime' then 'dateTime'
  when 'Boolean', 'String', 'Byte', 'Short', 'Int', 'Long', 'Float', 'Double' then x.downcase
  else x
  end
end

#deserialize(xml, typename = nil) ⇒ Object



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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rbvmomi/deserialization.rb', line 164

def deserialize xml, typename=nil
  if IS_JRUBY
    type_attr = xml.attribute_nodes.find { |a| a.name == 'type' &&
                                               a.namespace &&
                                               a.namespace.prefix == 'xsi' }
  else
    type_attr = xml.attribute_with_ns('type', NS_XSI)
  end
  typename = (type_attr || typename).to_s

  if typename =~ /^ArrayOf/
    typename = demangle_array_type $'
    return xml.children.select(&:element?).map { |c| deserialize c, typename }
  end

  t = @conn.type typename
  if t <= BasicTypes::DataObject
    props_desc = t.full_props_desc
    h = {}
    props_desc.select { |d| d['is-array'] }.each { |d| h[d['name'].to_sym] = [] }
    xml.children.each do |c|
      next unless c.element?
      field = c.name.to_sym
      d = t.find_prop_desc(field.to_s) or next
      o = deserialize c, d['wsdl_type']
      if h[field].is_a? Array
        h[field] << o
      else
        h[field] = o
      end
    end
    t.new h
  elsif t == BasicTypes::ManagedObjectReference
    @conn.type(xml['type']).new @conn, xml.text
  elsif t <= BasicTypes::ManagedObject
    @conn.type(xml['type'] || t.wsdl_name).new @conn, xml.text
  elsif t <= BasicTypes::Enum
    xml.text
  elsif t <= BasicTypes::KeyValue
    h = {}
    xml.children.each do |c|
      next unless c.element?
      h[c.name] = c.text
    end
    [h['key'], h['value']]
  elsif t <= String
    xml.text
  elsif t <= Symbol
    xml.text.to_sym
  elsif t <= Integer
    xml.text.to_i
  elsif t <= Float
    xml.text.to_f
  elsif t <= Time
    Time.parse xml.text
  elsif t == BasicTypes::Boolean
    xml.text == 'true' || xml.text == '1'
  elsif t == BasicTypes::Binary
    xml.text.unpack('m')[0]
  elsif t == BasicTypes::AnyType
    fail "attempted to deserialize an AnyType"
  else fail "unexpected type #{t.inspect} (#{t.ancestors * '/'})"
  end
rescue
  $stderr.puts "#{$!.class} while deserializing #{xml.name} (#{typename}):"
  $stderr.puts xml.to_s
  raise
end