You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now, dec_hooks and enc_hooks are called only if msgspec encounters unknown type. I'd like a way to override dec/enc logic for specific supported types, but there is no way to do it rn. Possible solutions:
add new argument with Dict[Type[T], Callable[[T], Any] that works even if T is subclass of Struct or any other supported type
inspect argument type annotation for callables passed to dec_hooks and enc_hooks. If it is annotated with some type, use hook even if type supported natively
introduce special (possibly dunder method) that can override default enc/dec logic. It can look something like this in python: decoded = if hasattr(obj.__class__, "__decode__") obj.__decode__(obj) else <native logic>. This will only work for Struct subclasses though
The text was updated successfully, but these errors were encountered:
mike0sv
changed the title
Support adding hooks for supported objects
Support adding hooks for supported objects types
Apr 19, 2024
we have the same problem.
msgspec json encoder supports a lot more types by default which sometimes isn't what we want(ex. sometimes we'd rather have serialization to fail rather than letting it pass and create an encoded data that we wouldn't know what type to decode it to by the time it gets to deserialization step).
and sometimes the way that msgspec json serializes certain supported types just isn't what we want.
both of these issues can be dealt with if there's a way to override behaviour for certain supported types.
Description
Right now,
dec_hooks
andenc_hooks
are called only if msgspec encounters unknown type. I'd like a way to override dec/enc logic for specific supported types, but there is no way to do it rn. Possible solutions:Dict[Type[T], Callable[[T], Any]
that works even if T is subclass of Struct or any other supported typedec_hooks
andenc_hooks
. If it is annotated with some type, use hook even if type supported nativelydecoded = if hasattr(obj.__class__, "__decode__") obj.__decode__(obj) else <native logic>
. This will only work for Struct subclasses thoughThe text was updated successfully, but these errors were encountered: