Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions lib/rbvmomi/basic_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def to_s
end
end

def as_json
to_s
end

init
end

Expand Down Expand Up @@ -162,22 +166,24 @@ def pretty_print q
q.text ')'
end

def as_hash(val)
if val.kind_of?(Array)
val.map { |v| as_hash(v) }
elsif val.respond_to?(:to_hash)
val.to_hash
else
val
end
def as_json
props.transform_values { |v| as_json_nested(v) }
end

def to_hash
props.transform_values { |v| as_hash(v) }
def to_json(options = nil)
as_json.merge(JSON.create_id => self.class.name).to_json
end

def to_json(options = nil)
to_hash.merge(JSON.create_id => self.class.name).to_json
private

def as_json_nested(val)
if val.kind_of?(Array)
val.map { |v| as_json_nested(v) }
elsif val.respond_to?(:as_json)
val.as_json
else
val
end
end

init
Expand All @@ -203,10 +209,6 @@ def to_s
"#{self.class.wsdl_name}(#{@ref.inspect})"
end

def to_hash
to_s
end

def pretty_print pp
pp.text to_s
end
Expand Down Expand Up @@ -282,7 +284,7 @@ def initialize value
@value = value
end

def to_hash
def to_s
value
end

Expand Down
14 changes: 7 additions & 7 deletions test/test_misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ def test_loader
assert_equal klass, klass2
end

def test_managed_object_to_hash
assert_equal VIM.VirtualMachine(nil, 'vm-123').to_hash, 'VirtualMachine("vm-123")'
def test_managed_object_as_json
assert_equal VIM.VirtualMachine(nil, 'vm-123').as_json, 'VirtualMachine("vm-123")'
end

def test_managed_object_to_json
assert_equal VIM.VirtualMachine(nil, 'vm-123').to_json, '"VirtualMachine(\\"vm-123\\")"'
end

def test_data_object_to_hash
def test_data_object_as_json
# With a nested ManagedObject value
assert_equal VIM.VirtualMachineSummary({vm: VIM.VirtualMachine(nil, 'vm-123')}).to_hash, {vm: 'VirtualMachine("vm-123")'}
assert_equal VIM.VirtualMachineSummary({vm: VIM.VirtualMachine(nil, 'vm-123')}).as_json, {vm: 'VirtualMachine("vm-123")'}

# With an array
assert_equal VIM.VirtualMachineSummary({customValue: [VIM.CustomFieldValue({key: 1})]}).to_hash, {customValue: [{key: 1}]}
assert_equal VIM.VirtualMachineSummary({customValue: [VIM.CustomFieldValue({key: 1})]}).as_json, {customValue: [{key: 1}]}

# With an Enum
assert_equal VIM.VirtualMachineSummary({overallStatus: VIM.ManagedEntityStatus('green')}).to_hash, {overallStatus: 'green'}
assert_equal VIM.VirtualMachineSummary({overallStatus: VIM.ManagedEntityStatus('green')}).as_json, {overallStatus: 'green'}

# Combined
assert_equal VIM.VirtualMachineSummary(
vm: VIM.VirtualMachine(nil, 'vm-123'),
customValue: [VIM.CustomFieldValue(key: 1)],
overallStatus: VIM.ManagedEntityStatus('green')
).to_hash,
).as_json,
{
vm: 'VirtualMachine("vm-123")',
customValue: [{key: 1}],
Expand Down