sval_json 0.1.0

Json support for the sval serialization framework
Documentation
use crate::std::string::String;
use crate::std::{
    fmt,
    io::Write,
};

/**
Write a [`sval::Value`] to a string.
*/
pub fn to_string(v: impl sval::Value) -> Result<String, sval::Error> {
    let mut out = String::new();

    crate::to_fmt(&mut out, v)?;

    Ok(out)
}

/**
Write a [`sval::Value`] to a writer.
*/
pub fn to_writer(writer: impl Write, v: impl sval::Value) -> Result<(), sval::Error> {
    crate::to_fmt(Writer(writer), v)
}

struct Writer<W>(W);

impl<W> fmt::Write for Writer<W>
where
    W: Write,
{
    fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
        self.0.write(s.as_bytes()).map_err(|_| fmt::Error)?;

        Ok(())
    }
}