edn_format icon indicating copy to clipboard operation
edn_format copied to clipboard

Custom types for dumps()

Open mdbenito opened this issue 7 months ago • 1 comments

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:

  1. One may not control the class definition at all so that subclassing is not an option.
  2. Using __str__ for EDN serialization might be undesirable. Very often one wants simpler representations for debugging from this method.
  3. 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 TaggedElement confusing.

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?

mdbenito avatar Jul 12 '25 13:07 mdbenito

The simplest implementation would probably be an optional default parameter like json.dump that would be called on objects unsupported by the serializer.

bfontaine avatar Jul 15 '25 11:07 bfontaine