feat(http): support custom JSON parser in HttpClient#67258
Open
omatheusmesmo wants to merge 5 commits intoangular:mainfrom
Open
feat(http): support custom JSON parser in HttpClient#67258omatheusmesmo wants to merge 5 commits intoangular:mainfrom
omatheusmesmo wants to merge 5 commits intoangular:mainfrom
Conversation
52907e5 to
5d4f476
Compare
Defines the HttpJsonParser interface and the HTTP_JSON_PARSER InjectionToken to allow for custom JSON parsing logic. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
Updates HttpXhrBackend and FetchBackend to use the injected HTTP_JSON_PARSER for parsing JSON responses. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
Introduces the withJsonParser feature function to allow easy configuration of a custom JSON parser when using provideHttpClient. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
Exports HttpJsonParser, HTTP_JSON_PARSER, and withJsonParser from the public API and updates the API golden files. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
Includes tests for both XHR and Fetch backends covering basic parsing, XSSI stripping, and error handling. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
5d4f476 to
69d40d6
Compare
JeanMeche
reviewed
Feb 24, 2026
Comment on lines
+16
to
+18
| export abstract class HttpJsonParser { | ||
| abstract parse(text: string): any; | ||
| } |
Member
There was a problem hiding this comment.
any reasons you prefered having a class instead of a function ?
Author
There was a problem hiding this comment.
any reasons you prefered having a class instead of a function ?
@JeanMeche ,
I preferred an abstract class to follow the established architectural pattern in common/http (similar to HttpBackend or XhrFactory). This approach provides better extensibility if we decide to add related methods in the future, such as a custom serializer, without needing new injection tokens. It also makes it more straightforward to inject other dependencies into the parser if needed by complex implementations.
Should it be different?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Built-in JSON parsing in
HttpClientis not easily customizable. While interceptors can be used, they require manual handling of XSSI prefix stripping and consistent error mapping, which is error-prone and inconsistent with the framework's default behavior.Issue Number: #48167
What is the new behavior?
This PR introduces the
HttpJsonParserinterface andHTTP_JSON_PARSERinjection token, allowing developers to provide a custom JSON parsing implementation.HttpJsonParserabstract class andDefaultHttpJsonParserimplementation.HTTP_JSON_PARSERtoken to override the default parser.withJsonParser(parser: Type<HttpJsonParser>)feature forprovideHttpClient.HttpXhrBackendandFetchBackend.HttpJsonParseErrorhandling.Does this PR introduce a breaking change?
Other information