JsonArrayFormatter

Struct JsonArrayFormatter 

Source
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<'_, '_, '_>

Source

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])
    })
});
Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.