Yaml2Json

Struct 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§

Source§

impl Yaml2Json

Source

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);
Source

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"}"#);
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.