djblets.util.serializers
Utilities for serializing content.
-
class DjbletsJSONEncoder(strip_datetime_ms: bool = True, *args, **kwargs)[source]
Bases: DjangoJSONEncoder
A JSON encoder that supports lazy strings, datetimes, and other objects.
This is a specialization of
DjangoJSONEncoder the does the
following:
Removes the milliseconds and microseconds
from datetimes (unless setting
strip_datetime_ms=False when constructing the encoder). This is
to help keep timestamps from appearing too new when compared against
data coming from a MySQL database (which historically, and by default,
chops off milliseconds).
Serializes objects (including Django models with) containing a to_json method.
-
__init__(strip_datetime_ms: bool = True, *args, **kwargs) → None[source]
Initialize the encoder.
- Parameters:
strip_datetime_ms (bool, optional) –
Determines whether milliseconds should be stripped from a
datetime.
This is True by default, to preserve the old behavior of
the encoder.
-
strip_datetime_ms: bool
Whether milliseconds should be stripped from a datetime.
- Type:
bool
-
__annotations__ = {'strip_datetime_ms': 'bool'}
-
default(obj: djblets.util.typing.SerializableJSONValue) → djblets.util.typing.SerializableJSONValue[source]
Encode the object into a JSON-compatible structure.
The result from this will be re-encoded as JSON.
- Parameters:
obj (object) – The object to encode.
- Returns:
A JSON-compatible value, or a value that can be serialized to
JSON through this encoder.
- Return type:
object