Serialization

Vorte provides a high-performance serialization layer powered by a native Rust engine with automatic fallback to orjson and the Python standard library. It supports multiple formats and lazy schema generation for zero-overhead startup.

FastSerializer

FastSerializer is the core serialization engine. It automatically selects the fastest available backend in this priority order:

BackendPerformanceRequirements
Rust native (vorte_serial)FastestRust 1.75+ compiled extension
orjsonVery fastpip install orjson
stdlib jsonBaselineBuilt into Python
example.py
python
Copied!
1from vorte.serialization import FastSerializer
2
3serializer = FastSerializer()
4
5data = {"name": "Alice", "scores": [98, 87, 95]}
6
7# Serialize to JSON bytes
8encoded = serializer.encode(data)
9
10# Deserialize from bytes
11decoded = serializer.decode(encoded)
12
13# Check which backend is active
14print(serializer.backend) # "rust" | "orjson" | "stdlib"

Multi-Format Support

Beyond JSON, Vorte supports binary serialization formats for high-throughput and low-latency scenarios.

JSON

example.py
python
Copied!
1from vorte.serialization import FastSerializer
2
3serializer = FastSerializer(format="json")
4
5encoded = serializer.encode({"key": "value"})
6# b'{"key":"value"}'

MessagePack

example.py
python
Copied!
1from vorte.serialization import FastSerializer
2
3serializer = FastSerializer(format="msgpack")
4
5data = {"user_id": 42, "action": "click", "timestamp": 1700000000}
6encoded = serializer.encode(data)
7decoded = serializer.decode(encoded)

CBOR

example.py
python
Copied!
1from vorte.serialization import FastSerializer
2
3serializer = FastSerializer(format="cbor")
4
5data = {"binary_field": b"\x00\x01\x02", "nested": {"a": 1}}
6encoded = serializer.encode(data)
7decoded = serializer.decode(encoded)

Protobuf

example.py
python
Copied!
1from vorte.serialization import FastSerializer
2
3serializer = FastSerializer(
4 format="protobuf",
5 proto_definition="protos/user.proto",
6 message_type="User",
7)
8
9encoded = serializer.encode({
10 "id": 1,
11 "name": "Alice",
12 "email": "alice@example.com",
13})
14decoded = serializer.decode(encoded)

Format Selection Table

FormatBinarySchemaBest For
JSONNoNot requiredPublic APIs, web clients, debugging
MsgPackYesNot requiredInternal microservices, high throughput
CBORYesNot requiredIoT, constrained environments, binary data
ProtobufYesRequired (.proto)Contract-first APIs, cross-language systems

Lazy Schema Generation

lazy_schema defers Pydantic model serialization schema generation until the first request. This dramatically reduces startup time for applications with many models.

userresponse.py
python
Copied!
1from vorte.serialization import FastSerializer, lazy_schema
2
3serializer = FastSerializer(
4 lazy_schemas=True,
5)
6
7# Schemas are not built at import time
8# They are generated on first encode/decode call per model
9
10@lazy_schema
11class UserResponse:
12 id: int
13 name: str
14 email: str
15 created_at: datetime
16
17# First call triggers schema generation
18encoded = serializer.encode(UserResponse(id=1, name="Alice", email="alice@example.com", created_at=datetime.now()))
19
20# Subsequent calls reuse the cached schema
21encoded2 = serializer.encode(UserResponse(id=2, name="Bob", email="bob@example.com", created_at=datetime.now()))

Custom Serializers

Register custom encode/decode logic for specific types.

encode_datetime.py
python
Copied!
1from vorte.serialization import FastSerializer
2from datetime import datetime
3
4serializer = FastSerializer()
5
6@serializer.register_encoder(datetime)
7def encode_datetime(value: datetime) -> str:
8 return value.isoformat()
9
10@serializer.register_decoder("datetime")
11def decode_datetime(value: str) -> datetime:
12 return datetime.fromisoformat(value)

Content Negotiation

Vorte automatically selects the serialization format based on the request Accept header.

example.py
python
Copied!
1from vorte import Vorte
2
3app = Vorte(
4 auto_load=True,
5 config={
6 "serialization": {
7 "negotiate_format": True,
8 "default_format": "json",
9 "supported_formats": ["json", "msgpack", "cbor"],
10 }
11 },
12)
13
14# Accept: application/json -> JSON response
15# Accept: application/msgpack -> MsgPack response
16# Accept: application/cbor -> CBOR response

Performance Benchmarks

Serialization benchmarks for a typical API response payload (1 KB, 20 fields, nested objects):

BackendEncode (ops/sec)Decode (ops/sec)
Rust native850,000720,000
orjson620,000510,000
stdlib json180,000150,000
Stay in the loop

Get Vorte release notes, module guides, and developer deep-dives. No spam — unsubscribe anytime.