Home > database >  What's Python JSON serializable?
What's Python JSON serializable?

Time:12-29

I have two ways to represent Python object by json.dumps()

First:

person = {
"name": "John",
"age": 30,
"city": "New York"
}

Second:

class Person:
   def _init_(self, name, age, city):
      self.name = name
      self.age = age
      self.city = city

person = Person("John", 30, "New York")

Then I tried p1 = json.dumps(person), the second way would say it's not JSON serializable.

So basically for Python, json.dumps only work for built-in object like dict object?

CodePudding user response:

If you are asking about vanilla Python, serialization could be done this way:

import json


class Person:
   def __init__(self, name, age, city):
      self.name = name
      self.age = age
      self.city = city

   def to_json(self):
       return json.dumps(self, default=lambda o: o.__dict__)


person = Person("John", 30, "New York")
print(person.to_json())

So we're just converting an object to a dict using __dict__ attribute.

But if you need something more sophisticated, you might need DRF (Django REST Framework) or pydantic. An example how it could be done with DRF:

from rest_framework import serializers
from rest_framework.serializers import Serializer


class Person:
   def __init__(self, name, age, city):
      self.name = name
      self.age = age
      self.city = city


class PersonSerializer(Serializer):
    name = serializers.CharField()
    age = serializers.IntegerField()
    city = serializers.CharField()

    def create(self, validated_data):
        return Person(**validated_data)


person = Person("John", 30, "New York")
print(PersonSerializer(person).data)

This way you have a much better control over it. See docs.

CodePudding user response:

Yes, the json module only knows how to serialize certain built-in types. An easy way to work with classes that have "fields" is the dataclasses module.

Example:

from dataclasses import dataclass, asdict
import json

@dataclass
class Person:
    name: str
    age: int
    city: str

person = Person("John", 30, "New York")
print(json.dumps(asdict(person)))

The asdict() function converts the dataclass instance into a dict which json can serialize.

  • Related