Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

Awesome Lists containing this project

README

        

pyserde


Yet another serialization library on top of dataclasses, inspired by serde-rs.




pypi


pypi


GithubActions


CodeCov



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
yukinarit

πŸ’»
Alexander Miskaryan
Alexander Miskaryan

πŸ’»
ydylla
ydylla

πŸ’»
Kevin Squire
Kevin Squire

πŸ’» πŸ“–
Yushi OMOTE
Yushi OMOTE

πŸ’»
Yuji Kanagawa
Yuji Kanagawa

πŸ’»
Weiliang Li
Weiliang Li

πŸ’»


Mauve
Mauve

πŸ’»
adsharma
adsharma

πŸ’»
Guilhem C.
Guilhem C.

πŸ“–
Pierre Tardy
Pierre Tardy

πŸ’»
Raphael Nestler
Raphael Nestler

πŸ“–
Pranav V P
Pranav V P

πŸ’»
andreymal
andreymal

πŸ’»


Johann Fuechsl
Johann Fuechsl

πŸ’»
DoeringChristian
DoeringChristian

πŸ’»
Stuart Axelbrooke
Stuart Axelbrooke

πŸ’»
Jakub BerΓ‘nek
Jakub BerΓ‘nek

πŸ’»
Fredrik Reinholdsen
Fredrik Reinholdsen

πŸ’»
Bruno Oliveira
Bruno Oliveira

πŸ“–
Kyle Kosic
Kyle Kosic

πŸ’»


Gajo Petrovic
Gajo Petrovic

πŸ“–
m472
m472

πŸ’»
acolley-gel
acolley-gel

πŸ’»
Marc-AndrΓ© Allaire
Marc-AndrΓ© Allaire

πŸ’»
Ganden Schaffner
Ganden Schaffner

πŸ’»
Dave Tapley
Dave Tapley

πŸ’»
Beartama
Beartama

πŸ’»


Rachael Sexton
Rachael Sexton

πŸ’»
JWSong
JWSong

πŸ’»
Emanuele Barsanti
Emanuele Barsanti

πŸ’»
Aman Clement Aranha
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).