Module aws_lambda_powertools.event_handler.openapi.encoders

Functions

def decimal_encoder(dec_value: decimal.Decimal) ‑> Union[int, float]

Encodes a Decimal as int of there's no exponent, otherwise float

This is useful when we use ConstrainedDecimal to represent Numeric(x,0) where an integer (but not int typed) is used. Encoding this as a float results in failed round-tripping between encode and parse.

>>> decimal_encoder(Decimal("1.0"))
1.0
>>> decimal_encoder(Decimal("1"))
1
def generate_encoders_by_class_tuples(type_encoder_map: Dict[Any, Callable[[Any], Any]]) ‑> Dict[Callable[[Any], Any], Tuple[Any, ...]]
def iso_format(o: Union[datetime.date, datetime.time]) ‑> str

ISO format for date and time

def jsonable_encoder(obj: Any, include: Union[Set[int], Set[str], Dict[int, Any], Dict[str, Any], ForwardRef(None)] = None, exclude: Union[Set[int], Set[str], Dict[int, Any], Dict[str, Any], ForwardRef(None)] = None, by_alias: bool = True, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, custom_serializer: Optional[Callable[[Any], str]] = None) ‑> Any

JSON encodes an arbitrary Python object into JSON serializable data types.

This is a modified version of fastapi.encoders.jsonable_encoder that supports encoding of pydantic.BaseModel objects.

Parameters

obj : Any
The object to encode
include : Optional[IncEx], optional
A set or dictionary of strings that specifies which properties should be included, by default None, meaning everything is included
exclude : Optional[IncEx], optional
A set or dictionary of strings that specifies which properties should be excluded, by default None, meaning nothing is excluded
by_alias : bool, optional
Whether field aliases should be respected, by default True
exclude_unset : bool, optional
Whether fields that are not set should be excluded, by default False
exclude_defaults : bool, optional
Whether fields that are equal to their default value (as specified in the model) should be excluded, by default False
exclude_none : bool, optional
Whether fields that are equal to None should be excluded, by default False
custom_serializer : Callable, optional
A custom serializer to use for encoding the object, when everything else fails.

Returns

Any
The JSON serializable data types