Skip to content

[RFC] - Easier Angular HttpClient serialization #63

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

Closed
ms1111 opened this issue Nov 1, 2020 · 3 comments · Fixed by #84
Closed

[RFC] - Easier Angular HttpClient serialization #63

ms1111 opened this issue Nov 1, 2020 · 3 comments · Fixed by #84
Assignees
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested

Comments

@ms1111
Copy link
Collaborator

ms1111 commented Nov 1, 2020

Easier Angular HttpClient serialization

Expose toPojo() in Serializable or make toJson() return a JsonObject (related to #59).

Motivation

In Angular, if you do something like:

return this.httpClient.post(this.url, mySerializableObject.toJson());

... it tries to send the JSON string as Content-type: text/plain instead of application/json.

Design Detail

Add a method to the Serializable class expose the internal serialized representation (the POJO before JSON.stringify()). In Angular you could then do:

return this.httpClient.post(this.url, mySerializableObject.toPojo());

Drawbacks

  • more noise/confusion in the API

Rationale and Alternatives

Alternatives:

  1. Some kind of HttpClient intereptor to detect a Serializable object and set the character set appropriately. The caller would then do:
return this.httpClient.post(this.url, mySerializableObject); // some magic will call toJson() on the object
  1. Or, callers could set HttpClient options to force the right content type on each post/put call.
const httpOptions = {
    headers: new HttpHeaders({
        'Content-Type': 'application/json',
    })
};
return this.httpClient.post(this.url, mySerializableObject.toJson(), httpOptions);

Prior Art

Unresolved questions

@ms1111 ms1111 added question Further information is requested enhancement New feature or request and removed question Further information is requested labels Nov 1, 2020
@hardy925 hardy925 added the question Further information is requested label Nov 4, 2020
@hardy925
Copy link
Contributor

hardy925 commented Nov 4, 2020

Personally, I am leaning more on this being an angular issue. HttpClient tries to infer the content-type base on the type of. the second value. I like that there are options to solve this outside of the project but I a unsure of the best approach

@hardy925 hardy925 removed the enhancement New feature or request label Nov 5, 2020
@hardy613 hardy613 added help wanted Extra attention is needed documentation Improvements or additions to documentation labels Nov 12, 2020
@ms1111
Copy link
Collaborator Author

ms1111 commented Nov 26, 2020

I think Scott had the idea of exposing a Serializable.tsSerialize() method that would return the object in JsonObject form that Angular or other frameworks could use.

(i.e. rename toPojo() to tsSerialize() and make it public)

@hardy925
Copy link
Contributor

it was your idea!

@hardy925 hardy925 self-assigned this Nov 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested
Projects
None yet
3 participants