Last active
December 2, 2021 17:32
-
-
Save RHDZMOTA/3b50b284d1892e4ac1bcffcca7dab135 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
import json | |
from typing import Dict, TypeVar | |
SerializableDataType = TypeVar( | |
"SerializableDataType", | |
bound="Serializable" | |
) | |
class Serializable: | |
default_cls_encoder = json.JSONEncoder | |
default_cls_decoder = json.JSONDecoder | |
@classmethod | |
def from_json(cls, content: str, **kwargs) -> SerializableDataType: | |
kwargs["cls"] = kwargs.pop("json_cls", cls.default_cls_decoder) | |
instance_kwargs = json.loads(content, **kwargs) | |
return cls(**instance_kwargs) | |
@classmethod | |
def from_file(cls, filename: str, **kwargs) -> SerializableDataType: | |
with open(filename, "r") as file: | |
return cls.from_json(content=file.read(), **kwargs) | |
@classmethod | |
def _get_init_arglist(cls): | |
return inspect.getfullargspec(cls).args | |
def to_dict(self, error_if_not_exists: bool = False) -> Dict: | |
return { | |
arg: getattr(self, arg) if error_if_not_exists else \ | |
getattr(self, arg, None) | |
for arg in self._get_init_arglist() | |
if arg != "self" | |
} | |
def to_json(self, **kwargs) -> str: | |
dictionary = self.to_dict() | |
kwargs["cls"] = kwargs.pop("json_cls", self.default_cls_encoder) | |
return json.dumps(dictionary, **kwargs) |
If you have json-incompatible datatypes, you'll need to provide companion decoders & encoders classes.
Consider the a simple human class with:
name (str)
: the name is a json-compatible datatype.birthdate (datetime)
: the birthdate is json-incompatile; we need to serialize (encode) the string before transforming to JSON and decode when creating a human instance.
You have two options:
- Use a regular string instread of a datetime instance.
- Create the encoders/decoders.
Let's take a look at the second option:
import json
import datetime as dt
import dateutil
from typing import Any, Dict, Union
from serializable_helper import Serializable
class HumanDecoder(json.JSONDecoder):
def __init__(self, *args, **kwargs):
super().__init__(object_hook=self.object_hook, *args, **kwargs)
def object_hook(self, obj: Dict) -> Dict:
human_attributes = set(Human._get_init_arglist())
object_keys = set(obj.keys())
# If not all human attributes match; return dict object
if human_attributes - object_keys - {"self"}:
return obj
# Return human-ready kwargs
return {
"name": obj["name"],
"birthdate": dateutil.parser.parse(obj["birthdate"])
}
class HumanEncoder(json.JSONEncoder):
def default(self, obj: Any):
if isinstance(obj, Human):
return obj.to_json()
if isinstance(obj, dt.datetime):
return dt.datetime.strftime(obj, "%Y-%m-%d")
return json.JSONEncoder.default(self, obj)
class Human(Serializable):
default_cls_decoder = HumanDecoder
default_cls_encoder = HumanEncoder
def __init__(self, name: str, birthdate: dt.datetime):
self.name = name
self.birthdate = birthdate
def __repr__(self) -> str:
return f"{self.name}"
You should now be able to use the Human
class with full serialization capabilities!
human_data = """
{
"name": "Guido van Rossum",
"birthdate": "1956-01-31"
}
"""
# Create a human instance from a valid json string
human_instance = Human.from_json(human_data)
# Validations
assert isinstance(human_instance, Human)
assert isinstance(human_instance.birthdate, dt.datetime)
print(human_instance, human_instance.to_json(), sep="\n")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple example using dataclasses:
Instances should be now serializable:
You should also be able to decode json strings: