pub struct FieldWriter<'a> { /* private fields */ }Expand description
A writer passed to closures registered with
add_multiple_dynamic_fields.
Call write_field to add key-value pairs directly to the JSON
output. Keys are &str (no allocation required for static strings), and values accept any
type that implements serde::Serialize.
Implementations§
Source§impl<'a> FieldWriter<'a>
impl<'a> FieldWriter<'a>
Sourcepub fn write_field(&mut self, key: &str, value: impl Serialize) -> Result<()>
pub fn write_field(&mut self, key: &str, value: impl Serialize) -> Result<()>
Writes a single key-value pair into the JSON output.
The key is serialized as a quoted, escaped JSON string. The value can be any type
implementing serde::Serialize: strings, numbers, booleans, Option<T>, structs
with #[derive(Serialize)], etc.
Returns Err if serialization fails. On error the writer state is rolled back so the
output remains valid JSON. Errors from this method are rare and indicate a bug in the
Serialize implementation of the value type.
§Errors
This function errors if serialization of the provided value fails. This means that either
the implementation of Serialize decides to fail, or if the value contains a map with
non-string keys.