This package is a fork of serde-yaml, designed to provide (mostly) panic-free operation. Specifically, it should not panic when encountering malformed YAML syntax. This makes the library suitable for safely parsing user-supplied YAML content. The library is hardened against the Billion Laughs attack, infinite recursion from merge keys and anchors (the limits are configurable) and duplicate keys.
Our fork supports merge keys, which reduce redundancy and verbosity by specifying shared key-value pairs once and then reusing them across multiple mappings. It additionally supports nested enums for Rust-aligned parsing of polymorphic data, as well as the !!binary tag.
These extensions come at the cost of some API restrictions: write access to indices and mappings has been removed. Read access remains possible, with Value::Null returned on invalid access. Also, duplicate keys are not longer permitted in YAML, returning proper error message instead.
We do not encourage using this crate beyond serialization with serde. If your use-case requires additional functionality, there are better-suited crates available, such as yaml-rust2 and the newer, more experimental saphyr, both capable of handling valid YAML that is not directly representable with Rust structures.
Since the API has changed to a more restrictive version, the major version number has been incremented.
If a panic does occur under some short and clear input, please report it as a bug.
Usage Example
Here's an example demonstrating how to parse YAML into a Rust structure using serde_yaml_bw with proper error
handling:
use Deserialize;
use Deserializer;
// Define the structure representing your YAML data.
Here is example with merge keys (inherited properties):
use Deserialize;
/// Configuration to parse into. Does not include "defaults"
Merge keys are standard in YAML 1.1. Although YAML 1.2 no longer includes merge keys in its specification, it doesn't explicitly disallow them either, and many parsers implement this feature.
Nested enums
Externally tagged enums naturally nest in YAML as maps keyed by the variant name. They enable the use of strict types (Rust enums with associated data) instead of falling back to generic maps.
/// Restrict space, speed, force, whatever - with associated data.
/// Multiple constraints can be taken into consideration
Binary scalars
YAML values tagged with !!binary are automatically base64-decoded when deserializing into Vec<u8>. To serialize in this form, annotate the field with #[serde(with = "serde_bytes")] from the serde_bytes crate.
use Deserialize;
Rc, Arc, Box and Cow
To serialize references (Rc, Arc), just add the "rc" feature to Serde. Box and Cow are supported out of the box.
Streaming
This library does not read the whole content of the Reader before even trying to parse. Hence it is possible to implement
streaming using the new StreamDeserializer.
use Deserialize;
use File;
DeserializerOptions
can be adjusted to control recursion or alias expansion limits. The formatting of emitted YAML can be configured using SerializerBuilder that is useful for a human-intended output.