The method of converting data class to JSON in Python

dataclass to JSON in Python

JavaScript Object Notation or JSON represents the use of script (executable) files composed of text in a programming language to store and transmit data.

Python supports JSON through the JSON built-in module. Therefore, we import the JSON package in our Python scripts to take advantage of this capability.

Quoted strings are used in JSON to enclose values ​​in key-value maps. It is similar to Python's dictionary.

Python natively supports JSON features, and the JSON display API is similar to that of users of the marshal and pickle modules in the standard library.

The dataclass, on the other hand, creates data transfer objects in which to store data; these objects require appropriate defined methods for equivalence comparisons and sometimes for display.

dataclass is used to create methods and short syntax for data transfer classes.

implements dataclass, as a dictionary

Python 3.7 and later are the only versions that support the dataclass decorator. It produces an object, often called a data transfer object, whose sole function is to store data.

The problem is that to provide the correct functionality for these objects, methods for equality comparison, display, etc. must be created.

The development of these methods must consume a lot of human and material resources, and is prone to error. A dataclass that generates all these methods for you, giving data transfer classes a short syntax.

It uses a slightly altered (and somewhat more efficient) dataclasses.asdict for serialization.

You're iterating over the dataclass fields, and when deserializing JSON to a dataclass instance for the first time, create a resolver for each annotated type, making the process more efficient when repeated.

Create a dataclass for each JSONroot node

Since the "users" field is an array of objects with "id" and "name", we can see that we need to build two classes: "Test" and "User".

Sample code:

from dataclasses import dataclass
from typing import List
@dataclass
class User:
    id: 1
    name: "Kelvin"
@dataclass
class Test:
    id: 2
    userid:" Jack"
users: List[User]

Every JSON property should be mapped to a type-safe Python property.

The code below maps each JSON node and attribute into a Python class and attribute. To do this, we create a static method in the Python class responsible for mapping our dictionary to your Python attributes.

Sample code:

from typing import List
from dataclasses import dataclass, asdict, field
from json import dumps
@dataclass
#Python小白学习交流群:153708845
class Students:
    id: 1
    name: "stu1"
    @property
    def __dict__(self):
        return asdict(self)
    @property
    def json(self):
        return dumps(self.__dict__)
test_object_1 = Students(id=1, name="Kelvin")
print(test_object_1.json)

output:

{
    
    "id": 1, "name": "Kelvin"}

Remember that Data Transfer Objects are made up of dataclasses, which are used to store data in them. Therefore, these objects require correct method definitions for equality comparisons and occasionally for display.

The methods and syntax of the data transfer class are using dataclass.

Guess you like

Origin blog.csdn.net/qdPython/article/details/132153228