How to make a class JSON serializable How to make a class JSON serializable python python

How to make a class JSON serializable


Here is a simple solution for a simple feature:

.toJSON() Method

Instead of a JSON serializable class, implement a serializer method:

import jsonclass Object:    def toJSON(self):        return json.dumps(self, default=lambda o: o.__dict__,             sort_keys=True, indent=4)

So you just call it to serialize:

me = Object()me.name = "Onur"me.age = 35me.dog = Object()me.dog.name = "Apollo"print(me.toJSON())

will output:

{    "age": 35,    "dog": {        "name": "Apollo"    },    "name": "Onur"}


Do you have an idea about the expected output? For example, will this do?

>>> f  = FileItem("/foo/bar")>>> magic(f)'{"fname": "/foo/bar"}'

In that case you can merely call json.dumps(f.__dict__).

If you want more customized output then you will have to subclass JSONEncoder and implement your own custom serialization.

For a trivial example, see below.

>>> from json import JSONEncoder>>> class MyEncoder(JSONEncoder):        def default(self, o):            return o.__dict__    >>> MyEncoder().encode(f)'{"fname": "/foo/bar"}'

Then you pass this class into the json.dumps() method as cls kwarg:

json.dumps(cls=MyEncoder)

If you also want to decode then you'll have to supply a custom object_hook to the JSONDecoder class. For example:

>>> def from_json(json_object):        if 'fname' in json_object:            return FileItem(json_object['fname'])>>> f = JSONDecoder(object_hook = from_json).decode('{"fname": "/foo/bar"}')>>> f<__main__.FileItem object at 0x9337fac>>>> 


For more complex classes you could consider the tool jsonpickle:

jsonpickle is a Python library for serialization and deserialization of complex Python objects to and from JSON.

The standard Python libraries for encoding Python into JSON, such as the stdlib’s json, simplejson, and demjson, can only handle Python primitives that have a direct JSON equivalent (e.g. dicts, lists, strings, ints, etc.). jsonpickle builds on top of these libraries and allows more complex data structures to be serialized to JSON. jsonpickle is highly configurable and extendable–allowing the user to choose the JSON backend and add additional backends.

(link to jsonpickle on PyPi)