Skip to content

implement json.enable and json.dump_func configs - #1541

Merged
defnull merged 1 commit into
bottlepy:masterfrom
alisonatwork:fix-json-config
Sep 15, 2026
Merged

defnull merged 1 commit into
bottlepy:masterfrom
alisonatwork:fix-json-config

Conversation

@alisonatwork

Copy link
Copy Markdown
Contributor

As discussed on #1539, the json.enable config - which in 0.13 deprecated and replaced the autojson application constructor arg - does nothing. This pull request updates json.enable so that it behaves as autojson used to: an on/off switch for dict-to-JSON-by-default behavior. When it is off, dictionaries will be treated as iterable, resulting in the same output as a list or tuple of the keys.

I've also implemented the json.dump_func config option, which can be used to replace the default (CPython or UltraJSON) dumps.

There is a bit business logic complexity with the custom serialization function, because JSONPlugin still allows passing a json_dumps function in its constructor. In practice the only way to usefully do that in 0.13 was to uninstall the default JSONPlugin and reinstall a new one or else reach around and overwrite the json_dumps property on the installed plugin then call reset to reapply it (similar to solution described in #287 (comment)).

I think the best way to support backward-compatibility here is to ignore both the json.enable and json.dump_func configs if and only if the JSONPlugin had a custom json_dumps set on it (whether through constructor or property set). The downside of this is that if you install a custom JSONPlugin then you can't use config to enable or disable it per-route, but I am going to guess that this is not really a use case most people will ever have in real life.

I am not sure how deep to go with updating the docs to describe this behavior, so open the PR first to see. Maybe it doesn't need to be documented because the configs are basically enough and should be the standard way going forward?

@defnull

defnull commented Sep 15, 2026

Copy link
Copy Markdown
Member

Looks good to me already. Tests, backwards compatibility, docs, all covered. Very nice! Since you force-pushed just 10 minutes ago I'll still wait a bit :)

@alisonatwork

Copy link
Copy Markdown
Contributor Author

Yeah, sorry about that force-push, I realized the commit was connected to my work email address vs my personal one. Not a big deal, since we use Bottle at work, but just for consistency I like to try keep the same email everywhere. If you are happy with the MR, feel free to merge 👍

@defnull
defnull merged commit 90bd4aa into bottlepy:master Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants