pub struct JsonArrayFormatter<'a, 'b, 'c> { /* private fields */ }Expand description
A formatter for JSON arrays.
This struct is created by the JsonFormatter::array() method and provides
methods for adding elements to a JSON array with proper formatting, spacing,
and indentation.
§Examples
use nojson::json;
let output = json(|f| {
f.array(|f| {
f.element(1)?;
f.element("test")?;
f.element(true)
})
});
assert_eq!(output.to_string(), "[1,\"test\",true]");
// With pretty-printing
let pretty = json(|f| {
f.set_indent_size(2);
f.set_spacing(true);
f.array(|f| {
f.elements([1, 2, 3])
})
});Implementations§
Source§impl JsonArrayFormatter<'_, '_, '_>
impl JsonArrayFormatter<'_, '_, '_>
Sourcepub fn element<T: DisplayJson>(&mut self, element: T) -> Result
pub fn element<T: DisplayJson>(&mut self, element: T) -> Result
Adds a single element to the JSON array.
This method handles adding the appropriate comma separators between elements and applies the current formatting rules like indentation and spacing.
§Examples
use nojson::json;
let output = json(|f| {
f.array(|f| {
f.element(1)?;
f.element("text")?;
f.element([9.87])
})
});Sourcepub fn elements<I>(&mut self, elements: I) -> Result
pub fn elements<I>(&mut self, elements: I) -> Result
Adds multiple elements to the JSON array from an iterator.
This is a convenience method that iterates over the provided collection and
adds each item as an element using the JsonArrayFormatter::element() method.
§Examples
use nojson::json;
let values = vec![1, 2, 3];
let output = json(|f| {
f.array(|f| f.elements(&values))
});
assert_eq!(output.to_string(), "[1,2,3]");
// Works with any iterable collection
let output = json(|f| {
f.array(|f| f.elements([true, false, true]))
});Auto Trait Implementations§
impl<'a, 'b, 'c> Freeze for JsonArrayFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> !RefUnwindSafe for JsonArrayFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> !Send for JsonArrayFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> !Sync for JsonArrayFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> Unpin for JsonArrayFormatter<'a, 'b, 'c>
impl<'a, 'b, 'c> !UnwindSafe for JsonArrayFormatter<'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