Skip to main content

ArrayWriter

Struct ArrayWriter 

Source
pub struct ArrayWriter<'w> { /* private fields */ }
Expand description

Adds the items of an array being written; see ValueWriter::array.

Implementations§

Source§

impl ArrayWriter<'_>

Source

pub fn item<T: ToJson + ?Sized>(&mut self, value: &T)

Adds value as the next item.

§Examples
use jsonx_lib::{ToJson, ValueWriter, Written};

struct Pair(&'static str, i32);
impl ToJson for Pair {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.array(|a| {
            a.item(self.0);
            a.item(&self.1);
        })
    }
}
assert_eq!(jsonx_lib::to_string(&Pair("x", -1)), r#"["x",-1]"#);
Source

pub fn item_with(&mut self, value: impl FnOnce(ValueWriter<'_>) -> Written)

Adds the next item, written by value.

§Examples
use jsonx_lib::{ToJson, ValueWriter, Written};

struct Squares(u32);
impl ToJson for Squares {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.array(|a| {
            for n in 1..=self.0 {
                a.item_with(|w| w.u64(u64::from(n * n)));
            }
        })
    }
}
assert_eq!(jsonx_lib::to_string(&Squares(3)), "[1,4,9]");
Source

pub fn items<I>(&mut self, items: I)
where I: IntoIterator, I::Item: ToJson,

Adds every item of items, in order.

§Examples
use jsonx_lib::{ToJson, ValueWriter, Written};

struct Evens(u32);
impl ToJson for Evens {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.array(|a| a.items((0..self.0).map(|n| n * 2)))
    }
}
assert_eq!(jsonx_lib::to_string(&Evens(3)), "[0,2,4]");

Auto Trait Implementations§

§

impl<'w> !RefUnwindSafe for ArrayWriter<'w>

§

impl<'w> !Send for ArrayWriter<'w>

§

impl<'w> !Sync for ArrayWriter<'w>

§

impl<'w> !UnwindSafe for ArrayWriter<'w>

§

impl<'w> Forget for ArrayWriter<'w>

§

impl<'w> Freeze for ArrayWriter<'w>

§

impl<'w> Move for ArrayWriter<'w>

§

impl<'w> Unpin for ArrayWriter<'w>

§

impl<'w> UnsafeUnpin for ArrayWriter<'w>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.