Summary
I have a Python object hierarchy I want to serialize using JSON (just via https://docs.python.org/3/library/json.html, not using any extra third-party library). I want to exclude certain fields/properties/sub-objects. I'm finding it surprisingly difficult to find a simple answer as to how to achieve this?
Example
I'll have a derived class instance ending up like this:
class MyItemClass(BaseItemClass):
self.saveThisProperty = 999
self.dontSaveThisProperty = "Something"
self.saveThisObject = ObjectType1()
self.dontSaveThisObject = ObjectType2()
If I were serializing to XML, I would want it to look like
<MyItemClass>
<saveThisProperty>999</saveThisProperty>
<saveThisObject>
...
</saveThisObject>
</MyItemClass>
Note that I only serialize certain properties/sub-objects, and I do not want to serialize the whole BaseItemClass
from which my class instance is derived.
In XML I'm fine. I know how to output bits of XML as I go along for what I do want, either to a temporary in-memory document which I save at the end or by outputting individual nodes/elements to the stream incrementally. I don't have to serialize everything. E.g.
xmlStream.writeStartElement("MyItemClass")
xmlStream.writeElementWithValue("saveThisProperty", 999)
xmlStream.writeStartElement("saveThisObject")
...
xmlStream.writeEndElement("saveThisObject")
xmlStream.writeEndElement("MyItemClass")
For JSON I can't do this, can I? Do I have to create some new, "standalone" object hierarchy (with no derivations from BaseClass
) by copying just the properties/sub-objects I want into it and then JSON serialize that?
I did see there is json.dump(default = ...)
, but that says:
If specified, default should be a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object
However, it is not that the original objects cannot be serialized by default Python->JSON, it is that I do not want that default, serialize-everything behaviour, I want my "selective" one.