pub struct JsonObjectFormatter<'a, 'b, 'c> { /* private fields */ }Expand description
A formatter for JSON objects.
This struct is created by the JsonFormatter::object() method and provides
methods for adding name-value pairs (members) to a JSON object with proper
formatting, spacing, and indentation.
§Examples
use nojson::json;
let output = json(|f| {
f.object(|f| {
f.member("name", "Alice")?;
f.member("age", 30)
})
});
assert_eq!(output.to_string(), r#"{"name":"Alice","age":30}"#);
// With pretty-printing
let pretty = json(|f| {
f.set_indent_size(2);
f.set_spacing(true);
f.object(|f| {
f.member("name", "Alice")?;
f.member("age", 30)
})
});Implementations§
Source§impl JsonObjectFormatter<'_, '_, '_>
impl JsonObjectFormatter<'_, '_, '_>
Sourcepub fn member<N, V>(&mut self, name: N, value: V) -> Resultwhere
N: Display,
V: DisplayJson,
pub fn member<N, V>(&mut self, name: N, value: V) -> Resultwhere
N: Display,
V: DisplayJson,
Adds a single name-value pair (member) to the JSON object.
This method handles adding the appropriate comma separators between members and applies the current formatting rules like indentation and spacing.
§Examples
use nojson::json;
let output = json(|f| {
f.object(|f| {
f.member("name", "Alice")?;
f.member("age", 30)?;
f.member("active", true)
})
});Sourcepub fn members<I, N, V>(&mut self, members: I) -> Result
pub fn members<I, N, V>(&mut self, members: I) -> Result
Adds multiple name-value pairs (members) to the JSON object from an iterator.
This is a convenience method that iterates over the provided collection and
adds each item as a member using the JsonObjectFormatter::member() method.
§Examples
use nojson::json;
use std::collections::BTreeMap;
let mut map = BTreeMap::new();
map.insert("name", "Alice");
map.insert("age", "30");
let output = json(|f| {
f.object(|f| f.members(&map))
});Auto Trait Implementations§
impl<'a, 'b, 'c> Freeze for JsonObjectFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> !RefUnwindSafe for JsonObjectFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> !Send for JsonObjectFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> !Sync for JsonObjectFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> Unpin for JsonObjectFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> !UnwindSafe for JsonObjectFormatter<'a, 'b, 'c>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more