pub fn to_writer_with_options<W: Write, T: Serialize>(
out: &mut W,
value: &T,
options: SerializerOptions,
) -> Result<(), Error>
Expand description
Serialize a value to a writer using SerializerOptions
.
Use this to tweak indentation or provide a custom anchor name generator.
Example: 4-space indentation.
use serde::Serialize;
#[derive(Serialize)]
struct Foo { a: i32 }
let mut buf = String::new();
let opts = serde_saphyr::SerializerOptions { indent_step: 4, anchor_generator: None };
serde_saphyr::to_writer_with_options(&mut buf, &Foo { a: 7 }, opts).unwrap();
assert!(buf.contains("a: 7"));
Example: custom anchor names when using Rc/Arc wrappers.
use serde::Serialize;
use std::rc::Rc;
#[derive(Serialize)]
struct Node { name: String }
let shared = Rc::new(Node { name: "n".into() });
let mut buf = String::new();
let opts = serde_saphyr::SerializerOptions { indent_step: 2, anchor_generator: Some(|id| format!("id{id}")) };
serde_saphyr::to_writer_with_options(&mut buf, &serde_saphyr::RcAnchor(shared), opts).unwrap();
assert!(buf.contains("&id1") || buf.contains("&id0"));