fqr.core.codecs.utl module

Codecs utility functions.

encode(value: Any) bool | float | int | None | str | dict[bool | float | int | None | str, Serial] | list[Serial]

JSON encode value using a corresponding encoder, otherwise returns repr(value).

parse(value: AnyType, tp: type[VariadicArrayType] | type[ArrayType] | type[MappingType] | type[SupportsAnnotations] | type[AnyType]) VariadicArrayType | ArrayType | MappingType | SupportsAnnotations | AnyType | ParseErrorRef

Try to recursively parse python tp from value.

Value should either be an instance of tp or a valid, serialized representation of that tp (JSON string or otherwise).

Returns enm.ParseErrorRef if no valid type could be parsed, allowing for downstream validation instead of immediately raising an exception within this function.

serialize(value: Any) str

Convert value to string.

try_decode(value: Any, tp: type[AnyType]) AnyType | ParseErrorRef

Attempt to parse value, returning enm.ParseErrorRef if error.

try_parse_json(json_string: str) bool | float | int | None | str | dict[bool | float | int | None | str, Serial] | list[Serial] | ParseErrorRef

Attempt to parse valid JSON string, returning enm.ParseErrorRef if error.