Skip to content

pythonjsonlogger.defaults 🔗

Collection of functions for building custom json_default functions.

In general functions come in pairs of use_x_default and x_default, where the former is used to determine if you should call the latter.

Most use_x_default functions also act as a TypeGuard.

bytes_default 🔗

bytes_default(
    obj: bytes | bytearray, url_safe: bool = True
) -> str

Default function for bytes

PARAMETER DESCRIPTION
obj

object to handle

TYPE: bytes | bytearray

url_safe

use URL safe base 64 character set.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
str

The byte data as a base 64 string.

dataclass_default 🔗

dataclass_default(obj) -> dict[str, Any]

Default function for dataclass instances

PARAMETER DESCRIPTION
obj

object to handle

date_default 🔗

date_default(obj: date) -> str

Default function for datetime.date instances

PARAMETER DESCRIPTION
obj

object to handle

TYPE: date

datetime_any 🔗

datetime_any(obj: time | date | date) -> str

Default function for datetime related instances

PARAMETER DESCRIPTION
obj

object to handle

TYPE: time | date | date

datetime_default 🔗

datetime_default(obj: datetime) -> str

Default function for datetime.datetime instances

PARAMETER DESCRIPTION
obj

object to handle

TYPE: datetime

enum_default 🔗

enum_default(obj: Enum | EnumMeta) -> Any | list[Any]

Default function for enums.

Supports both enum classes and enum values.

PARAMETER DESCRIPTION
obj

object to handle

TYPE: Enum | EnumMeta

exception_default 🔗

exception_default(obj: BaseException) -> str

Default function for exception instances

PARAMETER DESCRIPTION
obj

object to handle

TYPE: BaseException

time_default 🔗

time_default(obj: time) -> str

Default function for datetime.time instances

PARAMETER DESCRIPTION
obj

object to handle

TYPE: time

traceback_default 🔗

traceback_default(obj: TracebackType) -> str

Default function for tracebacks

PARAMETER DESCRIPTION
obj

object to handle

TYPE: TracebackType

type_default 🔗

type_default(obj: type) -> str

Default function for type objects.

PARAMETER DESCRIPTION
obj

object to handle

TYPE: type

unknown_default 🔗

unknown_default(obj: Any) -> str

Backup default function for any object type.

Will attempt to use str or repr. If both functions error will return the string "__could_not_encode__".

PARAMETER DESCRIPTION
obj

object to handle

TYPE: Any

use_bytes_default 🔗

use_bytes_default(obj: Any) -> TypeGuard[bytes | bytearray]

Default check function for bytes

use_dataclass_default 🔗

use_dataclass_default(obj: Any) -> bool

Default check function for dataclass instances

use_date_default 🔗

use_date_default(obj: Any) -> TypeGuard[date]

Default check function for datetime.date instances

use_datetime_any 🔗

use_datetime_any(
    obj: Any,
) -> TypeGuard[time | date | datetime]

Default check function for datetime related instances

use_datetime_default 🔗

use_datetime_default(obj: Any) -> TypeGuard[datetime]

Default check function for datetime.datetime instances

use_enum_default 🔗

use_enum_default(obj: Any) -> TypeGuard[Enum | EnumMeta]

Default check function for enums.

Supports both enum classes and enum values.

use_exception_default 🔗

use_exception_default(obj: Any) -> TypeGuard[BaseException]

Default check function for exception instances.

Exception classes are not treated specially and should be handled by the [use_]type_default functions.

use_time_default 🔗

use_time_default(obj: Any) -> TypeGuard[time]

Default check function for datetime.time instances

use_traceback_default 🔗

use_traceback_default(obj: Any) -> TypeGuard[TracebackType]

Default check function for tracebacks

use_type_default 🔗

use_type_default(obj: Any) -> TypeGuard[type]

Default check function for type objects (aka classes).

use_uuid_default 🔗

use_uuid_default(obj: Any) -> TypeGuard[UUID]

Default check function for uuid.UUID instances

uuid_default 🔗

uuid_default(obj: UUID) -> str

Default function for uuid.UUID instances

Formats the UUID using "hyphen" format.

PARAMETER DESCRIPTION
obj

object to handle

TYPE: UUID