OrenIshShalom
OrenIshShalom

Reputation: 7162

serializing to JSON an array of data classes

I have an array of data classes I need to serialize to JSON. I wrap the list in a class (Persons)

@attrs.frozen
class Person:
    name: str
    age: int
    grades: Dict[str, int]

@attrs.define
class Persons:
    persons: List[Person] = attrs.field(factory=list)
    def add(self, person: Person) -> None:
        self.persons.append(person)

def run(filename: str) -> None:

    college = Persons()
    college.add(Person("uzi" , 51, {"math": 100, "cs": 90}))
    college.add(Person("dave", 76, {"math": 94, "music": 92}))
    college.add(Person("dan",  22, {"bible": 98}))

    with open(filename, "w+") as fl:
        fl.write(f"{json.dumps(attrs.asdict(college), indent=4)}\n")

Can I somehow get rid of the wrapping class and still serialize a List[Person] easily?

Upvotes: 2

Views: 2018

Answers (2)

Wizard.Ritvik
Wizard.Ritvik

Reputation: 11662

For a high level approach with dataclasses, I recommend checking out the dataclass-wizard library.

It provides a few generic and useful implementations, such as a Container type, which is just a convenience wrapper around a list type in Python.

It also exposes useful mixin classes which make it easier to work with YAML/JSON files, as shown below.

from dataclasses import dataclass
from dataclass_wizard import Container, JSONListWizard, JSONFileWizard


# optional: extend from `JSONListWizard`, so de-serializing a list results
#   in a `Container` object; though you could also use `JSONWizard` instead.
#   also extend from `JSONFileWizard`, so we can de-serialize data back from
#   a JSON file.

@dataclass(frozen=True)
class Person(JSONListWizard, JSONFileWizard):
    name: str
    age: int
    grades: dict[str, int]


def run(filename: str) -> None:
    # create a `Container` object, which is just a wrapper around a Python `list`
    college = Container[Person]()

    college.append(Person("uzi", 51, {"math": 100, "cs": 90}))
    college.append(Person("dave", 76, {"math": 94, "music": 92}))
    college.append(Person("dan", 22, {"bible": 98}))

    # serialize the list of instances to a json file
    college.to_json_file(filename, indent=4)


run('my_file.json')

# now, read it back :-)
people = Person.from_json_file('my_file.json')

The contents of my_file.json would look like:

[
    {
        "name": "uzi",
        "age": 51,
        "grades": {
            "math": 100,
            "cs": 90
        }
    },
    {
        "name": "dave",
        "age": 76,
        "grades": {
            "math": 94,
            "music": 92
        }
    },
    {
        "name": "dan",
        "age": 22,
        "grades": {
            "bible": 98
        }
    }
]

Upvotes: 0

python_user
python_user

Reputation: 7113

You can use default argument of the json.dumps function and pass the function attrs.asdict to serialize it.

I have added the "persons" key so it matches the output of your code.

import json
import attrs


@attrs.frozen
class Person:
    name: str
    age: int
    grades: dict[str, int]


def run(filename: str) -> None:

    college = []
    college.append(Person("uzi", 51, {"math": 100, "cs": 90}))
    college.append(Person("dave", 76, {"math": 94, "music": 92}))
    college.append(Person("dan", 22, {"bible": 98}))

    with open(filename, "w+") as fl:
        fl.write(
            f"{json.dumps({'persons': college}, default=attrs.asdict, indent=4)}\n"
        )


run("my_json.json")

Output

{
    "persons": [
        {
            "name": "uzi",
            "age": 51,
            "grades": {
                "math": 100,
                "cs": 90
            }
        },
        {
            "name": "dave",
            "age": 76,
            "grades": {
                "math": 94,
                "music": 92
            }
        },
        {
            "name": "dan",
            "age": 22,
            "grades": {
                "bible": 98
            }
        }
    ]
}

Upvotes: 3

Related Questions