https://github.com/maspe36/json_typer
Seamlessly encode and decode python objects to json while maintaining their types.
https://github.com/maspe36/json_typer
dynamic-typing json library types
Last synced: 11 months ago
JSON representation
Seamlessly encode and decode python objects to json while maintaining their types.
- Host: GitHub
- URL: https://github.com/maspe36/json_typer
- Owner: maspe36
- License: mit
- Created: 2018-07-21T21:53:36.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-01T20:08:24.000Z (over 7 years ago)
- Last Synced: 2025-05-19T10:08:35.886Z (about 1 year ago)
- Topics: dynamic-typing, json, library, types
- Language: Python
- Homepage:
- Size: 21.5 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json_typer
Seamlessly encode and decode python objects to json while maintaining their types.
## Installation
`pip install json_typer`
## Usage
### Making a class type serializable
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.
```python
from json_typer import TypeSerializable
class Foo(TypeSerializable):
def __init__(bar, baz="", *args, **kwargs):
self.bar = bar
self.baz = baz
```
### Exporting a type serializable class
```python
from json_typer import io
foo = Foo(bar="example")
io.exportJSON(path=EXAMPLE_FILE, data=foo)
```
EXAMPLE_FILE contents
```javascript
{
"type": "Foo"
"module": "Foo"
"bar": "example",
"baz": ""
}
```
### Importing a JSON file
```python
from json_typer import io
foo = io.loadJSON(path=EXAMPLE_FILE)
```
Access the loaded attributes like you normally would
```python
foo.bar
>>> example
isinstance(foo, Foo)
>>> True
```
## Running Tests
Open a terminal in this projects root directory and type ```python -m unittest```
## Limitations
- Must have ```*args, **kwargs``` in the constructor and passed to the super call in any class that inherits from ```TypeSerializable```
- A class that inherits from ```TypeSerializable``` cannot implement ```_type``` or ```_module``` attributes
## Authors
* **Sam Privett** - *Initial work* - [maspe36](https://github.com/maspe36)
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details