使用 jsonpickle 从文件保存和加载对象
我有以下简单的方法使用 jsonpickle 将 python 对象写入文件:
def json_serialize(obj, filename, use_jsonpickle=True):
f = open(filename, 'w')
if use_jsonpickle:
import jsonpickle
json_obj = jsonpickle.encode(obj)
f.write(json_obj)
else:
simplejson.dump(obj, f)
f.close()
def json_load_file(filename, use_jsonpickle=True):
f = open(filename)
if use_jsonpickle:
import jsonpickle
json_str = f.read()
obj = jsonpickle.decode(json_str)
else:
obj = simplejson.load(f)
return obj
问题是每当我使用这些方法时,它都会将我的对象作为字典加载回来(具有以下字段:“py/object”:“my_module.MyClassName” ),但不是用于生成 json 字符串的类型的实际 Python 对象。我怎样才能让 jsonpickle 实际上将加载的字符串转换回对象?
为了用一个例子来说明这一点,请考虑以下内容:
class Foo:
def __init__(self, hello):
self.hello = hello
# make a Foo obj
obj = Foo("hello world")
obj_str = jsonpickle.encode(obj)
restored_obj = jsonpickle.decode(obj_str)
list_objects = [restored_obj]
# We now get a list with a dictionary, rather than
# a list containing a Foo object
print "list_objects: ", list_objects
这会产生:
list_objects: [{'py/object': 'as_events.Foo', 'hello': 'hello world'}]
而不是类似:[Foo()]。我该如何解决这个问题?
谢谢。
I have the following simple methods for writing a python object to a file using jsonpickle:
def json_serialize(obj, filename, use_jsonpickle=True):
f = open(filename, 'w')
if use_jsonpickle:
import jsonpickle
json_obj = jsonpickle.encode(obj)
f.write(json_obj)
else:
simplejson.dump(obj, f)
f.close()
def json_load_file(filename, use_jsonpickle=True):
f = open(filename)
if use_jsonpickle:
import jsonpickle
json_str = f.read()
obj = jsonpickle.decode(json_str)
else:
obj = simplejson.load(f)
return obj
the problem is that whenever I use these, it loads my objects back as dictionaries (that have fields like: "py/object": "my_module.MyClassName") but not as an actual Python object of the type that was used to generate the json string. How can I make it so jsonpickle actually converts the loaded string back to the object?
to illustrate this with an example, consider the following:
class Foo:
def __init__(self, hello):
self.hello = hello
# make a Foo obj
obj = Foo("hello world")
obj_str = jsonpickle.encode(obj)
restored_obj = jsonpickle.decode(obj_str)
list_objects = [restored_obj]
# We now get a list with a dictionary, rather than
# a list containing a Foo object
print "list_objects: ", list_objects
This yields:
list_objects: [{'py/object': 'as_events.Foo', 'hello': 'hello world'}]
Rather than something like: [Foo()]. How can I fix this?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正确的答案是我没有继承
object
。如果不继承object
,jsonpickle 似乎无法正确解码在构造函数中采用一个或多个参数的类。我绝不是专家,但在类声明中使用Foo(object):
而不是Foo:
修复了它。The correct answer was that I was not inheriting from
object
. Without inheriting fromobject
, jsonpickle cannot correctly decode classes that take one or more arguments in the constructor, it seems. I am by no means an expert but making itFoo(object):
rather thanFoo:
in the class declaration fixed it.确保
json_load_file()
中的use_jsonpickle == True
。看来您使用jsonpickle
序列化并使用json
加载。确保对象类型可用
Make sure that
use_jsonpickle == True
injson_load_file()
. It seems that you serialize usingjsonpickle
and load usingjson
.Make sure that object type is available
截至本文发布时,存在一个错误,如果序列化对象是内部类,则会导致编码错误。确保该类不位于另一个类中。我已向维护者提出问题。 https://github.com/jsonpickle/jsonpickle/issues/210
As of this posting, there is a bug which causes encoding to be wrong if the serialized object is an inner class. Make sure the class is not located within another class. I've filed an issue with the maintainer. https://github.com/jsonpickle/jsonpickle/issues/210