-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to configure custom JSON.parse reviver for the HttpClient #21079
Comments
You just ask for the body as text rather than json then parse it yourself |
@Toxicable That is what I am already doing with an interceptor. So it isn't a major priority and with interceptors I am already better off than I was with pre 4.3. But it would be nice to have a hook rather than copying/pasting that section of code to match the XSSI prefix striping, error handling, and whatever else changes in the future. I would also like to be able to set the proper responseType rather than changing it as a way to opt-out. |
+1. Same reason - for deserializing dates in a central place. |
@tade90 I'd say that the MVP is to allow a reviver to be specified. This will allow custom deserialization in general and not everyone will want to deserialize dates to the JavaScript Date object. People may want to deserialize to 3rd party representations (moment, js-joda, ...) that are better suited for their use-cases (handling timezones, date arithmetic, ...). To what you said about "turn date deserialization to Date objects on" we would first have to define what a date is since the JSON standard does not seem to specify a format for dates. There are several but I think that ISO 8601 is what most people probably want. Maybe angular could provide a reviver function that you could configure or even a flag, but if ISO 8601 is the standard then you can easily setup a reviver using |
Totally agree. Making the reviver able to be specified should be the base. Everthing from there would be sugar-coating. But it would be easier to use if there are pre-defined reviver functions for common use-cases like date serialization. This could be similar to the
ISO 8601 would fulfill my needs. |
I am also looking for a way to specify a custom reviver. My use case is also due to the need to handle dates. In this case ISO 8601 dates of the form "1999-02-28" which I want to revivify as Luxon DateTime objects. |
its such a common case im amazed this hasnt been implemented yet |
There should be a dependency injected service (E.x.: JsonSerializationService with a serialize/deserialize method) responsible for serialization that's used through angular so we can easily swap in our own serializer. The out of the box implementation should just call JSON.parse and JSON.stringify. It would also be great if the HttpClient had a property to allow getting/setting the serializer it will use. It will default to the dependency injected JsonSerializationService but exposing this allows specifying the serializer for that specific instance of HttpClient. This is all based on the premise that services can be replaced/overridden by reregistering them with 'providedIn: root'. If that's not possible, I'm hoping there are other means of doing so. |
+1 This would also be helpful in parsing JSON more accurately if the data contains large numbers and keeping significant digits is important with string conversion. |
@bygrace1986 reviver is not enough if You want to handle numbers with high precision like in |
This would also provide a way cleaner way to incorporate libraries like https://github.com/typestack/class-transformer |
+1 Really need to do this for efficiency because JSON.parse(reviver) is way faster than parsing and then looping through every property. |
This seems like a pretty common sense feature, although I am not surprised it has been lingering since 2017. With 2500 open issues, what does it take to actually get a feature like this to even be acknowledged by the Angular team? |
I do not know, maybe there is some magic upvote counter number, until then all high-voted issues & feture requests will be ignored by Angular team? |
+1. Lots of sneaky bugs can happen when an Angular newbie like myself was happily receiving responses with my TypeScript model classes happily saying "yes, this object is a Right now, getting around this not only requires being cognizant of this fact for every new HTTP call I make (sometimes the default parser is fine, sometimes it isn't), but also requires the same boilerplate code to be copy-pasted (ew!) just about everywhere. (Remember: Angular newbie. Most likely better ways but not from someone who's just onboarding to TypeScript, etc!) Please add this. |
+1 for having an angular native way to configure a custom replacer and reviver when doing JSON stringify and JSON parse of http requests, anyway i'm currently using an interceptor like this to do the job:
|
can't believe it is still not implemented |
We discussed this in the team meeting yesterday. The general consensus is that this is actually fairly straightforward to do via an interceptor and that the real problem is the lack of discoverability of this approach to solving the problem. We accept that the problem is compounded by the fact that The proposal then, is not to merge this feature into core, but instead to improve the documentation to lead developers clearly to an interceptor based solution. Here is a link to an example of how custom JSON parsing could be done in a reusable interceptor: https://stackblitz.com/edit/angular-ivy-nrdj5q?file=src%2Fapp%2Fapp.module.ts. Note that this could be wrapped up and distributed as a 3rd party library and then the application developer would only need to provide their own |
Update the HTTP guide and associated example to demonstrate how an interceptor can be used to provide a custom JSON parser. Resolves angular#21079
Update the HTTP guide and associated example to demonstrate how an interceptor can be used to provide a custom JSON parser. Resolves angular#21079
Update the HTTP guide and associated example to demonstrate how an interceptor can be used to provide a custom JSON parser. Resolves angular#21079
Update the HTTP guide and associated example to demonstrate how an interceptor can be used to provide a custom JSON parser. Resolves angular#21079
Update the HTTP guide and associated example to demonstrate how an interceptor can be used to provide a custom JSON parser. Resolves angular#21079
Update the HTTP guide and associated example to demonstrate how an interceptor can be used to provide a custom JSON parser. Resolves angular#21079
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
With the new HttpClient if the responseType is set to 'json' or if it is not set (defaulted to 'json') then the JSON is parsed before being returned to the consuming code. Here is the source-code where the JSON.parse occurs:
angular/packages/common/http/src/xhr.ts
Line 189 in 20e1cc0
Expected behavior
A reviver function can be configured that the HttpClient will use when parsing json. As far as the implementation, maybe an injection token can be exposed which developers can use to provide a reviver function.
What is the motivation / use case for changing the behavior?
Dates are deserialized as strings with JSON.parse. It is convenient to have dates serialized to Date objects globally via a centralized service using a reviver.
The text was updated successfully, but these errors were encountered: