serde-yaml/README.md

153 lines
4.2 KiB
Markdown
Raw Permalink Normal View History

2016-10-14 05:48:47 +00:00
Serde YAML
==========
2016-02-22 06:47:32 +00:00
2020-06-10 07:02:06 +00:00
[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/serde--yaml-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/serde-yaml)
[<img alt="crates.io" src="https://img.shields.io/crates/v/serde_yaml.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/serde_yaml)
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-serde__yaml-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/serde_yaml)
2022-12-16 01:52:43 +00:00
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/dtolnay/serde-yaml/ci.yml?branch=master&style=for-the-badge" height="20">](https://github.com/dtolnay/serde-yaml/actions?query=branch%3Amaster)
2018-08-23 12:51:23 +00:00
2024-03-25 00:39:45 +00:00
Rust library for using the [Serde] serialization framework with data in [YAML]
file format. _(This project is no longer maintained.)_
2018-08-30 13:57:24 +00:00
[Serde]: https://github.com/serde-rs/serde
2021-02-17 22:04:12 +00:00
[YAML]: https://yaml.org/
2018-08-30 13:57:24 +00:00
2016-10-14 05:48:47 +00:00
## Dependency
2016-02-22 06:47:32 +00:00
```toml
[dependencies]
2017-09-23 12:01:32 +00:00
serde = "1.0"
2022-07-28 19:51:44 +00:00
serde_yaml = "0.9"
2016-02-22 06:47:32 +00:00
```
2018-08-30 13:57:24 +00:00
Release notes are available under [GitHub releases].
[GitHub releases]: https://github.com/dtolnay/serde-yaml/releases
2016-03-21 01:15:36 +00:00
2016-03-21 03:05:28 +00:00
## Using Serde YAML
2016-02-22 06:47:32 +00:00
2018-08-30 13:57:24 +00:00
[API documentation is available in rustdoc form][docs.rs] but the general idea
is:
2020-06-10 07:02:06 +00:00
[docs.rs]: https://docs.rs/serde_yaml
2016-02-22 06:47:32 +00:00
```rust
use std::collections::BTreeMap;
2019-04-30 09:16:46 +00:00
fn main() -> Result<(), serde_yaml::Error> {
2016-10-14 05:44:47 +00:00
// You have some type.
2016-02-22 06:47:32 +00:00
let mut map = BTreeMap::new();
map.insert("x".to_string(), 1.0);
map.insert("y".to_string(), 2.0);
2016-10-14 05:44:47 +00:00
// Serialize it to a YAML string.
2022-07-28 19:50:35 +00:00
let yaml = serde_yaml::to_string(&map)?;
assert_eq!(yaml, "x: 1.0\ny: 2.0\n");
2016-02-22 06:47:32 +00:00
2016-10-14 05:44:47 +00:00
// Deserialize it back to a Rust type.
2022-07-28 19:50:35 +00:00
let deserialized_map: BTreeMap<String, f64> = serde_yaml::from_str(&yaml)?;
2016-02-22 06:47:32 +00:00
assert_eq!(map, deserialized_map);
2019-04-30 09:16:46 +00:00
Ok(())
2016-02-22 06:47:32 +00:00
}
```
2019-04-30 08:58:15 +00:00
It can also be used with Serde's derive macros to handle structs and enums
2022-07-28 19:50:35 +00:00
defined in your program.
2016-02-22 06:47:32 +00:00
```toml
[dependencies]
2019-04-30 08:58:15 +00:00
serde = { version = "1.0", features = ["derive"] }
2022-07-28 19:51:44 +00:00
serde_yaml = "0.9"
2016-02-22 06:47:32 +00:00
```
2022-07-28 19:50:35 +00:00
Structs serialize in the obvious way:
2016-02-22 06:47:32 +00:00
```rust
2019-04-30 08:58:15 +00:00
use serde::{Serialize, Deserialize};
2016-02-22 06:47:32 +00:00
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Point {
x: f64,
y: f64,
}
2019-04-30 09:16:46 +00:00
fn main() -> Result<(), serde_yaml::Error> {
2016-02-22 06:47:32 +00:00
let point = Point { x: 1.0, y: 2.0 };
2022-07-28 19:50:35 +00:00
let yaml = serde_yaml::to_string(&point)?;
assert_eq!(yaml, "x: 1.0\ny: 2.0\n");
2016-02-22 06:47:32 +00:00
2022-07-28 19:50:35 +00:00
let deserialized_point: Point = serde_yaml::from_str(&yaml)?;
2016-02-22 06:47:32 +00:00
assert_eq!(point, deserialized_point);
2019-04-30 09:16:46 +00:00
Ok(())
2016-02-22 06:47:32 +00:00
}
```
2016-02-27 22:42:06 +00:00
2022-07-28 19:50:35 +00:00
Enums serialize using YAML's `!tag` syntax to identify the variant name.
```rust
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum Enum {
Unit,
Newtype(usize),
Tuple(usize, usize, usize),
Struct { x: f64, y: f64 },
}
fn main() -> Result<(), serde_yaml::Error> {
let yaml = "
- !Newtype 1
- !Tuple [0, 0, 0]
- !Struct {x: 1.0, y: 2.0}
";
let values: Vec<Enum> = serde_yaml::from_str(yaml).unwrap();
assert_eq!(values[0], Enum::Newtype(1));
assert_eq!(values[1], Enum::Tuple(0, 0, 0));
assert_eq!(values[2], Enum::Struct { x: 1.0, y: 2.0 });
// The last two in YAML's block style instead:
let yaml = "
- !Tuple
- 0
- 0
- 0
- !Struct
x: 1.0
y: 2.0
";
let values: Vec<Enum> = serde_yaml::from_str(yaml).unwrap();
assert_eq!(values[0], Enum::Tuple(0, 0, 0));
assert_eq!(values[1], Enum::Struct { x: 1.0, y: 2.0 });
// Variants with no data can be written using !Tag or just the string name.
let yaml = "
- Unit # serialization produces this one
- !Unit
";
let values: Vec<Enum> = serde_yaml::from_str(yaml).unwrap();
assert_eq!(values[0], Enum::Unit);
assert_eq!(values[1], Enum::Unit);
Ok(())
}
```
2019-05-19 00:16:05 +00:00
<br>
2016-02-27 22:42:06 +00:00
2019-05-19 00:16:05 +00:00
#### License
2016-02-27 22:42:06 +00:00
2019-05-19 00:16:05 +00:00
<sup>
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
</sup>
2016-02-27 22:42:06 +00:00
2019-05-19 00:16:05 +00:00
<br>
2016-02-27 22:42:06 +00:00
2019-05-19 00:16:05 +00:00
<sub>
2016-02-27 22:42:06 +00:00
Unless you explicitly state otherwise, any contribution intentionally submitted
2019-05-19 00:16:05 +00:00
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
2016-02-27 22:42:06 +00:00
be dual licensed as above, without any additional terms or conditions.
2019-05-19 00:16:05 +00:00
</sub>