Skip to main content

Jce Struct

Tencent JCE Encode/Decode with fully pydantic support

Before Using

JceStruct is base on python type hint (doc) and Pydantic.

Read links above if first time using type hint or pydantic model.

Data validation and IDE type checking are all supported.

Installation

Install directly

pip install JceStruct
# or
poetry add JceStruct

or install from source (using poetry)

pip install git+https://github.com/yanyongyu/JceStruct.git
# or
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  # with editable mode

Usage

Create Struct

Create your struct by inheriting JceStruct and define your fields with JceField.

You can also combine your model fields with typing Optional, Union...

from jce import types, JceStruct, JceField


class ExampleStruct(JceStruct):
    # normal definition
    field1: types.INT32 = JceField(jce_id=1)
    # define type in options
    field2: float = JceField(jce_id=2, jce_type=types.DOUBLE)
    # define an optional field with default value
    field3: Optional[types.BOOL] = JceField(None, jce_id=3)
    # nested struct supported
    field4: types.LIST[OtherStruct] = JceField(types.LIST(), jce_id=4)
    # optional other pydantic field
    extra_pydantic_field: str = "extra_pydantic_field"

Encode/Decode

You can initialize a struct and encode it, or encode single field using to_bytes method.

# simple struct encode
example: ExampleStruct = ExampleStruct(
    field1=1, field2=2., field4=types.LIST[OtherStruct()]
)
bytes = example.encode()

# single field encode
bytes = types.STRING.to_bytes(jce_id=0, value="example")

You can decode bytes using decode classmethod of the struct, decode single field using from_bytes classmethod, or only get single list field using decode_list method of list inner struct.

# simple struct decode
example: ExampleStruct = ExampleStruct.decode(bytes, extra_pydantic_field="extra")

# single field decode
string, length = types.STRING.from_bytes(data=bytes, **extra)

# decode list from example struct
others: List[OtherStruct] = 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
        }

Command Line Usage

python -m jce 1f2e3d4c5b6a79

Metadata

Release files for jcestruct 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jcestruct 0.1.5
File Size Uploaded
JceStruct-0.1.5.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jcestruct 0.1.5
File Interpreter ABI Platform
JceStruct-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 19.0 kB

Release files / JceStruct-0.1.5.tar.gz

Download URL JceStruct-0.1.5.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c465d3ed91c64a0ca23b34c4f1fee66987d9fb7346934a4b160d3a5222ef7e0e
BLAKE2b-256 checksum
How to use checksums
d90157f77fffcd9e9999f3abd6e0457f03ea9970064f4ead0729edf20e086445
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.7 Linux/5.4.0-91-generic

Release files / JceStruct-0.1.5-py3-none-any.whl

Download URL JceStruct-0.1.5-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
638f847ac436522ff905bccbba53c1abede91ed0a08434fa0238742edd61fda8
BLAKE2b-256 checksum
How to use checksums
437ad7a96dbb972455fe33668f2789633db0147ad6bdaa5630a3028a38951cd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.7 Linux/5.4.0-91-generic

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page