Wednesday, July 13, 2011

vCO - OUT-parameter serialization with JSON

Today I stumbled again delivering OUT-parameters with vCO. There is a main thing you have to know: vCO can´t serialize objects. So it isn´t easy to deliver key-paired values to external interfaces, cause if you use "Object" and "Any" as output you`ll get some errors.

Discussing this with some smart guys at the customer site they came up with a JSON description in the "Developers Guide" which is used for WebView. Typically I put all information in a nested Array/string to generate a machine readable output, but JSON could be an alternative.

I started searching with the API Explorer for "JSON" and I found one action:

  •  objectToJson (includes the "serialize" action)
what describes exactly what I want to do.

So here is a little example vm(VC:VirtualMachine) ist IN-Parameter, param(string) is OUT-parameter:

var Specs = new Object();

Specs["vmName"] =;
Specs["vmId"] =;
Specs["vmNumCpus"] = vm.summary.config.numCpu;
Specs["vmMemorySize"] = vm.summary.config.memorySizeMB; 
Specs["vmPowerState"] = vm.runtime.PowerState.value;
Specs["vmIpAddress"] = vm.guest.ipAddress;
Specs["vmMacAddress"] = NicObject.macAddress;
Specs["vmGuestHeartbeatStatus"] = vm.summary.quickStats.guestHeartbeatStatus.value;
Specs["vmGuestMemoryUsage"] = vm.summary.quickStats.guestMemoryUsage;
Specs["vmOverallCpuUsage"] = vm.summary.quickStats.overallCpuUsage;
Specs["vmUptime"] = vm.summary.quickStats.uptimeSeconds/3600;
for(i in vm.guest.disk){
if(vm.guest.disk.capacity != undefined){
Specs["vmDiskCapacity"] = vm.guest.disk.capacity;
Specs["vmDiskCapacity"] = "undefined";
if(vm.guest.disk.freeSpace != undefined){
Specs["vmDiskFreeSpace"] = vm.guest.disk.freeSpace;
Specs["vmDiskFreeSpace"] = "undefined";

param = System.getModule("com.vmware.web.webview").objectToJson(Specs) ;

The easiest way to validate the code is JSONlint ( Please keep in mind that the JSON definition does not know "undefined" as type ( So you need to catch it up and change it into a string or sort it out.

Hope this helps.

1 comment: