Easily read objects from json
Project description
fromjson
Python library to easily read objects from JSON.
Have you ever had classes, which you want to read from JSON, but didn't want to write the boilerplate from_json classmethods to convert JSON to Python objects?
Now you don't have to!
fromjson adds a simple decorator for classes, which adds a from_json classmethod to the class.
It works on all classes, as long as the fields in the class are either Python primitive types (int, float, str, dict, list) or classes with a from_json classmethod, and the class has type hints for its constuctor.
This means that if you have nested classes which you want to load from JSON, you only need to add a decorator call for each of the classes.
This library is heavily inspired by the excellent Haskell library aeson.
Usage
All the examples have these imports
>>> from dataclasses import dataclass
>>> import json
>>>
>>> from fromjson import fromjson, tojson
Suppose we have the following JSON:
>>> foo = """
... {
... "field": "a string value",
... "another_field": 42
... }
... """
We want to deserialise it into a class defined by:
>>> @dataclass
... class Foo:
... field: str
... another_field: int
Adding a @fromjson decorator before the class definition adds a from_json classmethod to the class, which enables us to load it from a dictionary representation:
>>> @dataclass
... @fromjson
... class Foo:
... field: str
... another_field: int
...
>>> Foo.from_json(json.loads(foo))
Foo(field='a string value', another_field=42)
tojson
For completeness, there's also a @tojson decorator, which does the opposite.
It is used similarly:
>>> @dataclass
... @tojson
... class Foo:
... field: str
... another_field: int
...
>>> json.dumps(Foo("asd", 42).to_json())
'{"field": "asd", "another_field": 42}'
Nested classes
The real power of fromjson is in deserialising user made classes, which have other user made classes as fields. Suppose we have the following nested JSON data:
>>> bird = """
... {
... "name": "Duck",
... "cry": "Quack!",
... "egg": {
... "size": "Medium",
... "color": "White"
... }
... }
... """
We can parse it into the following classes, by adding a @fromjson call to each class:
>>> @dataclass
... @fromjson
... class Egg:
... size: str
... color: str
...
>>> @dataclass
... @fromjson
... class Bird:
... name: str
... cry: str
... egg: Egg
...
>>> import json
>>> Bird.from_json(json.loads(bird))
Bird(name='Duck', cry='Quack!', egg=Egg(size='Medium', color='White'))
Parsing logic
The json is parsed according to the following logic:
- The argument for
from_jsonhas to be aMapping(i.e. an ordinary dictionary is fine) - The type hints for the class are read
- For each type hint, if the type is one of
str,int,float,dict, orlist, the corresponding value from the argument mapping is used as-is. - If the type is none of the enumerated primitive types, it has to be a class with a
from_jsonclassmethod, which is then used to parse the object. - If the type isn't a primitive, and the class doesn't have a
from_jsonclassmethod, the parsing fails.
If your nested class has some really complicated parsing logic, for which fromjson is inadequate, you can write a from_json classmethod by hand.
Then the class can be used inside other classes, which have a @fromjson decorator call.
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 fromjson-0.1.0.tar.gz.
File metadata
- Download URL: fromjson-0.1.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a06dd1e042fefabb0cbbc9eb1663a1fe0ccc9e869addba4c353e0881d3c6d999
|
|
| MD5 |
a6b042b0b9dd110f8dd79773d62a339c
|
|
| BLAKE2b-256 |
cd78cb06ec0a9599bfb9c35228eb3e8313a54e7129fce1d34aa32ff67637e4af
|
File details
Details for the file fromjson-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fromjson-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f14b0c482213db8689332b364e34ccdcfdb11fd0db17f2a679b7bb7a859e431
|
|
| MD5 |
8b4e75ab849f9ed4e25886eca97a8ab0
|
|
| BLAKE2b-256 |
2e65715c5a0832ac0ab15a56dc7f395f1816878b040cf7057919b682dbe19faf
|