Serialization Deep Dive

May 30, 2026 | 5 min read

Custom Binary Protocol

def pack_ints(values: list[int]) -> bytes:
    result = bytearray()
    for v in values:
        result.append((v >> 24) & 0xff)
        result.append((v >> 16) & 0xff)
        result.append((v >> 8) & 0xff)
        result.append(v & 0xff)
    return bytes(result)

Struct Module

import struct

def pack_records(records: list[tuple[int, float]]) -> bytes:
    result = bytearray()
    for i, f in records:
        result.extend(struct.pack('If', i, f))
    return bytes(result)

Pickle Alternatives

For cross-language compatibility, prefer JSON or MessagePack. Pyvorin accelerates the Python-side encoding/decoding loops.