Skip to main content

from_reader

Function from_reader 

Source
pub fn from_reader<'a, R: Read + 'a, T: DeserializeOwned>(
    reader: R,
) -> Result<T, Error>
Expand description

Deserialize a single YAML document from any std::io::Read.

Reader-based entry points accept BOM-marked UTF-8, UTF-16LE, and UTF-16BE. If no recognized BOM is present, the input bytes are treated as UTF-8.

This method parses as it reads, without loading the entire input into memory first. Hence, budget limits protect against large (potentially malicious) input.

Example

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use serde_json::Value;

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

let yaml = "x: 3\ny: 4\n";
let reader = std::io::Cursor::new(yaml.as_bytes());
let p: Point = serde_saphyr::from_reader(reader).unwrap();
assert_eq!(p, Point { x: 3, y: 4 });

// It also works for dynamic values like serde_json::Value
let mut big = String::new();
let mut i = 0usize;
while big.len() < 64 * 1024 { big.push_str(&format!("k{0}: v{0}\n", i)); i += 1; }
let reader = std::io::Cursor::new(big.as_bytes().to_owned());
let _value: Value = serde_saphyr::from_reader(reader).unwrap();