How to make a Python class serializable?
A simple class:
class FileItem:
def __init__(self, fname):
self.fname = fname
What should I do to be able to get output of:
json.dumps()
Without an error (FileItem instance at ... is not JSON serializable
)
This is a small library that serializes an object with all its children to JSON and also parses it back:
https://github.com/Toubs/PyJSONSerialization/
I like Onur's answer but would expand to include an optional
toJSON()
method for objects to serialize themselves:There are many approaches to this problem. 'ObjDict' (pip install objdict) is another. There is an emphasis on providing javascript like objects which can also act like dictionaries to best handle data loaded from JSON, but there are other features which can be useful as well. This provides another alternative solution to the original problem.
If you don't mind installing a package for it, you can use json-tricks:
After that you just need to import
dump(s)
fromjson_tricks
instead of json, and it'll usually work:which'll give
And that's basically it!
This will work great in general. There are some exceptions, e.g. if special things happen in
__new__
, or more metaclass magic is going on.Obviously loading also works (otherwise what's the point):
This does assume that
module_name.test_class.MyTestCls
can be imported and hasn't changed in non-compatible ways. You'll get back an instance, not some dictionary or something, and it should be an identical copy to the one you dumped.If you want to customize how something gets (de)serialized, you can add special methods to your class, like so:
which serializes only part of the attributes parameters, as an example.
And as a free bonus, you get (de)serialization of numpy arrays, date & times, ordered maps, as well as the ability to include comments in json.
Disclaimer: I created json_tricks, because I had the same problem as you.
jaraco gave a pretty neat answer. I needed to fix some minor things, but this works:
Code
Note that we need two steps for loading. For now, the
__python__
property is not used.How common is this?
Using the method of AlJohri, I check popularity of approaches:
Serialization (Python -> JSON):
to_json
: 266,595 on 2018-06-27toJSON
: 96,307 on 2018-06-27__json__
: 8,504 on 2018-06-27for_json
: 6,937 on 2018-06-27Deserialization (JSON -> Python):
from_json
: 226,101 on 2018-06-27