Convert / return class object to JSON format in Python

A note on how to encode an object of a class defined by yourself in Python and how to decode it from Json format. Referenced page:

Example

Take this class as an example.

import json
from datetime import date

class MyA:
    def __init__(self, i=0, d=date.today()):
        self.i = i   #int type
        self.d = d   #date type

Json data

First of all, we have to think about the format of the object as Json data. It seems natural for the MyA object to be an object with 'i' and'd' as keys. Also, date type data may be a character string, but let's quickly convert it to an integer with the toordinal method. Then, for example

a = MyA(i=100, d=date(year=2345, month=6, day=12))

The a defined as is converted to the Json data {"i ": 100," d ": 856291}. This may be fine, but it can be tedious because you don't know what type to revert to when decoding. Therefore, the class name is stored in the key '_type', and the value is stored in the key 'value'. As a result, the Json data looks like this:

{ "_type": "MyA", 
  "value": {"i": 100, 
            "d": { "_type": "date",
                   "value": 856291}}}

Encode

The dump and dumps commands in Python's json library take an argument called cls. You can specify your own encoder here. Since JSONEncoder is a commonly used class, I make my own encoder based on this. What you really need to do is redefine a method called default.

class MyEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, date):
            return {'_type': 'date', 'value': o.toordinal()}
        if isinstance(o, MyA):
            return {'_type': 'MyA', 'value': o.__dict__}
        return json.JSONEncoder.default(self, o)

As you can see, you only have to write what each class is looking at directly, and the library will do the recursion for you. (In the case of MyA class object, what is returned is a dictionary, and date type data may be set there.)

It can be executed as follows.

    a = MyA(100, date(year=2345, month=6, day=12))
    js = json.dumps(a, cls=MyEncoder)
    print(js)

Execution result:

{"_type": "MyA", "value": {"i": 100, "d": {"_type": "date", "value": 856291}}}

Decode

This also creates a decoder based on a class called JSONDecoder. This time, when the object of the parent class JSONDecoder is created, write it so that the necessary decoding is performed in the function passed to the variable called object_hook. It looks like this:

class MyDecoder(json.JSONDecoder):
    def __init__(self, *args, **kwargs):
        json.JSONDecoder.__init__(self, object_hook=self.object_hook,
                                  *args, **kwargs)

    def object_hook(self, o):
        if '_type' not in o:
            return o
        type = o['_type']
        if type == 'date':
            return date.fromordinal(o['value'])
        elif type == 'MyA':
            return MyA(**o['value'])

As o of object_hook, the result of normal decoding is passed. In the above example, a dictionary with '_type' and 'value' as keys will come, so you can write the code to recreate it as an object of the appropriate class. Again, you don't have to think about recursive processing.

Following the execution at the time of encoding:

    b = json.loads(js, cls=MyDecoder)
    print(b.i, b.d)

Execution result:

100 2345-06-12

Recommended Posts

Convert / return class object to JSON format in Python
Convert Tweepy Status object to JSON
Convert markdown to PDF in Python
Determine the date and time format in Python and convert to Unixtime
[Introduction to Python] How to use class in Python?
Create a JSON object mapper in Python
How to use __slots__ in Python class
Convert Python date types to RFC822 format
format in python
How to convert DateTimeField format in Django
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
Convert XML document stored in XML database (BaseX) to CSV format (using Python)
I made a script in python to convert .md files to Scrapbox format
Try to extract specific data from JSON format data in object storage Cloudian/S3
Convert FBX files to ASCII <-> BINARY in Python
Convert PDFs to images in bulk with Python
How to generate a Python object from JSON
[Introduction to Python] How to handle JSON format data
Difference in object ID due to import in Python
Convert json format data to txt (using yolo)
Convert exponential notation float to str in Python
Convert cubic mesh code to WKT in Python
Convert strings to character-by-character list format with python
Handling json in python
Image format in Python
Object oriented in python
Convert json to excel
Class notation in Python
I tried to create a class that can easily serialize Json in Python
Convert timezoned date and time to Unixtime in Python2.7
I want to write in Python! (1) Code format check
How to convert / restore a string with [] in python
[Python] Convert general-purpose container and class to each other
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Convert CIDR notation netmask to dotted decimal notation in Python
How to convert a class object to a dictionary with SQLAlchemy
[Road to Python Intermediate] Define __getattr__ function in class
How to convert floating point numbers to binary numbers in Python
Convert the image in .zip to PDF with Python
How to convert JSON file to CSV file with Python Pandas
Output log in JSON format with Python standard logging
Convert callback-style asynchronous API to async / await in Python
[Road to intermediate Python] Define in in your own class
[Python] Created a method to convert radix in 1 second
How to convert Json file to CSV format or EXCEL format
Convert Webpay Entity type to Dict type (recursively in Python)
Try writing JSON format data to object storage Cloudian/S3
Convert Pascal VOC format xml file to COCO format json file
How to handle JSON in Ruby, Python, JavaScript, PHP
Use slackbot as a relay and return from bottle to slack in json format.
[python] Convert date to string
To flush stdout in Python
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
String object methods in Python
Null object comparison in Python
Login to website in Python
Parse JSON file to object
[Python] Convert Shift_JIS to UTF-8
Convert CIDR notation in Python