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"] = vm.name;
Specs["vmId"] = vm.id;
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){
System.log(vm.guest.disk.capacity);
Specs["vmDiskCapacity"] = vm.guest.disk.capacity;
}
else{
Specs["vmDiskCapacity"] = "undefined";
}
if(vm.guest.disk.freeSpace != undefined){
System.log(vm.guest.disk.freeSpace);
Specs["vmDiskFreeSpace"] = vm.guest.disk.freeSpace;
}
else{
Specs["vmDiskFreeSpace"] = "undefined";
}
}
param = System.getModule("com.vmware.web.webview").objectToJson(Specs) ;
The easiest way to validate the code is JSONlint (http://jsonlint.com). Please keep in mind that the JSON definition does not know "undefined" as type (http://en.wikipedia.org/wiki/JSON). So you need to catch it up and change it into a string or sort it out.
Hope this helps.
No comments:
Post a Comment