Skip to main content

unparse

Function unparse 

Source
pub fn unparse<V: AsRef<Value>>(
    _source: &Source,
    value: V,
) -> Result<String, Box<dyn Error + Send + Sync + 'static>>
Expand description

Serialize a Value tree into pretty-printed JSON (2-space indent).

Accepts a Value, &Value, LocatedValue, or &LocatedValue. source is accepted for signature symmetry with Parse::parse but is unused here.

use tanzim_parse::json::unparse;
use tanzim_source::SourceBuilder;
use tanzim_value::{Map, LocatedValue, Location, Value};

let source = SourceBuilder::new().with_source("file").build().unwrap();
let mut map = Map::new();
map.insert("port".into(), LocatedValue::new(
    Value::Int(8080),
    Location::at("file", "", None, None, None),
));
let text = unparse(&source, Value::Map(map)).unwrap();
assert_eq!(text, "{\n  \"port\": 8080\n}");