Python’s standardDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ismael-sarmiento/kimera_python/llms.txt
Use this file to discover all available pages before exploring further.
json.dumps rejects many common types — datetime, date, tuple, and nested combinations of these will raise a TypeError at runtime. KimeraSerializer solves this by routing each object through a prioritized chain of handler classes, each responsible for encoding and decoding one specific Python type into a JSON-safe envelope. The result is a fully round-trippable JSON string that can be stored in a cache, sent over a network, or written to a file and reconstructed back to the original Python object without any manual type hints.
KimeraSerializer
KimeraSerializer is a composed class that holds an ordered list of BaseSerializerHandler instances. When you call serialize, it finds the first handler whose can_i_handle_object_to_serialize returns True and delegates to that handler’s build_serializer. Deserialization follows the same pattern in reverse.
Supported Types
Primitives
str, int, float, and bool are handled by DefaultSerializerHandler.Collections
list and tuple are handled element-by-element, preserving nesting.Mappings
dict is handled key-by-key, with each value serialized recursively.Date
datetime.date is serialized using the format %Y%m%d.Datetime
datetime.datetime is serialized using %Y%m%d %H:%M:%S.%f.Nested
Any combination of the above types nested arbitrarily deep.
Methods
serialize(object) -> str
serialize(object) -> str
Finds the first handler whose
can_i_handle_object_to_serialize returns True and calls its build_serializer. Returns a JSON string envelope of the form {'data': ..., 'serializer': '<HandlerClassName>'}.deserialize(object) -> object
deserialize(object) -> object
Reads the
serializer field from a JSON envelope string to identify the correct handler, then calls build_deserializer to reconstruct the original Python object.get_serializer_from_object(object) -> BaseSerializerHandler
get_serializer_from_object(object) -> BaseSerializerHandler
Filters the handler list for the first handler whose
can_i_handle_object_to_serialize returns True for the given object and returns that handler instance. Raises an Exception if no handler is found.get_deserializer_from_object(object) -> BaseSerializerHandler
get_deserializer_from_object(object) -> BaseSerializerHandler
Filters the handler list for the first handler whose
can_i_handle_object_to_deserialize returns True for the given serialized string and returns that handler instance. Raises an Exception if no handler is found.can_any_advanced_serializer_handle_this_object(object) -> bool
can_any_advanced_serializer_handle_this_object(object) -> bool
Returns
True if any handler except DefaultSerializerHandler (the last entry) can handle the object for serialization. Used by KimeraEncoder.default to decide whether to delegate to KimeraSerializer or fall back to the standard encoder.Full Round-Trip Example
Custom user-defined class instances that are not in the handler list will fall through to
DefaultSerializerHandler, which passes the object to the standard json.JSONEncoder. If the encoder cannot handle the type, a TypeError is raised. Extend BaseSerializerHandler and add your handler to a custom KimeraSerializer subclass to support additional types.Handler Chain
KimeraSerializer.__init__ builds the following ordered list. The first handler whose can_i_handle_object_to_serialize(obj) returns True is used:
DateSerializerHandler
Matches
datetime.date objects where type(obj) == date exactly (not datetime subclass). Encodes using %Y%m%d; decodes back to date.DateTimeSerializerHandler
Matches
datetime.datetime instances (isinstance(obj, datetime)). Encodes using %Y%m%d %H:%M:%S.%f; decodes back to datetime.DictSerializerHandler
Matches any
dict. Each value is recursively serialized by calling KimeraSerializer().serialize(value).TupleSerializerHandler
Matches any
tuple. Each element is recursively serialized; deserialization reconstructs a tuple.ListSerializerHandler
Matches any
list. Each element is recursively serialized; deserialization reconstructs a list.KimeraEncoder / KimeraDecoder
KimeraEncoder and KimeraDecoder are json.JSONEncoder / json.JSONDecoder subclasses that integrate directly with Python’s standard json module. Use them when you already have code using json.dumps / json.loads and want Kimera’s type support with minimal refactoring.
How They Work
KimeraEncoder.default(_object)— called byjson.dumpsfor any object it cannot natively serialize. It delegates toKimeraSerializerif a non-default handler can process the object; otherwise it falls back tosuper().default().KimeraDecoder.object_hook(_object)— called byjson.loadsfor every decoded JSON object (dict). If the dict looks like a Kimera envelope ({'serializer': ..., 'data': ...}), it reconstructs the original Python type viaKimeraSerializer().deserialize.
Example
Pickle Serializer
ThePickle class is a thin, stateless wrapper around Python’s built-in pickle module that provides a consistent method-naming convention matching the rest of Kimera’s serializer API.
Methods
Pickle.serializer(object) -> bytes
Pickle.serializer(object) -> bytes
Calls
pickle.dumps(_object) and returns the binary byte string representation of the Python object.Pickle.deserializer(bytes) -> object
Pickle.deserializer(bytes) -> object
Calls
pickle.loads(serializer_object) and reconstructs the original Python object from its binary representation.