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
    "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)
} 

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

"boolExample" : {
    "type" : "bool"    # Set bool type
}

"listExample" : {
    "type" : "list",   # Set list type
    "subtype": "int",  # Set list contents type to an "int"
    "maxlen": 1        # The "maxlen" parameter for bytes or str (for "int" maxlen is still used instead of "byte")
}

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

Init

from proJSON import Crafter

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

crafter = Crafter(schema)

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.2.4.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

proJSON-1.2.4-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: proJSON-1.2.4.tar.gz
  • Upload date:
  • Size: 4.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.2.4.tar.gz
Algorithm Hash digest
SHA256 4bce6c899de7c63d660e991fd580737dec8703fa74e3b5bdcc3ab1bb8b6552be
MD5 8e2e74adc602dcad4d9e6b4e0ec70200
BLAKE2b-256 ac23a33d31687930896e303acdfa0bc02abb908dab09c917a345f2d4530ca56a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: proJSON-1.2.4-py3-none-any.whl
  • Upload date:
  • Size: 4.7 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.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 305316714fbce7833476d9e560c31a7ea3adf4bf53d31d9e1994855d3c4432c7
MD5 c6afa2e7c71a18ae17dc52d2357dce89
BLAKE2b-256 69665966bb7a8b918cac1b29cf6273ddd690855ac4662e2af800c55ef3df3130

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