README.md
1Serde YAML
2==========
3
4[![Build Status](https://api.travis-ci.org/dtolnay/serde-yaml.svg?branch=master)][travis]
5[![Latest Version](https://img.shields.io/crates/v/serde_yaml.svg)][crates.io]
6[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)][docs.rs]
7
8[travis]: https://travis-ci.org/dtolnay/serde-yaml
9[crates.io]: https://crates.io/crates/serde_yaml
10[docs.rs]: https://docs.rs/serde_yaml
11
12This crate is a Rust library for using the [Serde] serialization framework with
13data in [YAML] file format.
14
15[Serde]: https://github.com/serde-rs/serde
16[YAML]: http://yaml.org
17
18This library does not reimplement a YAML parser; it uses [yaml-rust] which is a
19pure Rust YAML 1.2 implementation.
20
21[yaml-rust]: https://github.com/chyh1990/yaml-rust
22
23## Dependency
24
25```toml
26[dependencies]
27serde = "1.0"
28serde_yaml = "0.8"
29```
30
31Release notes are available under [GitHub releases].
32
33[GitHub releases]: https://github.com/dtolnay/serde-yaml/releases
34
35## Using Serde YAML
36
37[API documentation is available in rustdoc form][docs.rs] but the general idea
38is:
39
40```rust
41use std::collections::BTreeMap;
42
43fn main() -> Result<(), serde_yaml::Error> {
44 // You have some type.
45 let mut map = BTreeMap::new();
46 map.insert("x".to_string(), 1.0);
47 map.insert("y".to_string(), 2.0);
48
49 // Serialize it to a YAML string.
50 let s = serde_yaml::to_string(&map)?;
51 assert_eq!(s, "---\nx: 1\ny: 2");
52
53 // Deserialize it back to a Rust type.
54 let deserialized_map: BTreeMap<String, f64> = serde_yaml::from_str(&s)?;
55 assert_eq!(map, deserialized_map);
56 Ok(())
57}
58```
59
60It can also be used with Serde's derive macros to handle structs and enums
61defined by your program.
62
63```toml
64[dependencies]
65serde = { version = "1.0", features = ["derive"] }
66serde_yaml = "0.8"
67```
68
69```rust
70use serde::{Serialize, Deserialize};
71
72#[derive(Debug, PartialEq, Serialize, Deserialize)]
73struct Point {
74 x: f64,
75 y: f64,
76}
77
78fn main() -> Result<(), serde_yaml::Error> {
79 let point = Point { x: 1.0, y: 2.0 };
80
81 let s = serde_yaml::to_string(&point)?;
82 assert_eq!(s, "---\nx: 1\ny: 2");
83
84 let deserialized_point: Point = serde_yaml::from_str(&s)?;
85 assert_eq!(point, deserialized_point);
86 Ok(())
87}
88```
89
90## License
91
92Licensed under either of
93
94 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
95 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
96
97at your option.
98
99### Contribution
100
101Unless you explicitly state otherwise, any contribution intentionally submitted
102for inclusion in Serde YAML by you, as defined in the Apache-2.0 license, shall
103be dual licensed as above, without any additional terms or conditions.
104