edn_format
edn_format copied to clipboard
Custom types for dumps()
As far as I can tell, currently the only branch in udump() that allows serialization of custom types goes through TaggedElement. One must subclass this and implement __str__(). IMO this is problematic for two reasons:
- One may not control the class definition at all so that subclassing is not an option.
- Using
__str__for EDN serialization might be undesirable. Very often one wants simpler representations for debugging from this method. - It is not always the case that a custom type must be tagged. One can of course just not return any, but that makes the semantics of
TaggedElementconfusing.
I think all of these can be overcome by allowing registering custom handlers for arbitrary types, exactly in the same manner that @tag does. These would be based on type instead of string. One could add some syntactic sugar with a decorator as well.
Or is there some other way I missed?
The simplest implementation would probably be an optional default parameter like json.dump that would be called on objects unsupported by the serializer.