File: README.md

package info (click to toggle)
rust-http-serde 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# [Serde][serde] support for the HTTP crate

Adds ability to serialize and deserialize types from the [HTTP][http] crate.

If you want to serialize `Request` or `Response`, use `into_parts()` and serialize their parts, and then rebuild them using their `Builder`.

[serde]: https://lib.rs/serde
[http]: https://lib.rs/http

## Usage

You must annotate fields with `#[serde(with = "http_serde::<appropriate method>")]`.

```rust
# use http::{*, uri::*};
#[derive(serde::Serialize, serde::Deserialize)]
struct MyStruct {
    #[serde(with = "http_serde::method")]
    method: Method,

    #[serde(with = "http_serde::status_code")]
    status: StatusCode,

    #[serde(with = "http_serde::uri")]
    uri: Uri,

    #[serde(with = "http_serde::header_map")]
    headers: HeaderMap,

    #[serde(with = "http_serde::authority")]
    authority: Authority,
}
```

There's also support for the types wrapped in an `Option`. To use it, change the `with` attribute prefix from `http_serde::` to `http_serde::option::`.

```rust
# use http::{*, uri::*};
#[derive(serde::Serialize, serde::Deserialize)]
struct MyStruct {
    #[serde(with = "http_serde::option::header_map")]
    //                          ^^^^^^
    optional_headers: Option<HeaderMap>,
}
```

## Requirements

* Rust 1.56 or later.