Usage
Install and import perde
.
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')
Supported formats
-
JSON (
perde.json
) -
YAML (
perde.yaml
) -
MessagePack (
perde.msgpack
) -
TOML (
perde.toml
) - CBOR
- Pickle
- RON
- BSON
- Avro
- JSON5
- Postcard
- URL
- Environment variables
- AWS Parameter Store
- S-expressions
- D-Bus
- FlexBuffer
- XML
All the formats provide the three methods:
dumps(objects)
: Serializeobjects
in the format.loads(data)
: Deserializedata
to python objects.loads_as(type, input)
: Deserializedata
to python objects as specifiedtype
.