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 representations (__str__
).
Note: for datetime objects, the __str__
is dumped to an ISO8601-like format: yyyy-mm-dd HH:MM:SS
, and it is the same format that will be expected by .loads
method.
.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
datetime.date
datetime.datetime
uuid.UUID
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 Distributions
Built Distribution
Hashes for jsonextra-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bb08d95a48aa1873a8062f3139ad4c9a51c9ed9464a18e62254adb729e320e5 |
|
MD5 | b80d5c3f3be776c913ad0f8fc79eb684 |
|
BLAKE2b-256 | 0934f594e9f66fdfc972972cca459f6912ee7efecf36fd7b829acdc8c223b07a |