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.0.tar.gz
(8.6 kB
view hashes)
Built Distribution
Close
Hashes for JceStruct-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 513c1d2a431e84303966ecb9a06347585a7a5ca77198f37ca1cfec0d4c7e2d5f |
|
MD5 | 08027d25960ffece3382625a561dd9fc |
|
BLAKE2b-256 | f2516880720cb736394e10ed75aa9a1c987cc3afdf6218f08a58c69570487a6e |