Skip to main content

jsonextra

Build Status Codecov PyPI Version License

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.

Metadata

Release files for jsonextra 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonextra 0.4.0
File Size Uploaded
jsonextra-0.4.0.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonextra 0.4.0
File Interpreter ABI Platform
jsonextra-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / jsonextra-0.4.0.tar.gz

Download URL jsonextra-0.4.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
31169aa697ccec0a85b77dddb71c88cac4ebd025df1c0fce77ae5395e44a8b0e
BLAKE2b-256 checksum
How to use checksums
cd1a18f4585615db2d2b4b69f62c2bde50f8ab4e84400da33609abbae5b634ad
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / jsonextra-0.4.0-py3-none-any.whl

Download URL jsonextra-0.4.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1e5fc4c9e4228b7e4241e9981f048a576fdceb2d3678c2063400dbaae1080c5
BLAKE2b-256 checksum
How to use checksums
061e7744c3734c553da68b7ac74a5b33a28e572edf946a6afccbc8fb2dfb93e5
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page