I come from here: Subclassed json.JSONEncoder does not respect default method for supported types · Issue #74528 · python/cpython · GitHub
As mentioned, I need to serialize a class (which is a 128-bit value which extends the standard int
class) into an RFC-4122 compliant string, and the fact that I can’t override int
serialization (which, of course, cuts off my int
s because of numbers-as-floats in JSON…) seems bonkers.
The original author of the PR had done good work, but it was refused on the grounds of “needing more discussion”, which I understand, given that their change was potentially breaking, and if you want to do that kind of change, better do it well and durably.
However, all this new feature really needs to be non-breaking is to add an “override()” method to the JSONEncoder class. It would basically “pass” in the implementation of JSONEncoder, but you could extend it like you would “default()” currently. This “override()” method would simply be read before the standard conversions, unlike “default()”.
In the meantime, I’ll be writing my own “json.dumps preprocessor” for my code, but this seems like a lot of trouble for a feature which should absolutely be standard.
If there is any better way to override the JSON serialization for standard types, or classes that extend standard types, I’m all ears.