我可以为您的情况考虑三种解决方案:
解决方案1:
使用Pykson第三方库并将要序列化的字段定义为pykson字段。
示例:
class MyItemClass(pykson.JsonObject):
saved_property = pykson.IntegerField()
my_object = MyItemClass(saved_property=1,accept_unknown=True)
my_object.unsaved_property = 2
pykson.Pykson().to_json(my_object)
免责声明:我是pykson库的开发人员。
解决方案2:
第二种解决方案是使用带有自定义默认解串器的包装器类。
class ObjectWrapper:
def __init__(self,value,should_serialize=False)
self.value = value
self.should_serialize = should_serialize
def default_handler(obj):
if isinstance(obj,ObjectWrapper):
if obj.should_serialize:
return obj.value
else:
return None
else:
raise TypeError
json.dump(default=default_handler)
解决方案3:
这可能不是一个好主意,但是如果您的层次结构较深,则还可以向allc类添加一个函数,该函数将被序列化,并使用此函数获取字典并将字典轻松转换为json。
class MyChildClass:
def __init__(self,serialized_property,not_serialized_property):
self.serialized_property = serialized_property
self.not_serialized_property = not_serialized_property
def to_dict(self):
# only add serialized property here
return {
"serialized_property": self.serialized_property
}
class MyParentClass:
def __init__(self,child_property,some_other_property):
self.child_property = child_property
self.some_other_property = some_other_property
def to_dict(self):
return {
'child_property': self.child_property.to_dict(),'some_other_property': self.some_other_property
}
my_child_object = MyChildClass(serialized_property=1,not_serialized_property=2)
my_parent_object = MyParentClass(child_property=my_child_object,some_other_property='some string here')
json.dumps(my_parent_object.to_dict())
或者您可以使用默认处理程序获得相同的结果:
class MyChildClass:
def __init__(self,some_other_property):
self.child_property = child_property
self.some_other_property = some_other_property
def to_dict(self):
return {
'child_property': self.child_property,'some_other_property': self.some_other_property
}
def handle_default(obj):
if isinstance(obj,MyChildClass):
return obj.to_dict()
elif isinstance(obj,MyParentClass):
return obj.to_dict()
return None
my_child_object = MyChildClass(serialized_property=1,some_other_property='some string here')
json.dumps(my_parent_object,default=handle_default)
,
我是OP。为了清楚起见,我在这里发布了我最终用于案例的内容。
我在此主题中将@Sina Rezaei的帖子标记为“接受的解决方案”,因为那(他的帖子的最后一节)和@snakechamerb的评论激发了我理解要求的内容。
我的解决方案的轮廓如下:
class ModelScene(QGraphicsScene):
# Serialize whole scene to JSON into stream
def json_serialize(self,stream) -> None:
# Get `json.dump()` to call `ModelScene.json_serialize_dump_obj()` on every object to be serialized
json.dump(self,stream,indent=4,default=ModelScene.json_serialize_dump_obj)
# Static method to be called from `json.dump(default=ModelScene.json_serialize_dump_obj)`
# This method is called on every object to be dumped/serialized
@staticmethod
def json_serialize_dump_obj(obj):
# if object has a `json_dump_obj()` method call that...
if hasattr(obj,"json_dump_obj"):
return obj.json_dump_obj()
# ...else just allow the default JSON serialization
return obj
# Return dict object suitable for serialization via JSON.dump()
# This one is in `ModelScene(QGraphicsScene)` class
def json_dump_obj(self) -> dict:
return {
"_classname_": self.__class__.__name__,"node_data": self.node_data
}
class CanvasModelData(QAbstractListModel):
# Return dict object suitable for serialization via JSON.dump()
# This one is class CanvasModelData(QAbstractListModel)
def json_dump_obj(self) -> dict:
_data = {}
for key,value in self._data.items():
_data[key] = value
return {
"_classname_": self.__class__.__name__,"data_type": self.data_type,"_data": _data
}
- 每个“复杂”类都定义一个
def json_dump_obj(self) -> dict:
方法。
- 该方法仅返回序列化所需的属性/子对象。
- 顶级
json.dump(self,default=ModelScene.json_serialize_dump_obj)
使得通过静态方法ModelScene.json_serialize_dump_obj
将访问的每个节点逐步序列化为流。这会调用我的obj.json_dump_obj()
(如果有的话),否则会调用基本对象类型的默认JSON序列化。
有趣的是,我遇到了一个和我一样担心的人。从 python中的json.dump()和json.dumps()有什么区别?,解决方案https://stackoverflow.com/a/57087055/489865:
内存使用率和速度。
致电jsonstr = json.dumps(mydata)
时,它将首先创建一个完整的
将数据复制到内存中,然后file.write(jsonstr)
到磁盘。因此,这是一种更快的方法,但是如果您有
要保存的大量数据。
致电json.dump(mydata,file)
时,如果没有's'
,则将
未使用,因为数据是按块转储的。但是整个过程是
慢大约2倍。
源:我检查了json.dump()
和json.dumps()
的源代码,并
还用time.time()
和
观看htop中的内存使用情况。
本文链接:https://www.f2er.com/3008231.html