pub struct Yaml2Json { /* private fields */ }
Expand description

Yaml2Json can convert individual YAML documents into JSON. Each instance can be configured to have different styles of output.

The JSON output can be returned as a string:

use yaml2json_rs::{Yaml2Json, Style};

let y2j = Yaml2Json::new(Style::COMPACT);
let input = "hello: world";
let output = y2j.document_to_string(input).unwrap();

assert_eq!(output, r#"{"hello":"world"}"#);

Or, the JSON output can be sent to a writer:

use yaml2json_rs::{Yaml2Json, Style};
use std::io;

let y2j = Yaml2Json::new(Style::COMPACT);
let input = "hello: world";
let mut stdout = io::stdout();

y2j.document_to_writer(input, &mut stdout).unwrap();

// {"hello":"world"}

Implementations

new() creates a new Yaml2Json. It expects you to provide an output Style.

use yaml2json_rs::{Yaml2Json, Style};

let y2j_pretty = Yaml2Json::new(Style::PRETTY);
let y2j_compact = Yaml2Json::new(Style::COMPACT);

document_to_string() takes a YAML document &str and converts it to a JSON String.

use yaml2json_rs::{Yaml2Json, Style};

let y2j = Yaml2Json::new(Style::COMPACT);
let input = "hello: world";
let output = y2j.document_to_string(input).unwrap();

assert_eq!(output, r#"{"hello":"world"}"#);

document_to_writer() takes a YAML document string, converts it to JSON and sends the output to the provided writer.

use yaml2json_rs::{Yaml2Json, Style};
use std::io;

let y2j = Yaml2Json::new(Style::COMPACT);
let input = "hello: world";
let mut stdout = io::stdout();

y2j.document_to_writer(input, &mut stdout).unwrap();

// {"hello":"world"}

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.