QuickstartΒΆ
To use PyJackson in a project, define a class with type hinted constructor arguments
1 2 3 4 | class MyPayload:
def __init__(self, string_field: str, int_field: int):
self.string_field = string_field
self.int_field = int_field
|
Now you are able to serialize instance of your class to dict and back with
serialize()
and deserialize()
1 2 3 4 | instance = MyPayload('value', 10)
payload = pyjackson.serialize(instance) # {'string_field': 'value', 'int_field': 10}
new_instance = pyjackson.deserialize(payload, MyPayload) # MyPayload('value', 10)
|
It also works with nested structures and supports typing module generic annotations
1 2 3 4 5 6 7 8 | class PayloadList:
def __init__(self, payload_list: typing.List[MyPayload]):
self.payload_list = payload_list
plist = PayloadList([instance])
payloads = pyjackson.serialize(plist)
# {'payload_list': [{'string_field': 'value', 'int_field': 10}]}
|
This code can be found in examples/quickstart.py