ASCii Serial Object Notation
Project description
ASCSON for Python
ASCII Serialized Object Notation
What?
This is basically a way to write binary data to text files with a structure similar to actual binary files. Also includes a system for writing formatted data in a way similar to NBT.
Why?!
I have always wondered what it would look like if binary flatfiles were encoded in a readable way, and if I could come up with something relatively efficient.
This is the result of that question, answered by a very sleep-deprived me on a cold Tuesday night in February 2023. I've since cleaned it up a bit.
Caveats
If you actually need to store data efficiently but in an ASCII format, I highly suggest looking elsewhere. Packing data to binary and then dumping to Base64 is probably way better than this burning dumpster fire.
Installing
pip install https://gitlab.com/N3X15/ascson.git
Usage
Encoding
import ascson
o: str = ascson.dumps({
'a': 1,
'b': 2.5,
'c': True,
'd': 'A longer string.'
})
print(o)
d4S1ai1S1bFGK4S1ctS1dSG1A longer string.
Decoding
import ascson
o = ascson.loads('d4S1ai1S1bFGK4S1ctS1dSG1A longer string.')
print(repr(o))
OrderedDict([('a', 1), ('b', 2.5), ('c', True), ('d', 'A longer string.')])
How it works
Let's break it up a bit.
# ints are encoded as 4-bit integers with a 5th bit,
# used to tell the parser whether to continue reading characters.
assert ascson.dumps(1) == "i1"
assert ascson.dumps(-1) == "i-1"
assert ascson.dumps(123) == "iR7"
# String constants are prefixed with 'S' and the 4-bit encoded length of the string in characters (NOT bytes).
assert ascson.dumps("") == "S0"
assert ascson.dumps("a") == "S1a"
# Floats are prefixed with 'F', converted to bytes (double-format for accuracy), read as a big-endian integer, encoded as an ascson integer.
assert ascson.dumps(1.5) == "FVJOF"
# True booleans are presented as the letter 't'
assert ascson.dumps(True) == "t"
# False booleans are presented as the letter 'f'
assert ascson.dumps(False) == "f"
# Dictionaries are prefixed with 'd' and the 4-bit encoded length of the dictionary.
# Each key-value pair then follows.
assert ascson.dumps(dict()) == "d0"
assert ascson.dumps(dict(a=2)) == "d1S1ai2"
# Lists are prefixed with 'l' and the 4-bit encoded length of the list
assert ascson.dumps([]) == "l0"
assert ascson.dumps(["a"]) == "l1S1a"
License
As usual, this is available to you under the Terms of the MIT Open-Source License Agreement.
Contributing
Any merge requests or issue reports are welcome. I have a day job, so this may get updated infrequently.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ascson-0.0.1.tar.gz.
File metadata
- Download URL: ascson-0.0.1.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d11b20ffa98ab413382c3c14447e88b69c3e2347aa0c20843af501a9aefcad0f
|
|
| MD5 |
bf06dc59a9ce9aafea3e7b49453d6a9b
|
|
| BLAKE2b-256 |
0b61024848f1948506ac683f5d85d3261bcbf68ab0c8a995335ba6319e3df5e7
|
File details
Details for the file ascson-0.0.1-py3-none-any.whl.
File metadata
- Download URL: ascson-0.0.1-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29d18e9cb1bdca11b1412e2149f7d31d382e89148ef90aece5e48786b095e95d
|
|
| MD5 |
1a6e73b258c2dc40ff7f46477d2da740
|
|
| BLAKE2b-256 |
643c8f5812ba28cba94ac3a224254b36a1f487a9e5d4a3daad6819a923090895
|