Skip to content

Comments

feat(http): support custom JSON parser in HttpClient#67258

Open
omatheusmesmo wants to merge 5 commits intoangular:mainfrom
omatheusmesmo:feat/http-custom-json-parser-signed
Open

feat(http): support custom JSON parser in HttpClient#67258
omatheusmesmo wants to merge 5 commits intoangular:mainfrom
omatheusmesmo:feat/http-custom-json-parser-signed

Conversation

@omatheusmesmo
Copy link

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Built-in JSON parsing in HttpClient is 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 HttpJsonParser interface and HTTP_JSON_PARSER injection token, allowing developers to provide a custom JSON parsing implementation.

  • New HttpJsonParser abstract class and DefaultHttpJsonParser implementation.
  • HTTP_JSON_PARSER token to override the default parser.
  • withJsonParser(parser: Type<HttpJsonParser>) feature for provideHttpClient.
  • Integrated into both HttpXhrBackend and FetchBackend.
  • Maintains automatic XSSI prefix stripping and consistent HttpJsonParseError handling.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@pullapprove pullapprove bot requested a review from AndrewKushnir February 24, 2026 19:54
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: common/http Issues related to HTTP and HTTP Client labels Feb 24, 2026
@ngbot ngbot bot added this to the Backlog milestone Feb 24, 2026
@omatheusmesmo omatheusmesmo force-pushed the feat/http-custom-json-parser-signed branch from 52907e5 to 5d4f476 Compare February 24, 2026 20:46
@angular-robot angular-robot bot added detected: breaking change PR contains a commit with a breaking change area: docs Related to the documentation area: build & ci Related the build and CI infrastructure of the project area: core Issues related to the framework runtime area: router area: devtools area: vscode-extension Issues related to the Angular Language Service VsCode extension labels Feb 24, 2026
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>
@omatheusmesmo omatheusmesmo force-pushed the feat/http-custom-json-parser-signed branch from 5d4f476 to 69d40d6 Compare February 24, 2026 20:56
Comment on lines +16 to +18
export abstract class HttpJsonParser {
abstract parse(text: string): any;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reasons you prefered having a class instead of a function ?

Copy link
Author

@omatheusmesmo omatheusmesmo Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@JeanMeche JeanMeche removed area: build & ci Related the build and CI infrastructure of the project area: router area: core Issues related to the framework runtime area: devtools detected: breaking change PR contains a commit with a breaking change area: docs Related to the documentation area: vscode-extension Issues related to the Angular Language Service VsCode extension labels Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: common/http Issues related to HTTP and HTTP Client detected: feature PR contains a feature commit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants