[ENG-759] [ENG-1104] patch `json.dumps` to handle `__wrapped__` objects (#4166)
* [ENG-1104] patch `json.dumps` to handle `__wrapped__` objects
Unwrap proxied objects if the default serializer doesn't work.
* pre-commit fixup
* Skip default fallback logic when `cls` is specified
`cls` will provide its own default serialization mechanism, passing a `cls`
Encoder class is now also a way to opt-out of our patching shenanigans and just
use your own code.
This will work, provided the library doing the JSON encoding isn't also using
their own custom class.
* Override JSONEncoder.default instead of json.dumps
Many libraries (like httpx, used by openai), will use `from json import dumps`,
and if they do that before `reflex.state` gets imported, then they will get the
original dumps function instead of our patched one.
To workaround this, monkeypatch the `JSONEncoder.default` function instead.
This is also nicer behavior for custom subclasses of JSONEncoder; if someone
wants to opt-out of our monkeypatching, they can simply not call
`super().default(o)` in their subclass, which by default only raises a
TypeError.
---------
Co-authored-by: Nikhil Rao <nikhil@reflex.dev>