databind.json
databind.json
The #databind.json package implements the capabilities to bind JSON payloads to objects and the reverse.
JsonConverter
Bases: ClassDecoratorSetting
Use this setting to decorate a class or to annotate a type hint to inform the JSON module to use the specified convert when deserialize the type instead of any converter that would otherwise match the type.
Example:
from databind.json.settings import JsonConverter
class MyCustomConverter(Converter):
def __init__(self, direction: Direction) -> None:
self.direction = direction
def convert(self, ctx: Context) -> Any:
...
@JsonConverter.using_classmethods(serialize="__str__", deserialize="of")
class MyCustomType:
def __str__(self) -> str:
...
@staticmethod
def of(s: str) -> MyCustomType:
...
The same override can also be achieved by attaching the setting to an Annotated
type hint:
Source code in databind/json/settings.py
JsonModule
Bases: Module
The JSON module combines all converters provided by the #databind.json package in one usable module. The direction in which the converters should convert must be specified with the direction argument. Alternatively, use one of the convenience static methods #serializing() and #deserializing().