A library to make deserialization easy.
Project description
deserialize
A library to make deserialization easy. To get started, just run pip install deserialize
How it used to be
Without the library, if you want to convert:
{
"a": 1,
"b": 2
}
into a dedicated class, you had to do something like this:
class MyThing:
def __init__(self, a, b):
self.a = a
self.b = b
@staticmethod
def from_json(json_data):
a_value = json_data.get("a")
b_value = json_data.get("b")
if a_value is None:
raise Exception("'a' was None")
elif b_value is None:
raise Exception("'b' was None")
elif type(a_value) != int:
raise Exception("'a' was not an int")
elif type(b_value) != int:
raise Exception("'b' was not an int")
return MyThing(a_value, b_value)
my_instance = MyThing.from_json(json_data)
How it is now
With deserialize
all you need to do is this:
import deserialize
class MyThing:
a: int
b: int
my_instance = deserialize.deserialize(MyThing, json_data)
That's it. It will pull out all the data and set it for you type checking and even checking for null values.
If you want null values to be allowed though, that's easy too:
from typing import Optional
class MyThing:
a: Optional[int]
b: Optional[int]
Now None
is a valid value for these.
Types can be nested as deep as you like. For example, this is perfectly valid:
class Actor:
name: str
age: int
class Episode:
title: str
identifier: st
actors: List[Actor]
class Season:
episodes: List[Episode]
completed: bool
class TVShow:
seasons: List[Season]
creator: str
Advanced Usage
Custom Keys
It may be that you want to name your properties in your object something different to what is in the data. This can be for readability reasons, or because you have to (such as if your data item is named __class__
). This can be handled too. Simply use the key
annotation as follows:
@deserialize.key("identifier", "id")
class MyClass:
value: int
identifier: str
This will now assign the data with the key id
to the field identifier
. You can have multiple annotations to override multiple keys.
Parsers
Sometimes you'll want something in your object in a format that the data isn't in. For example, if you get the data:
{
"successful": True,
"timestamp": 1543770752
}
You may want that to be represented as:
class Result:
successful: bool
timestamp: datetime.datetime
By default, it will fail on this deserialization as the value in the data is not a timestamp. To correct this, use the parser
decorator to tell it a function to use to parse the data. E.g.
@deserialize.parser("timestamp", datetime.datetime.fromtimestamp)
class Result:
successful: bool
timestamp: datetime.datetime
This will now detect when handling the data for the key timestamp
and run it through the parser function supplied before assigning it to your new class instance.
The parser is run before type checking is done. This means that if you had something like Optional[datetime.datetime]
, you should ensure your parser can handle the value being None
. Your parser will obviously need to return the type that you have declared on the property in order to work.
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
File details
Details for the file deserialize-0.5.0.tar.gz
.
File metadata
- Download URL: deserialize-0.5.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.10 CPython/3.7.2 Darwin/18.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9eb6c05ff810b9003c887239afb18796a172f629932efe0a04d366cfeeb463a2 |
|
MD5 | 71ae5c0fe64c807053ab6f39d68e4991 |
|
BLAKE2b-256 | f35a76334545014a17c026b354e9abe15549e40d86f8a200048cf8c0ff9fcca9 |
File details
Details for the file deserialize-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: deserialize-0.5.0-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.10 CPython/3.7.2 Darwin/18.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c59e9af7eb86733e516e4742943ae544a4c48088edddcaa103a7fd23cd6251ad |
|
MD5 | e6cf63f35488b01804351e23853ec87d |
|
BLAKE2b-256 | c031762c595fd1caf08ac53786fad1596e8ac79bf83f9bf423f2df3cc71fbf3f |