Junior — Middle
What data types and properties can be implemented in data serialization classes?
sobes.tech AI
Answer from AI
In data serialization classes (for example, when using libraries like dataclasses, pydantic, or marshmallow in Python), the following data types and properties are usually implemented:
- Primitive types:
int,float,str,bool— basic types for storing numbers, strings, and boolean values. - Collections:
list,tuple,set,dict— for storing sequences and mappings. - Nested objects: other classes or structures that can also be serialized.
- Optional fields: fields that can be
Noneor absent. - Validation and transformation: properties with value checks, for example, through descriptors or validators.
Example using dataclasses and types from typing:
from dataclasses import dataclass
from typing import List, Optional
@dataclass
class User:
id: int
name: str
email: Optional[str] = None
tags: List[str] = None
user = User(id=1, name="Alice", tags=["admin", "user"])
Here, Optional[str] means that the email field can be a string or absent (None). The List[str] collection stores a list of tags. Such types help when serializing to JSON or other formats.