我的班级结构如下 -
class HelloWorld (object):
def __init__(self, name, i, id):
self.name = name
self.i = i
self.id = id
我正在创建一个对象
p = HelloWorld('pj', 3456, '1234')
并将此对象传递给我使用的定义,jsonpickle.encode
如下jsonpickle.decode
所示
>>>print(type(p))
<class 'HelloWorld'>
>>>x = jsonpickle.encode(p)
>>>print(x)
{"i": 3456, "name": "pj", "py/object": "__builtin__.HelloWorld", "id": "1234"}
>>>y = jsonpickle.decode(x)
>>>print(type(y))
<class 'dict'>
我不明白为什么我无法将其解码回原始对象,即使区分py/object
也存在。
谁能建议我做错了什么?
添加生成上述用例的动态类的代码。
def _create_pojo(self, pojo_class_name, param_schema):
#extracting the properties from the parameter 'schema'
pojo_properties = param_schema['properties']
#creating a list of property keys
pojo_property_list = []
for key in pojo_properties:
pojo_property_list.append(key)
source = \
"class %s (object):\n" % ( pojo_class_name )
source += " def __init__(self, %s" % ', '.join(pojo_property_list) +"):\n" #defining __init__ for the class
for prop in pojo_property_list: #creating a variable in the __init__ method for each property in the property list
source += " self.%s = %s\n" % (prop, prop)
#generating the class code
class_code = compile( source, "<generated class>", "exec" )
fakeglobals = {}
eval( class_code, {"object" : object}, fakeglobals )
result = fakeglobals[ pojo_class_name ]
result ._source = source
pprint.pprint(source)
self._object_types[ pojo_class_name ] = result
return result