allow send_json to send any serde::Serialize value #446
+33
−29
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.
I am currently writing a project using
ureq
with thejson
feature enabled and noticed the following which confused me while trying to understand the API:send_json
does not allow users to send anyserde::Serialize
-able data and instead requires converting data into aserde_json::Value
before sending. This is inefficient, since data that could be serialize immediately needs to be converted into an intermediate in-memory JSON format instead.serde_json
types it usesThis PR:
send_json
to allow for any type implementingserde::Serialize
(includingserde_json::Value
) to be sent. ¹serde_json
types in the API with the proper names fromserde_json
serde_json
serde_json
andserde
¹ this is achieved by serializing the data into a
Vec<u8>
insend_json
and passing that throughPayload::Bytes
.This has the effect that the crate internal
Debug
implementation now prints the bytes of the json data instead. It would be possible to usePayload::Text
with the"utf8"
encoding instead, but the risk of encoding errors seems to outweight the minor benefit in debugging to me.