A simple object serialization library
Project description
obj2bin
A simple object serialization library. Define the data structure with classes and let obj2bin deal with the serialization.
import binascii
from obj2bin import Const, Field, pack, encode, decode, utf8tobytes, utf8frombytes, utf8size
@pack(
packet_id=Const(1, "B"),
user_id=Field(">H"),
name_size=Field("B", meta=True),
name=Field("<{name_size}s", enc=utf8tobytes, dec=utf8frombytes)
)
class User:
user_id: int
name: str
@property
def name_size(self) -> int: return utf8size(self.name)
user = User(13, "John")
print(user)
buff, size = encode(user)
print(binascii.hexlify(buff).decode("utf-8"), size)
print(*decode(User, buff))
# output:
# User(user_id=13, name='John')
# 01000d044a6f686e 8
# User(user_id=13, name='John') 8
Note: Refer to example.py for a more complete demonstration.
Python's struct module is used to serialize data to bytes. How each object attribute is serialized is determined by the fmt parameter which expects a struct format string.
The order the fields will be encoded is determined by the order of the keyword arguments passed to the @pack() decorator. Because of this, dictionary insertion order must be maintained and, therefore, obj2bin requires Python >= 3.7. This requirement is planned to be removed in the future.
Field types
Field
A field with a dynamic value. The value to be serialized is taken from the object's attribute whose name matches the keyword entry name.
Const
A field with a constant value. If, when being decoded, the value does not match the one specified in Const an exception will be raised. Useful for defining attributes which identify a packet (e.g. packet type identifier or version).
Child
A field which serializes another object. Multiple objects can be specified for the same field.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file obj2bin-0.0.3.tar.gz.
File metadata
- Download URL: obj2bin-0.0.3.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4862320c18654d817ddc32f79ac8e360fcac3c307fb7863355204de63972b9f4
|
|
| MD5 |
f1549c17d15bc513751f170ee88c068c
|
|
| BLAKE2b-256 |
b5ccbc73dc5574c44cc98ebdb9aff4c6c0298e7e74d059e343e0ac85e8592dea
|
File details
Details for the file obj2bin-0.0.3-py3-none-any.whl.
File metadata
- Download URL: obj2bin-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54a3abbad2574e1c6fcd76766e137cb485829d31ce867bbf26e6c2093ab2b848
|
|
| MD5 |
c19531ecb1afd0542cbb8c9306a9f48d
|
|
| BLAKE2b-256 |
2ae6c0169e0e5d9aefdcd9b997443e92ed796eb5971da0127ec25b7767a6d563
|