JsonObjectFormatter

Struct JsonObjectFormatter 

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

Source

pub fn member<N, V>(&mut self, name: N, value: V) -> Result
where 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)
    })
});
Source

pub fn members<I, N, V>(&mut self, members: I) -> Result
where I: IntoIterator<Item = (N, V)>, N: Display, V: DisplayJson,

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> 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.