Skip to main content

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)

Uploaded Source

Built Distribution

JceStruct-0.1.0-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

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