Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yukinarit/pyserde
Yet another serialization library on top of dataclasses, inspired by serde-rs.
https://github.com/yukinarit/pyserde
dataclasses json msgpack python serde serialization toml typing yaml
Last synced: 29 days ago
JSON representation
Yet another serialization library on top of dataclasses, inspired by serde-rs.
- Host: GitHub
- URL: https://github.com/yukinarit/pyserde
- Owner: yukinarit
- License: mit
- Created: 2018-12-05T05:10:29.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2024-09-24T00:56:30.000Z (about 2 months ago)
- Last Synced: 2024-09-30T23:02:57.775Z (about 1 month ago)
- Topics: dataclasses, json, msgpack, python, serde, serialization, toml, typing, yaml
- Language: Python
- Homepage: https://yukinarit.github.io/pyserde/guide/en
- Size: 7.72 MB
- Stars: 719
- Watchers: 8
- Forks: 40
- Open Issues: 37
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
pyserde
Yet another serialization library on top of dataclasses, inspired by serde-rs.
Guideπ¬π§ | γ¬γ€γπ―π΅ | API Reference | Examples## Overview
`pyserde` is a simple yet powerful serialization library on top of [dataclasses](https://docs.python.org/3/library/dataclasses.html). It allows you to convert Python objects to and from JSON, YAML, and other formats easily and efficiently.
Declare your class with `@serde` decorator and annotate fields using [PEP484](https://peps.python.org/pep-0484/) as below.
```python
@serde
class Foo:
i: int
s: str
f: float
b: bool
```You can serialize `Foo` object into JSON.
```python
>>> to_json(Foo(i=10, s='foo', f=100.0, b=True))
'{"i":10,"s":"foo","f":100.0,"b":true}'
```You can deserialize JSON into `Foo` object.
```python
>>> from_json(Foo, '{"i": 10, "s": "foo", "f": 100.0, "b": true}')
Foo(i=10, s='foo', f=100.0, b=True)
```That's it! If you're interested in pyserde, please check our documentation!
Happy coding with pyserde! π
* [Getting started](https://yukinarit.github.io/pyserde/guide/en/getting-started.html)
* [API Reference](https://yukinarit.github.io/pyserde/api/serde.html)
* [Examples](https://github.com/yukinarit/pyserde/tree/main/examples)## Features
- Supported data formats
- dict
- tuple
- JSON
- Yaml
- Toml
- MsgPack
- Pickle
- Supported types
- Primitives (`int`, `float`, `str`, `bool`)
- Containers
- `list`, `set`, `tuple`, `dict`
- [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset), [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict)
- [`typing.Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)
- [`typing.Union`](https://docs.python.org/3/library/typing.html#typing.Union)
- User defined class with [`@dataclass`](https://docs.python.org/3/library/dataclasses.html)
- [`typing.NewType`](https://docs.python.org/3/library/typing.html#newtype) for primitive types
- [`typing.Any`](https://docs.python.org/3/library/typing.html#the-any-type)
- [`typing.Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)
- [`typing.Generic`](https://docs.python.org/3/library/typing.html#user-defined-generic-types)
- [`typing.ClassVar`](https://docs.python.org/3/library/typing.html#typing.ClassVar)
- [`dataclasses.InitVar`](https://docs.python.org/3/library/dataclasses.html#init-only-variables)
- [`Enum`](https://docs.python.org/3/library/enum.html#enum.Enum) and [`IntEnum`](https://docs.python.org/3/library/enum.html#enum.IntEnum)
- Standard library
- [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html)
- [`decimal.Decimal`](https://docs.python.org/3/library/decimal.html)
- [`uuid.UUID`](https://docs.python.org/3/library/uuid.html)
- [`datetime.date`](https://docs.python.org/3/library/datetime.html#date-objects), [`datetime.time`](https://docs.python.org/3/library/datetime.html#time-objects), [`datetime.datetime`](https://docs.python.org/3/library/datetime.html#datetime-objects)
- [`ipaddress`](https://docs.python.org/3/library/ipaddress.html)
- PyPI library
- [`numpy`](https://github.com/numpy/numpy) types
- [`SQLAlchemy`](https://github.com/sqlalchemy/sqlalchemy) Declarative Dataclass Mapping (experimental)
- [Class Attributes](https://github.com/yukinarit/pyserde/blob/main/docs/en/class-attributes.md)
- [Field Attributes](https://github.com/yukinarit/pyserde/blob/main/docs/en/field-attributes.md)
- [Decorators](https://github.com/yukinarit/pyserde/blob/main/docs/en/decorators.md)
- [Type Check](https://github.com/yukinarit/pyserde/blob/main/docs/en/type-check.md)
- [Union Representation](https://github.com/yukinarit/pyserde/blob/main/docs/en/union.md)
- [Forward reference](https://github.com/yukinarit/pyserde/blob/main/docs/en/decorators.md#how-can-i-use-forward-references)
- [PEP563 Postponed Evaluation of Annotations](https://github.com/yukinarit/pyserde/blob/main/docs/en/decorators.md#pep563-postponed-evaluation-of-annotations)
- [PEP585 Type Hinting Generics In Standard Collections](https://github.com/yukinarit/pyserde/blob/main/docs/en/getting-started.md#pep585-and-pep604)
- [PEP604 Allow writing union types as X | Y](https://github.com/yukinarit/pyserde/blob/main/docs/en/getting-started.md#pep585-and-pep604)
- [PEP681 Data Class Transform](https://github.com/yukinarit/pyserde/blob/main/docs/en/decorators.md#serde)
- [Case Conversion](https://github.com/yukinarit/pyserde/blob/main/docs/en/class-attributes.md#rename_all)
- [Rename](https://github.com/yukinarit/pyserde/blob/main/docs/en/field-attributes.md#rename)
- [Alias](https://github.com/yukinarit/pyserde/blob/main/docs/en/field-attributes.md#alias)
- Skip (de)serialization ([skip](https://github.com/yukinarit/pyserde/blob/main/docs/en/field-attributes.md#skip), [skip_if](https://github.com/yukinarit/pyserde/blob/main/docs/en/field-attributes.md#skip_if), [skip_if_false](https://github.com/yukinarit/pyserde/blob/main/docs/en/field-attributes.md#skip_if_false), [skip_if_default](https://github.com/yukinarit/pyserde/blob/main/docs/en/field-attributes.md#skip_if_default))
- [Custom field (de)serializer](https://github.com/yukinarit/pyserde/blob/main/docs/en/field-attributes.md#serializerdeserializer)
- [Custom class (de)serializer](https://github.com/yukinarit/pyserde/blob/main/docs/en/class-attributes.md#class_serializer--class_deserializer)
- [Custom global (de)serializer](https://github.com/yukinarit/pyserde/blob/main/docs/en/extension.md#custom-global-deserializer)
- [Flatten](https://github.com/yukinarit/pyserde/blob/main/docs/en/field-attributes.md#flatten)## Extensions
* [pyserde-timedelta](https://github.com/yukinarit/pyserde-timedelta): (de)serializing datetime.timedelta in ISO 8601 duration format.
## Contributors β¨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
yukinarit
π»
Alexander Miskaryan
π»
ydylla
π»
Kevin Squire
π» π
Yushi OMOTE
π»
Yuji Kanagawa
π»
Weiliang Li
π»
Mauve
π»
adsharma
π»
Guilhem C.
π
Pierre Tardy
π»
Raphael Nestler
π
Pranav V P
π»
andreymal
π»
Johann Fuechsl
π»
DoeringChristian
π»
Stuart Axelbrooke
π»
Jakub BerΓ‘nek
π»
Fredrik Reinholdsen
π»
Bruno Oliveira
π
Kyle Kosic
π»
Gajo Petrovic
π
m472
π»
acolley-gel
π»
Marc-AndrΓ© Allaire
π»
Ganden Schaffner
π»
Dave Tapley
π»
Beartama
π»
Rachael Sexton
π»
JWSong
π»
Emanuele Barsanti
π»
Aman Clement Aranha
π
π'
π
Add your contributions
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
## LICENSE
This project is licensed under the [MIT license](https://github.com/yukinarit/pyserde/blob/main/LICENSE).