Serialization library on top of dataclasses.
Project description
pyserde: Serialization Library on top of dataclasses
Table of Contents
Installation
$ pip install pyserde
QuickStart
You can serialize and deserialize a dataclass in various message formats quite easily!
# main.py
# /usr/bin/env python
from dataclasses import dataclass
from serde import deserialize, serialize
from serde.json import from_json, to_json
@deserialize
@serialize
@dataclass
class Foo:
i: int
s: str
f: float
b: bool
h = Foo(i=10, s='foo', f=100.0, b=True)
print(f"Into Json: {to_json(h)}")
s = '{"i": 10, "s": "foo", "f": 100.0, "b": true}'
print(f"From Json: {from_json(Foo, s)}")
$ python main.py
Into Json: {"i": 10, "s": "foo", "f": 100.0, "b": true}
From Json: Foo(i=10, s='foo', f=100.0, b=True)
Benchmark
Serialize and Deserialize a struct into and from json 10,000 times.
Environment
- macOS 10.14 Mojave
- Intel 2.3GHz 8-core Intel Core i9
- DDR4 32GB RAM
Result
Serialize | Deserialize |
---|---|
You can check the code for more information.
Features
- Data format
- Class attributes
- Case conversion e.g. camelCase, kebab-case
- Field attributes
Case conversion
>>> @serialize(rename_all = 'camelcase')
... @dataclass
... class Foo:
... int_field: int
... str_field: str
>>>
>>> to_json(Foo(int_field=10, str_field='foo'))
'{"intField": 10, "strField": "foo"}'
Rename field
>>> @serialize
... @dataclass
... class Foo:
... # Use 'class_name' because 'class' is a keyword.
... class_name: str = field(metadata={'serde_rename': 'class'})
>>> to_json(Foo(class_name='Foo'))
'{"class": "Foo"}'
For complete example, please see ./examples/rename.py
Skip
>>> @serialize
... @dataclass
... class Resource:
... name: str
... hash: str
... metadata: Dict[str, str] = field(default_factory=dict, metadata={'serde_skip': True})
>>> resources = [
... Resource("Stack Overflow", "b6469c3f31653d281bbbfa6f94d60fea130abe38"),
... Resource("GitHub", "5cb7a0c47e53854cd00e1a968de5abce1c124601", metadata={"headquarters": "San Francisco"}) ]
>>> to_json(resources)
'[{"name": "Stack Overflow", "hash": "b6469c3f31653d281bbbfa6f94d60fea130abe38"}, {"name": "GitHub", "hash": "5cb7a0c47e53854cd00e1a968de5abce1c124601"}]'
For complete example, please see ./examples/skip.py
Skip if
>>> @serialize
... @dataclass
... class World:
... player: str
... buddy: str = field(default='', metadata={'serde_skip_if': lambda v: v == 'Pikachu'})
>>> world = World('satoshi', 'Pikachu')
>>> to_json(world)
'{"player": "satoshi"}'
>>> world = World('green', 'Charmander')
>>> print(to_json(world))
'{"player": "green", "buddy": "Charmander"}'
For complete example, please see ./examples/skip.py
Skip if value is evaluated as False
>>> @serialize
... @dataclass
... class World:
... player: str
... enemies: List[str] = field(default_factory=list, metadata={'serde_skip_if_false': True})
>>> world = World('satoshi', ['Rattata', 'Pidgey'])
>>> to_json(world)
'{"player": "satoshi", "enemies": ["Rattata", "Pidgey"]}'
>>> world = World('green', [])
>>> print(to_json(world))
'{"player": "green"}'
For complete example, please see ./examples/skip.py
Documentation
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
pyserde-0.0.8.tar.gz
(17.2 kB
view hashes)
Built Distribution
pyserde-0.0.8-py3-none-any.whl
(25.2 kB
view hashes)