https://github.com/yushiomote/perde
Python serialization framework powered by Rust
https://github.com/yushiomote/perde
dataclasses json messagepack python serialization toml typing yaml
Last synced: 16 days ago
JSON representation
Python serialization framework powered by Rust
- Host: GitHub
- URL: https://github.com/yushiomote/perde
- Owner: YushiOMOTE
- Created: 2020-08-27T12:06:57.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-12-12T08:15:12.000Z (over 4 years ago)
- Last Synced: 2025-04-12T23:46:37.838Z (26 days ago)
- Topics: dataclasses, json, messagepack, python, serialization, toml, typing, yaml
- Language: Rust
- Homepage: https://yushiomote.github.io/perde
- Size: 3.78 MB
- Stars: 26
- Watchers: 2
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
# perde: python-wrapped serde
### Heavily under construction towards 0.1.0 🎅
[](https://www.repostatus.org/#wip)
[](https://opensource.org/licenses/MIT)
[](https://pypi.python.org/pypi/perde)
[](https://pypi.org/project/perde/)
[](https://github.com/YushiOMOTE/perde/actions)
[](https://codecov.io/gh/yushiomote/perde)
[](https://github.com/ambv/black)
Python wrapper around [the powerful Rust serialization framework](https://github.com/serde-rs/serde).
* Serialization & deserialization of python data structures.
* Supports various types including dataclasses, generic types, enum and common built-in types.
* Supports various serialization formats. By design, `perde` can support as many format as `serde` can.
* Provides string case conversion of field names, skipping serialization/deserialization options, structure flattening.
* Precise type checking based on type hints.
* Very fast.### Install
```sh
pip install perde
```### Usage
```python
>>> import perde```
Assume you have a dataclass,
```python
>>> @dataclass
... class A:
... a: int
... b: str```
To serialize class `A` to JSON,
```python
>>> perde.json.dumps(A(a=10, b='x'))
'{"a":10,"b":"x"}'```
To deserialize JSON to class `A`,
```python
>>> perde.json.loads_as(A, '{"a":10,"b":"x"}')
A(a=10, b='x')```
To deserialize JSON to a dictionary,
```python
>>> perde.json.loads('{"a":10,"b":"x"}')
{'a': 10, 'b': 'x'}```
More formats are supported.
```python
>>> perde.yaml.dumps(A(10, "x"))
'---\na: 10\nb: x'
>>> perde.yaml.loads_as(A, '---\na: 10\nb: x')
A(a=10, b='x')
>>> perde.msgpack.dumps(A(10, "x"))
b'\x82\xa1a\n\xa1b\xa1x'
>>> perde.msgpack.loads_as(A, b'\x82\xa1a\n\xa1b\xa1x')
A(a=10, b='x')```
### Supported formats
* [x] JSON (`perde.json`)
* [x] YAML (`perde.yaml`)
* [x] MessagePack (`perde.msgpack`)
* [x] TOML (`perde.toml`)
* [ ] CBOR
* [ ] Pickle
* [ ] RON
* [ ] BSON
* [ ] Avro
* [ ] JSON5
* [ ] Postcard
* [ ] URL
* [ ] Environment variables
* [ ] AWS Parameter Store
* [ ] S-expressions
* [ ] D-Bus
* [ ] FlexBuffer
* [ ] XML### Supported types
* `dataclass`
* Primitive types
* `int`
* `str`
* `float`
* `bool`
* `bytes`
* `bytearray`
* Generic types
* `dict` /`typing.Dict`
* `list` / `typing.List`
* `set` / `typing.Set`
* `frozenset` / `typing.FrozenSet`
* `tuple` / `typing.Tuple`
* `typing.Optional`
* `typing.Union`
* `typing.Any`
* Enum types
* `Enum`
* `IntEnum`
* `Flag`
* `IntFlag`
* More built-in types
* `datetime.datetime`
* `datetime.date`
* `datetime.time`
* `decimal.Decimal`
* `uuid.UUID`### Attributes
Attributes allow to modify the way of serialization/deserialization.
For example, to serialize/deserialize the field names as `camelCase`,
```python
>>> @perde.attr(rename_all="camelCase")
... @dataclass
... class A:
... foo_bar: int
... bar_bar: int>>> perde.json.dumps(A(foo_bar=1, bar_bar=2))
'{"fooBar":1,"barBar":2}'
>>> perde.json.loads_as(A, '{"fooBar":1,"barBar":2}')
A(foo_bar=1, bar_bar=2)```
See [the book](https://yushiomote.github.io/perde/attributes.html) for more details.
### Benchmark
![]()
![]()
![]()
The benchmark repeats (de)serializing the data structure `A` 10000 times:
```python
class A:
a: int
b: str
c: float
d: bool
```* [pyserde](https://github.com/yukinarit/pyserde)
* [mashumaro](https://github.com/Fatal1ty/mashumaro)
* [attrs](https://github.com/python-attrs/attrs)
* [cattrs](https://github.com/Tinche/cattrs)