Mirror of https://github.com/dtolnay/serde-yaml. Part of the kade rust2024 distribution.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Balazs Horvath f1cd3aa9e5
Some checks failed
ci.yml / Point siblings at the kade den; allow dangerous_implicit_autorefs (push) Failing after 0s
Point siblings at the kade den; allow dangerous_implicit_autorefs
Converts the five dependencies (indexmap, itoa, ryu, serde, unsafe-libyaml) to
bare git.sly.so refs with no version pins, per the rust2024 distribution
standard.

Edition stays at upstream's 2021. A 2024 conversion is not viable here: it
breaks the libyaml bindings in 22 places, and the deny-by-default
dangerous_implicit_autorefs lint would require rewriting raw-pointer FFI in a
crate upstream has marked deprecated. One documented lint allowance is a much
smaller deviation than a memory-safety rewrite, and it keeps this mirror
tracking upstream's source.
2026-10-04 11:39:39 +02:00
.github Test docs.rs documentation build in CI 2023-09-24 10:53:43 -07:00
fuzz Update fuzz crate gitignore to ignore coverage dir 2023-03-25 21:35:20 -07:00
src Point siblings at the kade den; allow dangerous_implicit_autorefs 2026-10-04 11:39:39 +02:00
tests Ignore dead code lint in tests 2024-03-23 20:04:05 -07:00
.gitignore Add redirect from top-level index.html 2016-02-24 23:55:19 -08:00
Cargo.toml Point siblings at the kade den; allow dangerous_implicit_autorefs 2026-10-04 11:39:39 +02:00
LICENSE-APACHE Sync license text with rust-lang repos 2022-12-30 12:00:51 -08:00
LICENSE-MIT Remove pseudo copyright lines and boilerplate 2019-04-30 02:13:04 -07:00
README.md Add unmaintained note 2024-03-24 17:40:08 -07:00

Serde YAML

github crates.io docs.rs build status

Rust library for using the Serde serialization framework with data in YAML file format. (This project is no longer maintained.)

Dependency

[dependencies]
serde = "1.0"
serde_yaml = "0.9"

Release notes are available under GitHub releases.

Using Serde YAML

API documentation is available in rustdoc form but the general idea is:

use std::collections::BTreeMap;

fn main() -> Result<(), serde_yaml::Error> {
    // You have some type.
    let mut map = BTreeMap::new();
    map.insert("x".to_string(), 1.0);
    map.insert("y".to_string(), 2.0);

    // Serialize it to a YAML string.
    let yaml = serde_yaml::to_string(&map)?;
    assert_eq!(yaml, "x: 1.0\ny: 2.0\n");

    // Deserialize it back to a Rust type.
    let deserialized_map: BTreeMap<String, f64> = serde_yaml::from_str(&yaml)?;
    assert_eq!(map, deserialized_map);
    Ok(())
}

It can also be used with Serde's derive macros to handle structs and enums defined in your program.

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"

Structs serialize in the obvious way:

use serde::{Serialize, Deserialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Point {
    x: f64,
    y: f64,
}

fn main() -> Result<(), serde_yaml::Error> {
    let point = Point { x: 1.0, y: 2.0 };

    let yaml = serde_yaml::to_string(&point)?;
    assert_eq!(yaml, "x: 1.0\ny: 2.0\n");

    let deserialized_point: Point = serde_yaml::from_str(&yaml)?;
    assert_eq!(point, deserialized_point);
    Ok(())
}

Enums serialize using YAML's !tag syntax to identify the variant name.

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(())
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.