Serial
The `serial` library provides a framework for simple and powerful serialization and deserialization of Python class instances to and from various data formats like JSON, YAML, and XML. It leverages schema definitions for robust type handling and validation. The current version is 0.0.97, and it maintains an active development pace, though it is still in a pre-1.0 state.
Common errors
-
ModuleNotFoundError: No module named 'serial'
cause You are trying to import the `serial` library, but it has not been installed in your current Python environment.fixInstall the library using pip: `pip install serial` -
AttributeError: module 'serial' has no attribute 'Serial'
cause This error often occurs when users confuse this `serial` library (for data serialization) with `pyserial` (for serial port communication). `pyserial` provides a class named `Serial`, whereas this `serial` library provides `Serializer` and `Deserializer`.fixIf you intend to use *this* data serialization library, import `from serial import Serializer` or `from serial import Deserializer`. If you intend to use `pyserial` for serial port communication, ensure `pyserial` is installed (`pip install pyserial`) and then use `import serial` (note the import name for `pyserial` is still `serial`). -
serial.exceptions.ValidationError: Missing required fields: ['name']
cause Your input data is missing one or more fields that are marked as `required=True` in your defined schema.fixEnsure that all fields marked as required in your `serial.fields` schema are present in the dictionary you are attempting to serialize or deserialize. For example, if 'name' is required, `{'id': 1}` will fail, but `{'id': 1, 'name': 'John'}` will pass.
Warnings
- gotcha This `serial` library for data serialization is distinct from `pyserial` (often imported as `serial`), which is used for serial port communication. Users frequently confuse the two due to the similar package name.
- breaking The library is in a pre-1.0 state (version 0.0.x), which means breaking changes can occur between minor or even patch versions without adhering to strict semantic versioning.
- gotcha Schema validation errors, such as missing required fields or type mismatches, are raised as `serial.exceptions.ValidationError` exceptions.
Install
-
pip install serial
Imports
- Serializer
from serial.serializer import Serializer
from serial import Serializer
- Deserializer
from serial.deserializer import Deserializer
from serial import Deserializer
- fields
from serial.fields import *
from serial import fields
Quickstart
from serial import Serializer, Deserializer, fields
import json
# Define a schema for a simple User object
user_schema = {
"id": fields.Integer(required=True),
"name": fields.String(required=True),
"email": fields.Email(required=False),
"is_active": fields.Boolean(default=True)
}
# Create a serializer instance for JSON
user_serializer = Serializer(user_schema, format="json")
# Create a deserializer instance for JSON
user_deserializer = Deserializer(user_schema, format="json")
# Example data to serialize
user_data = {"id": 1, "name": "Alice Wonderland", "email": "alice@example.com"}
# Serialize the data
serialized_output = user_serializer.serialize(user_data)
print(f"Serialized JSON: {serialized_output}")
# Deserialize the data
deserialized_object = user_deserializer.deserialize(serialized_output)
print(f"Deserialized Object: {deserialized_object}")