No more boilerplate to check and build a Python object from JSON.
Reason this release was yanked:
Bug in check of recursive data structure. v0.4.2 onwards include a fix.
Project description
JSONloader
This module is for you if you're tired of writing boilerplate that:
- builds a straightforward Python object from loaded JSON.
- checks that your input-loaded-JSON has all necessary attributes for your pipeline.
- checks that your input JSON has the right types.
Example
Main intended usage is through the JSONclass decorator
, example below:
>>> # By default we don't check for anything, we just build the object
>>> # as we received it.
>>> data = {'a': 'aa', 'b': 'bb', 'c': 1}
>>> @JSONClass
... class Example:
... pass
...
>>> wrapper = Example(data)
>>> wrapper.a
'aa'
>>> wrapper.b
'bb'
>>> # We want to ensure we have annotated parameters
>>> data = {'a': 'aa', 'b': 'bb', 'c': 1}
>>> @JSONClass(annotations=True)
... class Example:
... a : str
... d : int
...
>>> try:
... wrapper = Example(data)
... except KeyError:
... print("error - missing 'd'")
...
error - missing 'd'
>>> # We want to ensure we have *only* annotated parameters
>>> data = {'a': 'aa', 'b': 'bb', 'c': 1}
>>> @JSONClass(annotations=True, annotations_strict=True)
... class Example:
... a : str
... b : int
...
>>> try:
... wrapper = Example(data)
... except KeyError:
... print("error - extra 'c'")
...
error - extra 'c'
>>> # We want to check we have only annotated parameters and they
>>> # are of annotated type.
>>> data = {'a': 'aa', 'b': 'bb'}
>>> @JSONClass(annotations=True, annotations_strict=True, annotations_type=True)
... class Example:
... a : str
... b : int
...
>>> try:
... wrapper = Example(data)
... except TypeError:
... print("error - b is int")
...
error - b is int
Install
User installation
python3 -m virtualenv venv
. venv/bin/activate
pip3 install jsonloader
Developer installation
python3 -m virtualenv venv
. venv/bin/activate
pip3 install -e '.[dev]'
Run Tests
# From git directory
nose2 -t .
Tests coverage
For example, leverage coverage
module: nose2 -t . -C --coverage-report html
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
jsonloader-0.4.1.tar.gz
(5.3 kB
view hashes)
Built Distribution
Close
Hashes for jsonloader-0.4.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9451239d9ce245da670edd80adcf90200a214171e84bd7fb593eef90f75cbe5 |
|
MD5 | fe9afb8ec588cf4888f90c28f814ca72 |
|
BLAKE2b-256 | 7949673c413f0c5e47a9f1b1bf83a338104bf208ae88f9956059b30ae57627d6 |