Easily serialize dataclasses to and from JSON
Project description
# Dataclasses JSON
This library provides a simple API for encoding and decoding [dataclasses](https://www.python.org/dev/peps/pep-0557/) to and from JSON.
It's recursive (see caveats below), so you can easily work with nested dataclasses.
In addition to the supported types in the [py to JSON table](https://docs.python.org/3/library/json.html#py-to-json-table), any arbitrary
[Collection](https://docs.python.org/3/library/collections.abc.html#collections.abc.Collection) type is supported (they are encoded into JSON arrays, but decoded into the original collection types).
**The [latest release](https://github.com/lidatong/dataclasses-json/releases/latest) is compatible with both Python 3.7 and Python 3.6 (with the dataclasses backport).**
## Quickstart
`pip install dataclasses-json`
```python
my_dataclass_instance.to_json()
MyDataClass.from_json(some_json_string)
```
```python
from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin
@dataclass(frozen=True)
class Minion():
name: str
@dataclass(frozen=True)
class Boss(DataClassJsonMixin):
minions: List[Minion]
boss = Boss([Minion('evil minion'), Minion('very evil minion')])
boss_json = """
{
"minions": [
{
"name": "evil minion"
},
{
"name": "very evil minion"
}
]
}
""".strip()
assert boss.to_json(indent=4) == boss_json
assert Boss.from_json(boss_json) == boss
```
## Caveats
Data Classes that contain forward references (e.g. recursive dataclasses) are not currently supported.
This library provides a simple API for encoding and decoding [dataclasses](https://www.python.org/dev/peps/pep-0557/) to and from JSON.
It's recursive (see caveats below), so you can easily work with nested dataclasses.
In addition to the supported types in the [py to JSON table](https://docs.python.org/3/library/json.html#py-to-json-table), any arbitrary
[Collection](https://docs.python.org/3/library/collections.abc.html#collections.abc.Collection) type is supported (they are encoded into JSON arrays, but decoded into the original collection types).
**The [latest release](https://github.com/lidatong/dataclasses-json/releases/latest) is compatible with both Python 3.7 and Python 3.6 (with the dataclasses backport).**
## Quickstart
`pip install dataclasses-json`
```python
my_dataclass_instance.to_json()
MyDataClass.from_json(some_json_string)
```
```python
from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin
@dataclass(frozen=True)
class Minion():
name: str
@dataclass(frozen=True)
class Boss(DataClassJsonMixin):
minions: List[Minion]
boss = Boss([Minion('evil minion'), Minion('very evil minion')])
boss_json = """
{
"minions": [
{
"name": "evil minion"
},
{
"name": "very evil minion"
}
]
}
""".strip()
assert boss.to_json(indent=4) == boss_json
assert Boss.from_json(boss_json) == boss
```
## Caveats
Data Classes that contain forward references (e.g. recursive dataclasses) are not currently supported.
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
Close
Hashes for dataclasses_json-0.0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac613342b321d63130f58f0bc5512b2ae6b76cbd74c722aa0758f5d38edc71a9 |
|
MD5 | c7437f67aced5b75d8398a4a8d2bd198 |
|
BLAKE2b-256 | 41ebe78976ce67460d6f99a7482a0ad61d5fdc4a4dcaf52052c2c689a531d028 |