JSON Extra | JSON that gives you extra datetime, uuid and bytes data types
Project description
jsonextra
same as json
library, but with extra support for bytes
, uuid
and datetime
data classes
Installation
$ pip install jsonextra
Usage
Use just like json
as normal once imported, but with addition of extra data classes.
import uuid, datetime # for creation of `my_data` object
import jsonextra
my_data = {'id': uuid.uuid4(), 'created': datetime.date.today()}
# my_data --> {'id': uuid.UUID('5f7660c5-88ea-46b6-93e2-860d5b7a0271'), 'created': datetime.date(2019, 6, 16)}
# Serializes the key values to stringified versions
my_json = jsonextra.dumps(my_data)
# my_json --> '{"id": "5f7660c5-88ea-46b6-93e2-860d5b7a0271", "created": "2019-06-16"}'
# Deserializes the object and confirms the output matches `my_data`
assert jsonextra.loads(my_json) == my_data # True
.dump(obj, fp, **kwargs)
& .dumps(obj, **kwargs)
Will serialize extra data classes into their string (__str__
) or special representations (eg: .isoformat
, etc.).
.load(fp, **kwargs)
& .loads(s, **kwargs)
Will deserialize any stings, which match patterns of extra supported data classes.
For example, if something looks like a uuid - it will be converted to uuid.UUID
.
If this behaviour is undesired, please use the built-in json.loads
method instead of jsonextra.loads
.
Supported extra data classes
Python Data Class | Python Object (deserialized) | JSON Object (serialized) |
---|---|---|
datetime.date |
datetime.date(2019, 1, 1) |
"2019-01-01" |
datetime.time |
datetime.time(23, 59, 11) |
"23:59:11" |
datetime.datetime |
datetime.datetime(2019, 1, 1, 23, 59, 11) |
"2019-01-01T23:59:11" |
uuid.UUID |
uuid.UUID('5f7660c5-88ea-46b6-93e2-860d5b7a0271') |
"5f7660c5-88ea-46b6-93e2-860d5b7a0271" |
bytes |
b'\xd6aO\x1d\xd71Y\x05' |
"base64:1mFPHdcxWQU=" |
More examples of serialized/deserialized values can be found in tests ;p
Why?
Why would you want to use this library?
- If you work with a model-less data structures, and its data types expend beyond the JSON standard supports.
- If your model schemas are too dynamic to be able to use model-based serializers, and requires to store more data types.
- If your data structure does not fit with the JSON standard, and it needs expanding to support one or more data types.
How it works
An extra supported python object is dumped to a string value. When loading a serialized json object, any values matching the string supported data class, will be converted to their expected data class instances.
If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.
Contributions
Contibutions are welcome, please submit your pull requests into dev
branch for a review.
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
Built Distribution
File details
Details for the file jsonextra-0.4.0.tar.gz
.
File metadata
- Download URL: jsonextra-0.4.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31169aa697ccec0a85b77dddb71c88cac4ebd025df1c0fce77ae5395e44a8b0e |
|
MD5 | a4c5819fec892257ddf348fe1fdf67ac |
|
BLAKE2b-256 | cd1a18f4585615db2d2b4b69f62c2bde50f8ab4e84400da33609abbae5b634ad |
File details
Details for the file jsonextra-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: jsonextra-0.4.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1e5fc4c9e4228b7e4241e9981f048a576fdceb2d3678c2063400dbaae1080c5 |
|
MD5 | 0dba614b971a309dd598df4295086052 |
|
BLAKE2b-256 | 061e7744c3734c553da68b7ac74a5b33a28e572edf946a6afccbc8fb2dfb93e5 |