Skip to main content

A python library to compress a dict into a bytearray.

Project description

proJSON

A python library to compress a dict into a bytearray.

Tests

Install

pip install proJSON

The Crafter

Types

# Format

"name" : {
    "type": type,      # Types specified below with arguments
    ...
}

"intExample" : {
    "type": "int",     # Set int type
    "byte": 1          # Set max int size to 1 byte (int8)
} 

"stringExample" : {
    "type": "string",  # Set string type
    "maxlen": 1        # Set max size of string ( the max size of the string is calculated by (2^(8*n)-1). if n is 1 then the max size is 255) (optional defaults to 2)
} 

"bytesExample" : {
    "type": "bytes",   # Set bytes type
    "byte": 1          # Set max size of string ( the max size of the string is calculated by (2^(8*n)-1). if n is 1 then the max size is 255) (optional defaults to 2)
} 

"dirExample" : {
    "type": "dir",     # Set dir type
    "subdirs": {}      # A dictionary of int, string and byte types in the format above.
} 

Don't put a dir in another dir. It won't work

Init

from projson import Crafter

template = {
    # See the [types section](###Types) for how to make this.
    "intExample" : {
        "type": "int",
        "byte": 1
    },
    "stringExample" : {
        "type": "string",
        "maxlen": 1
    },
    "bytesExample" : {
        "type": "bytes",
        "byte": 1
    },
    "dirExample" : {
        "type": "dir",
        "subdirs": {
            "intExample2" : {
                "type": "int",
                "byte": 1
            },
            "stringExample2" : {
                "type": "string",
                "maxlen": 1
            },
        }
    } 
}

crafter = Crafter(template)

Encoding data

exampledata = {
    "intExample" : 7,
    "stringExample" : "Hello World!",
    "bytesExample": b"This is bytes",
    "dirExample" : {
        "intExample2" : 200,
        "stringExample2": "Hello Again!"
    }
}

encoded = crafter.encode(exampledata)

print(encoded) # b'\x07\x0cHello World!\x00\rThis is bytes\xc8\x0cHello Again!' (43 bytes)

Decoding data

data = b'\x07\x0cHello World!\x00\rThis is bytes\xc8\x0cHello Again!'

decoded = crafter.decode(data)

print(decoded) # {'intExample': 7, 'stringExample': 'Hello World!', 'bytesExample': b'This is bytes', 'dirExample': {'intExample2': 200, 'stringExample2': 'Hello Again!'}} (154 bytes)

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

proJSON-1.1.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

proJSON-1.1.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file proJSON-1.1.1.tar.gz.

File metadata

  • Download URL: proJSON-1.1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for proJSON-1.1.1.tar.gz
Algorithm Hash digest
SHA256 86b9e3fa82711da87ab1ecd57e136080c707a5399051a0340ad04fc91da43f3a
MD5 838783b7c85dc422069a78f419957330
BLAKE2b-256 28698e63947070b3718496a5cd73282ec265e290247cb508f1f6ec5351470dbf

See more details on using hashes here.

File details

Details for the file proJSON-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: proJSON-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for proJSON-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bd9cecf0351758d7352da6df0605365712857e90843e0ed9ad8ab933f3b28f49
MD5 649c2bb7701b25f002296dba1600aeea
BLAKE2b-256 896252034d842e7d318de29c00524be99bdadbca02c31d257d0149e8f3c0d709

See more details on using hashes here.

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