Struct yaml2json_rs::Yaml2Json
source · [−]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
sourceimpl Yaml2Json
impl Yaml2Json
sourcepub fn new(style: Style) -> Self
pub fn new(style: Style) -> Self
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);
sourcepub fn document_to_string(
&self,
document: &str
) -> Result<String, Yaml2JsonError>
pub fn document_to_string(
&self,
document: &str
) -> Result<String, Yaml2JsonError>
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"}"#);
sourcepub fn document_to_writer<W: Write>(
&self,
document: &str,
w: &mut W
) -> Result<(), Yaml2JsonError>
pub fn document_to_writer<W: Write>(
&self,
document: &str,
w: &mut W
) -> Result<(), Yaml2JsonError>
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
impl RefUnwindSafe for Yaml2Json
impl Send for Yaml2Json
impl Sync for Yaml2Json
impl Unpin for Yaml2Json
impl UnwindSafe for Yaml2Json
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more