hyperium / http

Rust HTTP types
Apache License 2.0
1.15k stars 285 forks source link

Add optional serde support for top level types #631

Closed andrewtoth closed 11 months ago

andrewtoth commented 11 months ago

This adds serde::Serialize and serde::Deserialize trait implementations for all types re-exported at the root level, except for Extensions, Result, and Error. serde is added as an optional dependency behind the feature flag serde1.

These are HeaderMap, HeaderName, HeaderValue, Method, Request, Response, StatusCode, Uri, Version, request::Parts, response::Parts. Note for Request and Response it will fail serialization if extensions is non-empty, and will deserialize to a Extensions::default().

Current workarounds are to use the http_serde crate, but that requires annotating #[serde(with)] and doesn't support using Option or other container types. Suggested alternatives are quite complex.

There was a previous attempt just for StatusCode in https://github.com/hyperium/http/pull/274 which seems to have gone stale.

Implements https://github.com/hyperium/http/issues/397, https://github.com/hyperium/http/issues/273, https://github.com/hyperium/http/issues/434

seanmonstar commented 11 months ago

Reading the old issue, it's been a while since I've thought about this. With some of the ecosystem recently worried about serde (not that I particularly agree with the worry itself), I find myself leaning back towards not including it directly in http. 🙈

Other maintainers could disagree. But it seems sufficiently solvable with a separate crate.

andrewtoth commented 11 months ago

A fair point. Thank you for sharing your thoughts on this. I've created a crate to solve this using the serde(with = "...") attributes in case anyone else comes here looking for a solution. https://crates.io/crates/http-serde-ext