Skip to main content

Simple, fast, and space-efficient (de)serialization for simple Python classes

Project description

Miniserial

Miniserial is a Python package for dead-simple, space-efficient serialization and deserialization of simple dataclasses. There are many great packages for general purpose serialization in the standard library and on PyPI (e.g. json, marshal, pickle, ujson, bson, etc.), but most use serialization formats that can come with significant byte overhead. Sometimes, project constraints—like the 256-byte-max-packet radio devices that inspired this package—encourage the use of a much more compact format, something akin to protobuf or the layout of C structs. But libraries in this space typically come with the overhead of implementing manual serializers, modifying class fields with various wrappers, or using non python data specifications (e.g. .proto files). msgpack comes close, but does not work out of the box with classes. Miniserial makes compact serialization easy. Simply have your dataclass inherit from the Serialization mixin, and serialize and deserialize methods will be automatically generated for the class. For example:

from dataclasses import dataclass
from miniserial import Serializable

@dataclass
class Person(Serializable):
    name   : str
    age    : int
    titles : list[str]
    balance: float
    
p = Person("Bob", 34, ["Mr.", "Dr.", "Professor"], 239847.25)
assert Person.deserialize(p.serialize()) == p

Classes that inherit the Serializable mixin must be dataclasses composed of fields annotated with supported types, which include any other class which inherits Serializable. This means that even recursive structures, like trees, can be serialized and deserialized.

from __future__ import annotations
from dataclasses import dataclass
from miniserial import Serializable

@dataclass
class Node(Serializable):
    value   : int
    children: list[Node]

#                 1
#               /   \ 
#              2     3 
#             / \
#            4   5
tree = Node(1, [Node(2, [Node(4, []), Node(5, [])]), Node(3, [])])
assert Node.deserialize(tree.serialize()) == tree

Documentation of supported types and the serialization format is on the way. For now, bool, int, float, str, and subclasses of collections.abc.Collection (e.g. list, set, tuple) are supported, along with any other user-defined class that inherits Serializable. int and float are taken to be 32 bit values. Support for more types, including int64, float64, etc. from numpy are on the horizon.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

miniserial-0.2.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

miniserial-0.2.3-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file miniserial-0.2.3.tar.gz.

File metadata

  • Download URL: miniserial-0.2.3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for miniserial-0.2.3.tar.gz
Algorithm Hash digest
SHA256 8915ae93b9df4d57924a6c907c46bf16cc1b0b2dc641bb80696b5da4e5467657
MD5 01c6547d18d906c7e6fefbf1057c1b32
BLAKE2b-256 93cb5470d08121fa45b2ec55471ef1b8de9d68eea61311efe19ba93b6f5a2741

See more details on using hashes here.

File details

Details for the file miniserial-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: miniserial-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for miniserial-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0120aa94109dea1515f1ab6b41aa817db92e95195237f82b0809eed8c9433be2
MD5 e96ea3691bc99ceb2eefe9f90889962b
BLAKE2b-256 f75e1ee519ef3691b1efe46cf177246dcec50779310732ef1de3a0c30b8c59be

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page