]>
Commit | Line | Data |
---|---|---|
f9f354fc | 1 | # Serde   [![Build Status]][travis] [![Latest Version]][crates.io] [![serde: rustc 1.13+]][Rust 1.13] [![serde_derive: rustc 1.31+]][Rust 1.31] |
041b39d2 XL |
2 | |
3 | [Build Status]: https://api.travis-ci.org/serde-rs/serde.svg?branch=master | |
4 | [travis]: https://travis-ci.org/serde-rs/serde | |
5 | [Latest Version]: https://img.shields.io/crates/v/serde.svg | |
6 | [crates.io]: https://crates.io/crates/serde | |
f9f354fc XL |
7 | [serde: rustc 1.13+]: https://img.shields.io/badge/serde-rustc_1.13+-lightgray.svg |
8 | [serde_derive: rustc 1.31+]: https://img.shields.io/badge/serde_derive-rustc_1.31+-lightgray.svg | |
9 | [Rust 1.13]: https://blog.rust-lang.org/2016/11/10/Rust-1.13.html | |
10 | [Rust 1.31]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html | |
7cac9316 XL |
11 | |
12 | **Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.** | |
13 | ||
14 | --- | |
15 | ||
16 | You may be looking for: | |
17 | ||
18 | - [An overview of Serde](https://serde.rs/) | |
19 | - [Data formats supported by Serde](https://serde.rs/#data-formats) | |
0731742a | 20 | - [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html) |
7cac9316 XL |
21 | - [Examples](https://serde.rs/examples.html) |
22 | - [API documentation](https://docs.serde.rs/serde/) | |
23 | - [Release notes](https://github.com/serde-rs/serde/releases) | |
24 | ||
25 | ## Serde in action | |
26 | ||
abe05a73 XL |
27 | <details> |
28 | <summary> | |
29 | Click to show Cargo.toml. | |
f9f354fc | 30 | <a href="https://play.rust-lang.org/?edition=2018&gist=72755f28f99afc95e01d63174b28c1f5" target="_blank">Run this code in the playground.</a> |
abe05a73 XL |
31 | </summary> |
32 | ||
33 | ```toml | |
34 | [dependencies] | |
35 | ||
36 | # The core APIs, including the Serialize and Deserialize traits. Always | |
f9f354fc XL |
37 | # required when using Serde. The "derive" feature is only required when |
38 | # using #[derive(Serialize, Deserialize)] to make Serde work with structs | |
39 | # and enums defined in your crate. | |
40 | serde = { version = "1.0", features = ["derive"] } | |
abe05a73 XL |
41 | |
42 | # Each data format lives in its own crate; the sample code below uses JSON | |
43 | # but you may be using a different one. | |
44 | serde_json = "1.0" | |
45 | ``` | |
46 | ||
47 | </details> | |
48 | <p></p> | |
7cac9316 XL |
49 | |
50 | ```rust | |
f9f354fc | 51 | use serde::{Serialize, Deserialize}; |
7cac9316 XL |
52 | |
53 | #[derive(Serialize, Deserialize, Debug)] | |
54 | struct Point { | |
55 | x: i32, | |
56 | y: i32, | |
57 | } | |
58 | ||
59 | fn main() { | |
60 | let point = Point { x: 1, y: 2 }; | |
61 | ||
62 | // Convert the Point to a JSON string. | |
63 | let serialized = serde_json::to_string(&point).unwrap(); | |
64 | ||
65 | // Prints serialized = {"x":1,"y":2} | |
66 | println!("serialized = {}", serialized); | |
67 | ||
68 | // Convert the JSON string back to a Point. | |
69 | let deserialized: Point = serde_json::from_str(&serialized).unwrap(); | |
70 | ||
71 | // Prints deserialized = Point { x: 1, y: 2 } | |
72 | println!("deserialized = {:?}", deserialized); | |
73 | } | |
74 | ``` | |
75 | ||
76 | ## Getting help | |
77 | ||
8faf50e0 XL |
78 | Serde developers live in the #serde channel on [`irc.mozilla.org`][irc]. The |
79 | \#rust channel is also a good resource with generally faster response time but | |
80 | less specific knowledge about Serde. If IRC is not your thing or you don't get a | |
81 | good response, we are happy to respond to [GitHub issues][issues] as well. | |
82 | ||
83 | [irc]: https://wiki.mozilla.org/IRC | |
84 | [issues]: https://github.com/serde-rs/serde/issues/new/choose | |
7cac9316 | 85 | |
f9f354fc | 86 | <br> |
7cac9316 | 87 | |
f9f354fc | 88 | #### License |
7cac9316 | 89 | |
f9f354fc XL |
90 | <sup> |
91 | Licensed under either of <a href="LICENSE-APACHE">Apache License, Version | |
92 | 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. | |
93 | </sup> | |
7cac9316 | 94 | |
f9f354fc | 95 | <br> |
7cac9316 | 96 | |
f9f354fc | 97 | <sub> |
7cac9316 XL |
98 | Unless you explicitly state otherwise, any contribution intentionally submitted |
99 | for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be | |
100 | dual licensed as above, without any additional terms or conditions. | |
f9f354fc | 101 | </sub> |