Seamlessly encode and decode python objects to json while maintaining their types.
pip install json_typer
To make a class type serializable (a.k.a to seamlessly serialize the class to JSON and then when loading the JSON convert the object to its old type) inherit from TypeSerializable and make sure you are passing *args, **kwargs to the super constructor.
from json_typer import TypeSerializable
class Foo(TypeSerializable):
def __init__(bar, baz="", *args, **kwargs):
self.bar = bar
self.baz = bazfrom json_typer import io
foo = Foo(bar="example")
io.exportJSON(path=EXAMPLE_FILE, data=foo)EXAMPLE_FILE contents
{
"type": "Foo"
"module": "Foo"
"bar": "example",
"baz": ""
}from json_typer import io
foo = io.loadJSON(path=EXAMPLE_FILE)Access the loaded attributes like you normally would
foo.bar
>>> example
isinstance(foo, Foo)
>>> TrueOpen a terminal in this projects root directory and type python -m unittest
- Must have
*args, **kwargsin the constructor and passed to the super call in any class that inherits fromTypeSerializable - A class that inherits from
TypeSerializablecannot implement_typeor_moduleattributes
- Sam Privett - Initial work - maspe36
This project is licensed under the MIT License - see the LICENSE.md file for details