Skip to content

Support request-aware JSON replacers - #7603

Open
AjnasNB wants to merge 2 commits into
expressjs:masterfrom
AjnasNB:proposal/json-replacer-factory
Open

AjnasNB wants to merge 2 commits into
expressjs:masterfrom
AjnasNB:proposal/json-replacer-factory

Conversation

@AjnasNB

@AjnasNB AjnasNB commented Nov 21, 2026

Copy link
Copy Markdown

Express applications that need JSON formatting based on the current request or response have to wrap serialization at each call site. This adds an opt-in json replacer factory setting shared by res.json() and res.jsonp(). The factory receives (req, res) once and returns a native JSON replacer. With the setting unset, Express uses the existing app-wide replacer.

The included example shows one use case for #4755: format a response according to request parameters and response locals. This is an alternative API to #3981. Maintainer feedback on whether this belongs in core would be welcome.

Validation: 1,381 tests passed on Node 24.22.3 (Windows). ESLint and git diff --check passed.

OpenAI Codex assisted with the implementation, tests, and PR description.

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.

1 participant