Python wrapper around the powerful Rust serialization framework.
perde
can support as many format as serde
can.pip install perde
>>> import perde
Assume you have a dataclass,
>>> @dataclass
... class A:
... a: int
... b: str
To serialize class A
to JSON,
>>> perde.json.dumps(A(a=10, b='x'))
'{"a":10,"b":"x"}'
To deserialize JSON to class A
,
>>> perde.json.loads_as(A, '{"a":10,"b":"x"}')
A(a=10, b='x')
To deserialize JSON to a dictionary,
>>> perde.json.loads('{"a":10,"b":"x"}')
{'a': 10, 'b': 'x'}
More formats are supported.
>>> 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')
perde.json
)perde.yaml
)perde.msgpack
)perde.toml
)dataclass
int
str
float
bool
bytes
bytearray
dict
/typing.Dict
list
/ typing.List
set
/ typing.Set
frozenset
/ typing.FrozenSet
tuple
/ typing.Tuple
typing.Optional
typing.Union
typing.Any
Enum
IntEnum
Flag
IntFlag
datetime.datetime
datetime.date
datetime.time
decimal.Decimal
uuid.UUID
Attributes allow to modify the way of serialization/deserialization.
For example, to serialize/deserialize the field names as camelCase
,
>>> @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 for more details.
The benchmark repeats (de)serializing the data structure A
10000 times:
class A:
a: int
b: str
c: float
d: bool