JCE Encode/Decode
Project description
Jce Struct
Tencent JCE Encode/Decode with fully pydantic support
Installation
pip install JceStruct
or install from source (using poetry)
poetry add git+https://github.com/yanyongyu/JceStruct.git
or clone and install
git clone https://github.com/yanyongyu/JceStruct.git
cd JceStruct
poetry install
Usage
Create Struct
Create your struct by inheriting JceStruct
and define your fields with JceField
from jce import types, JceStruct, JceField
class ExampleStruct(JceStruct):
field1: types.INT32 = JceField(jce_id=1)
field2: float = JceField(jce_id=2, jce_type=types.DOUBLE) # define type in options
field3: types.LIST[OtherStruct] = JceField(jce_id=3) # nested struct supported
extra_pydantic_field: str = "extra_pydantic_field" # other field is optional
Encode/Decode
# simple encode decode
raw: ExampleStruct = ExampleStruct.decode(bytes, extra_pydantic_field="extra")
bytes = raw.encode()
# decode list from example struct
raw = OtherStruct.decode_list(bytes, jce_id=3, **extra)
Custom Encoder/Decoder
Just inherit JceEncoder/JceDecoder and add it to your struct configuration
from jce import JceStruct, JceEncoder
class CustomEncoder(JceEncoder):
pass
class ExampleStruct(JceStruct):
class Config:
jce_encoder = CustomEncoder
# jce_decoder = CustomDecoder
Custom types
Just inherit JceType and implement abstruct methods
from jce import types
class CustomType(types.JceType):
...
Change default types
By default, head bytes are treated like this:
{
0: BYTE,
1: INT16,
2: INT32,
3: INT64,
4: FLOAT,
5: DOUBLE,
6: STRING1,
7: STRING4,
8: MAP,
9: LIST,
10: STRUCT_START,
11: STRUCT_END,
12: ZERO_TAG,
13: BYTES
}
field will be converted to the type defined in struct when validate.
to change it:
class ExampleStruct(JceStruct):
class Config:
jce_default_type = {
# add all types here
}
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
JceStruct-0.1.1.tar.gz
(8.6 kB
view hashes)
Built Distribution
Close
Hashes for JceStruct-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 315a7e97692a183225ec28e77a571a1d11c08c2264079ac0c242bf959e4e96ff |
|
MD5 | 8ecdc85a9ed48bded9e890399fbef704 |
|
BLAKE2b-256 | 3f413c4507d6b5eef6e2e4eff42cf7bd8dd62d92169f6c662ea92e01540403c2 |