From 06dda10f31d52f3aa8d08fcf4ec1242c01e97863 Mon Sep 17 00:00:00 2001 From: Prasanna Santhanam Date: Wed, 30 Nov 2011 17:38:31 +0530 Subject: [PATCH] - when de-serializing back to python from json, python attribute must be built from dicts only, lists and tuples which don't contain dicts will be directly set to the attribute - eg: listnetworkserviceproviderresponse : servicelist = ['vpn', 'dhcp', 'firewall'] contains no name,value combination to make attributes. So we make self.servicelist = ['vpn','dhcp','firewall'] --- tools/testClient/jsonHelper.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/testClient/jsonHelper.py b/tools/testClient/jsonHelper.py index 6bf6d056f1f..32d32f05f59 100644 --- a/tools/testClient/jsonHelper.py +++ b/tools/testClient/jsonHelper.py @@ -1,7 +1,7 @@ import cloudstackException import json import inspect -from cloudstackAPI import * +from cloudstackAPI import * import pdb class jsonLoader: @@ -12,7 +12,10 @@ class jsonLoader: if isinstance(v, dict): setattr(self, k, jsonLoader(v)) elif isinstance(v, (list, tuple)): - setattr(self, k, [jsonLoader(elem) for elem in v]) + if isinstance(v[0], dict): + setattr(self, k, [jsonLoader(elem) for elem in v]) + else: + setattr(self, k, v) else: setattr(self,k,v) def __getattr__(self, val): @@ -125,6 +128,7 @@ if __name__ == "__main__": result = '{ "listnetworkserviceprovidersresponse" : { "count":1 ,"networkserviceprovider" : [ {"name":"VirtualRouter","physicalnetworkid":"ad2948fc-1054-46c7-b1c7-61d990b86710","destinationphysicalnetworkid":"0","state":"Disabled","id":"d827cae4-4998-4037-95a2-55b92b6318b1","servicelist":["Vpn","Dhcp","Dns","Gateway","Firewall","Lb","SourceNat","StaticNat","PortForwarding","UserData"]} ] } }' nsp = getResultObj(result) + print nsp[0].id result = '{ "listzonesresponse" : { "count":1 ,"zone" : [ {"id":1,"name":"test0","dns1":"8.8.8.8","dns2":"4.4.4.4","internaldns1":"192.168.110.254","internaldns2":"192.168.110.253","networktype":"Basic","securitygroupsenabled":true,"allocationstate":"Enabled","zonetoken":"5e818a11-6b00-3429-9a07-e27511d3169a","dhcpprovider":"DhcpServer"} ] } }' zones = getResultObj(result)