Skip to content

Commit f12c062

Browse files
committed
[bitreq] Check utf-8 while parsing JSON responses
While deserializing, `serde_json::from_slice` validates utf-8 as needed. So instead of making two passes on the response body, one to validate utf-8, and another to deserialize the object, we let `serde_json::from_slice` check utf-8 as needed during deserialization. Making a single pass over large response bodies reduces the number of cache misses, and hence decreases the cycles taken to fully deserialize such responses. `Response::json` now returns `Error::SerdeJsonError` instead of `Error::InvalidUtf8InBody` if invalid utf-8 is found during deserialization. For this error case, the `Error::SerdeJsonError` inner type `serde_json::error::Error` is of category `serde_json::error::Category::Syntax`. Other JSON syntax errors are also assigned to this category. We accept this loss of information given the performance gain described above.
1 parent d31fc06 commit f12c062

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

bitreq/src/response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl Response {
248248
where
249249
T: serde::de::Deserialize<'a>,
250250
{
251-
match serde_json::from_str(self.as_str()?) {
251+
match serde_json::from_slice(self.as_bytes()) {
252252
Ok(json) => Ok(json),
253253
Err(err) => Err(Error::SerdeJsonError(err)),
254254
}

0 commit comments

Comments
 (0)