serde_yaml

Strongly typed YAML library for Rust

Latest version: 0.9.34-deprecated registry icon
Maintenance score
0
Safety score
0
Popularity score
85
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
0.9.34-deprecated 0 0 0 0 0
0.9.34+deprecated 0 0 0 0 0
0.9.33 0 0 0 0 0
0.9.32 0 0 0 0 0
0.9.31 0 0 0 0 0
0.9.30 0 0 0 0 0
0.9.29 0 0 0 0 0
0.9.28 0 0 0 0 0
0.9.27 0 0 0 0 0
0.9.26 0 0 0 0 0
0.9.25 0 0 0 0 0
0.9.24 0 0 0 0 0
0.9.23 0 0 0 0 0
0.9.22 0 0 0 0 0
0.9.21 0 0 0 0 0
0.9.20 0 0 0 0 0
0.9.19 0 0 0 0 0
0.9.18 0 0 0 0 0
0.9.17 0 0 0 0 0
0.9.16 0 0 0 0 0
0.9.15 0 0 0 0 0
0.9.14 0 0 0 0 0
0.9.13 0 0 0 0 0
0.9.12 0 0 0 0 0
0.9.11 0 0 0 0 0
0.9.10 0 0 0 0 0
0.9.9 0 0 0 0 0
0.9.8 0 0 0 0 0
0.9.7 0 0 0 0 0
0.9.6 0 0 0 0 0
0.9.5 0 0 0 0 0
0.9.4 0 0 0 0 0
0.9.3 0 0 0 0 0
0.9.2 0 0 0 0 0
0.9.1 0 0 0 0 0
0.9.0 0 0 0 0 0
0.8.26 0 0 0 0 0
0.8.25 0 0 0 0 0
0.8.24 0 0 0 0 0
0.8.23 0 0 0 0 0
0.8.22 0 0 0 0 0
0.8.21 0 0 0 0 0
0.8.20 0 0 0 0 0
0.8.19 0 0 0 0 0
0.8.18 0 0 0 0 0
0.8.17 0 0 0 0 0
0.8.16 0 0 0 0 0
0.8.15 0 0 0 0 0
0.8.14 0 0 0 0 0
0.8.13 0 0 0 0 0
0.8.12 0 0 0 0 0
0.8.11 0 0 0 0 0
0.8.10 0 0 0 0 0
0.8.9 0 0 0 0 0
0.8.8 0 0 0 0 0
0.8.7 0 0 0 0 0
0.8.6 0 0 0 0 0
0.8.5 0 0 0 0 0
0.8.4 0 0 0 0 0
0.8.3 0 0 1 0 0
0.8.2 0 0 1 0 0
0.8.1 0 0 1 0 0
0.8.0 0 0 1 0 0
0.7.5 0 0 1 0 0
0.7.4 0 0 1 0 0
0.7.3 0 0 1 0 0
0.7.2 0 0 1 0 0
0.7.1 0 0 1 0 0
0.7.0 0 0 1 0 0
0.6.2 0 0 1 0 0
0.6.1 0 0 1 0 0
0.6.0 0 0 1 0 0
0.5.1 0 0 0 0 0
0.5.0 0 0 0 0 0
0.4.1 0 0 0 0 0
0.4.0 0 0 0 0 0
0.3.0 0 0 0 0 0
0.2.5 0 0 0 0 0
0.2.4 0 0 0 0 0
0.2.3 0 0 0 0 0
0.2.2 0 0 0 0 0
0.2.1 0 0 0 0 0
0.2.0 0 0 0 0 0
0.1.0 0 0 0 0 0

Stability
Latest release:

0.9.34-deprecated - This version may not be safe as it has not been updated for a long time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

Apache-2.0   -   Apache License 2.0

Not a wildcard

Not proprietary

OSI Compliant


MIT   -   MIT License

Not a wildcard

Not proprietary

OSI Compliant



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.