-
Notifications
You must be signed in to change notification settings - Fork 639
Open
Description
As far as I can tell, using a custom formatter requires interfacing with Serializer, which means any crate doing this needs the direct serde_core dependency:
use serde_core::Serialize;
use serde_json::{Serializer, json, ser::PrettyFormatter};
fn main() {
let j = json! {{"a": [1, 2, 3], "b": {}}};
let p = PrettyFormatter::with_indent(b" ");
let mut v = Vec::new();
let mut s = Serializer::with_formatter(&mut v, p);
j.serialize(&mut s).unwrap();
println!("{}", str::from_utf8(&v).unwrap());
}Would it be possible to add convenience functions like to_{string,vec,writer}_formatter that make this easier?
pub fn to_string_formatter<T, F>(value: &T, formatter: F) -> Result<String>
where
T: ?Sized + Serialize,
F: Formatter;fn main() {
let j = json! {{"a": [1, 2, 3], "b": {}}};
let p = PrettyFormatter::with_indent(b" ");
println!("{}", serde::to_string_formatter(&j, p).unwrap());
}Any -> String versions should emit an error if the indentation is not valid UTF-8 (#750).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels