jamcodec


Namejamcodec JSON
Version 0.1.4 PyPI version JSON
download
home_pageNone
SummaryPython JAM Codec Library
upload_time2025-10-15 14:28:38
maintainerNone
docs_urlNone
authorNone
requires_python<4,>=3.8
licenseNone
keywords jam codec
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python JAM Codec

[![Build Status](https://img.shields.io/github/actions/workflow/status/polkascan/py-scale-codec/unittests.yml?branch=master)](https://github.com/polkascan/py-scale-codec/actions/workflows/unittests.yml?query=workflow%3A%22Run+unit+tests%22)
[![Latest Version](https://img.shields.io/pypi/v/scalecodec.svg)](https://pypi.org/project/scalecodec/) 
[![Supported Python versions](https://img.shields.io/pypi/pyversions/scalecodec.svg)](https://pypi.org/project/scalecodec/)
[![License](https://img.shields.io/pypi/l/scalecodec.svg)](https://github.com/polkascan/py-scale-codec/blob/master/LICENSE)


## Description
Python implementation of the JAM codec as specified in the graypaper.


## Installation
```bash
pip install jamcodec
```

## Code examples

```python
from jamcodec.types import JamBytes, Bool, String, U32, U8, U16, Struct, Vec, VarInt64, Tuple, Enum

# encode a Vec<u16>
obj = Vec(U16).new()
value = [1, 2]
data = obj.encode(value)

# Define and decode a Struct
scale_obj = Struct(test=U8, test2=Tuple(U8, U8)).new()
value = scale_obj.decode(JamBytes("0x020105"))

# Define and encode an Enum
scale_obj = Enum(
    Bool=Bool(),
    Number=U32,
    Complex=Struct(data=String(), version=VarInt64),
    None_=None
).new()
value = {'Bool': True}

data = scale_obj.encode(value)
```

## Examples of different types

| Type                                                                         | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Example SCALE decoding value                                                | SCALE encoded value                                                             |
|------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|---------------------------------------------------------------------------------|
| `bool`                                                                       | Boolean values are encoded using the least significant bit of a single byte.                                                                                                                                                                                                                                                                                                                                                                                                         | `True`                                                                      | `0x01`                                                                          |
| `u16`                                                                        | Basic integers are encoded using a fixed-width little-endian (LE) format.                                                                                                                                                                                                                                                                                                                                                                                                            | `42`                                                                        | `0x2a00`                                                                        |
| `VarInt64`                                                                   | xxxxxxx                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | `0`                                                                         | `0x00`                                                                          |
|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `1`                                                                         | `0x04`                                                                          |
|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `42`                                                                        | `0xa8`                                                                          |
|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `69`                                                                        | `0x1501`                                                                        |
|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `100000000000000`                                                           | `0x0b00407a10f35a`                                                              |
| `Vec`                                                                        | A collection of same-typed values is encoded, prefixed with a compact encoding of the number of items, followed by each item's encoding concatenated in turn.                                                                                                                                                                                                                                                                                                                        | `[4, 8, 15, 16, 23, 42]`                                                    | `0x18040008000f00100017002a00`                                                  |
| `BitVec`                                                                     | A sequence of bools, represented in a more space efficient bit format                                                                                                                                                                                                                                                                                                                                                                                                                | `0b00000010_01111101`                                                    | `0x287d02`                                                  |
| `str`,`Bytes`, `String`                                                      | Strings are Vectors of bytes (`Vec<u8>`) containing a valid UTF8 sequence.                                                                                                                                                                                                                                                                                                                                                                                                           | `"Test"`                                                                    | `0x1054657374`                                                                  |
|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `b"Test"`                                                                   | `0x1054657374`                                                                  |
|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `[84, 101, 115, 116]`                                                       | `0x1054657374`                                                                  |
| `[u8; 4]`                                                                    | Fixed sized array of in this case an `u8`                                                                                                                                                                                                                                                                                                                                                                                                                                            | `b"babe"`                                                                   | `0x62616265`                                                                    |
|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `"0x62616265"`                                                              | `0x62616265`                                                                    |
|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `[98, 97, 98, 101]`                                                         | `0x62616265`                                                                    |
| `Enum` Example: `enum IntOrBool { Int(u8), Bool(bool),}`                     | A fixed number of variants, each mutually exclusive and potentially implying a further value or series of values. Encoded as the first byte identifying the index of the variant that the value is. Any further bytes are used to encode any data that the variant implies. Thus, no more than 256 variants are supported.                                                                                                                                                           | `{'Int': 8}`                                                                | `0x002a`                                                                        |
|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `{'Bool': True}`                                                            | `0x0101`                                                                        |
| `Struct` Example: `struct Motion { pub votes: Vec<AccountId>, pub id: u32 }` | For structures, the values are named, but that is irrelevant for the encoding (names are ignored - only order matters). All containers store elements consecutively. The order of the elements is not fixed, depends on the container, and cannot be relied on at decoding. This implicitly means that decoding some byte-array into a specified structure that enforces an order and then re-encoding it could result in a different byte array than the original that was decoded. | `{"votes": ["5GDyPHLVHcQYPTWfygtPYeo gQjyZy7J9fsi4brPhgEFq4pcv"], "id": 4}` | `0x04b80269ec500e458a630846b99105c397ee57 4125823d6f4388e9c7572e115c0504000000` |


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "jamcodec",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.8",
    "maintainer_email": null,
    "keywords": "jam, codec",
    "author": null,
    "author_email": "JAMdot Technologies <dev@jamdot.tech>",
    "download_url": "https://files.pythonhosted.org/packages/83/6d/d623dd9eddc3ee95ab3970a61210bc7c891f199af21f9ce0c5eda1557c55/jamcodec-0.1.4.tar.gz",
    "platform": null,
    "description": "# Python JAM Codec\n\n[![Build Status](https://img.shields.io/github/actions/workflow/status/polkascan/py-scale-codec/unittests.yml?branch=master)](https://github.com/polkascan/py-scale-codec/actions/workflows/unittests.yml?query=workflow%3A%22Run+unit+tests%22)\n[![Latest Version](https://img.shields.io/pypi/v/scalecodec.svg)](https://pypi.org/project/scalecodec/) \n[![Supported Python versions](https://img.shields.io/pypi/pyversions/scalecodec.svg)](https://pypi.org/project/scalecodec/)\n[![License](https://img.shields.io/pypi/l/scalecodec.svg)](https://github.com/polkascan/py-scale-codec/blob/master/LICENSE)\n\n\n## Description\nPython implementation of the JAM codec as specified in the graypaper.\n\n\n## Installation\n```bash\npip install jamcodec\n```\n\n## Code examples\n\n```python\nfrom jamcodec.types import JamBytes, Bool, String, U32, U8, U16, Struct, Vec, VarInt64, Tuple, Enum\n\n# encode a Vec<u16>\nobj = Vec(U16).new()\nvalue = [1, 2]\ndata = obj.encode(value)\n\n# Define and decode a Struct\nscale_obj = Struct(test=U8, test2=Tuple(U8, U8)).new()\nvalue = scale_obj.decode(JamBytes(\"0x020105\"))\n\n# Define and encode an Enum\nscale_obj = Enum(\n    Bool=Bool(),\n    Number=U32,\n    Complex=Struct(data=String(), version=VarInt64),\n    None_=None\n).new()\nvalue = {'Bool': True}\n\ndata = scale_obj.encode(value)\n```\n\n## Examples of different types\n\n| Type                                                                         | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Example SCALE decoding value                                                | SCALE encoded value                                                             |\n|------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|---------------------------------------------------------------------------------|\n| `bool`                                                                       | Boolean values are encoded using the least significant bit of a single byte.                                                                                                                                                                                                                                                                                                                                                                                                         | `True`                                                                      | `0x01`                                                                          |\n| `u16`                                                                        | Basic integers are encoded using a fixed-width little-endian (LE) format.                                                                                                                                                                                                                                                                                                                                                                                                            | `42`                                                                        | `0x2a00`                                                                        |\n| `VarInt64`                                                                   | xxxxxxx                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | `0`                                                                         | `0x00`                                                                          |\n|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `1`                                                                         | `0x04`                                                                          |\n|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `42`                                                                        | `0xa8`                                                                          |\n|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `69`                                                                        | `0x1501`                                                                        |\n|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `100000000000000`                                                           | `0x0b00407a10f35a`                                                              |\n| `Vec`                                                                        | A collection of same-typed values is encoded, prefixed with a compact encoding of the number of items, followed by each item's encoding concatenated in turn.                                                                                                                                                                                                                                                                                                                        | `[4, 8, 15, 16, 23, 42]`                                                    | `0x18040008000f00100017002a00`                                                  |\n| `BitVec`                                                                     | A sequence of bools, represented in a more space efficient bit format                                                                                                                                                                                                                                                                                                                                                                                                                | `0b00000010_01111101`                                                    | `0x287d02`                                                  |\n| `str`,`Bytes`, `String`                                                      | Strings are Vectors of bytes (`Vec<u8>`) containing a valid UTF8 sequence.                                                                                                                                                                                                                                                                                                                                                                                                           | `\"Test\"`                                                                    | `0x1054657374`                                                                  |\n|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `b\"Test\"`                                                                   | `0x1054657374`                                                                  |\n|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `[84, 101, 115, 116]`                                                       | `0x1054657374`                                                                  |\n| `[u8; 4]`                                                                    | Fixed sized array of in this case an `u8`                                                                                                                                                                                                                                                                                                                                                                                                                                            | `b\"babe\"`                                                                   | `0x62616265`                                                                    |\n|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `\"0x62616265\"`                                                              | `0x62616265`                                                                    |\n|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `[98, 97, 98, 101]`                                                         | `0x62616265`                                                                    |\n| `Enum` Example: `enum IntOrBool { Int(u8), Bool(bool),}`                     | A fixed number of variants, each mutually exclusive and potentially implying a further value or series of values. Encoded as the first byte identifying the index of the variant that the value is. Any further bytes are used to encode any data that the variant implies. Thus, no more than 256 variants are supported.                                                                                                                                                           | `{'Int': 8}`                                                                | `0x002a`                                                                        |\n|                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | `{'Bool': True}`                                                            | `0x0101`                                                                        |\n| `Struct` Example: `struct Motion { pub votes: Vec<AccountId>, pub id: u32 }` | For structures, the values are named, but that is irrelevant for the encoding (names are ignored - only order matters). All containers store elements consecutively. The order of the elements is not fixed, depends on the container, and cannot be relied on at decoding. This implicitly means that decoding some byte-array into a specified structure that enforces an order and then re-encoding it could result in a different byte array than the original that was decoded. | `{\"votes\": [\"5GDyPHLVHcQYPTWfygtPYeo gQjyZy7J9fsi4brPhgEFq4pcv\"], \"id\": 4}` | `0x04b80269ec500e458a630846b99105c397ee57 4125823d6f4388e9c7572e115c0504000000` |\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python JAM Codec Library",
    "version": "0.1.4",
    "project_urls": {
        "Homepage": "https://github.com/JAMdotTech/py-jam-codec"
    },
    "split_keywords": [
        "jam",
        " codec"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "70346a38652dcda1d4ee0c43c84b8da143405b137aeeb8b55d5635ba5b8f1137",
                "md5": "f462286c5b5e7416c6d2878390e6d450",
                "sha256": "51132b6efbc57590b51dcb70f3f8dc591cb4db830a6db8e857ce72d8efec06d3"
            },
            "downloads": -1,
            "filename": "jamcodec-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f462286c5b5e7416c6d2878390e6d450",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.8",
            "size": 19938,
            "upload_time": "2025-10-15T14:28:37",
            "upload_time_iso_8601": "2025-10-15T14:28:37.203598Z",
            "url": "https://files.pythonhosted.org/packages/70/34/6a38652dcda1d4ee0c43c84b8da143405b137aeeb8b55d5635ba5b8f1137/jamcodec-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "836dd623dd9eddc3ee95ab3970a61210bc7c891f199af21f9ce0c5eda1557c55",
                "md5": "28b54eec7d30a6cf09b40066e5dcbfa6",
                "sha256": "0146d1b37ca4df61c72cc17d2350946b04fb019daad0439de5f13c39da3e91ce"
            },
            "downloads": -1,
            "filename": "jamcodec-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "28b54eec7d30a6cf09b40066e5dcbfa6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8",
            "size": 27192,
            "upload_time": "2025-10-15T14:28:38",
            "upload_time_iso_8601": "2025-10-15T14:28:38.538910Z",
            "url": "https://files.pythonhosted.org/packages/83/6d/d623dd9eddc3ee95ab3970a61210bc7c891f199af21f9ce0c5eda1557c55/jamcodec-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-15 14:28:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "JAMdotTech",
    "github_project": "py-jam-codec",
    "github_not_found": true,
    "lcname": "jamcodec"
}
        
Elapsed time: 2.43969s