Skip to main content

sval_json/
to_string.rs

1use crate::{Error, JsonStr};
2
3use alloc::{boxed::Box, string::String};
4
5/**
6Stream a value as JSON into a string.
7
8This method will fail if the value contains complex values as keys.
9*/
10pub fn stream_to_string(v: impl sval::Value) -> Result<String, Error> {
11    let mut out = String::new();
12    crate::stream_to_fmt_write(&mut out, v)?;
13
14    Ok(out)
15}
16
17/**
18Stream a value as JSON into a `JsonStr`.
19
20This method will fail if the value contains complex values as keys.
21*/
22pub fn stream_to_json_str(v: impl sval::Value) -> Result<Box<JsonStr>, Error> {
23    Ok(JsonStr::boxed(stream_to_string(v)?))
24}