pub struct ArrayWriter<'w> { /* private fields */ }Expand description
Adds the items of an array being written; see ValueWriter::array.
Implementations§
Source§impl ArrayWriter<'_>
impl ArrayWriter<'_>
Sourcepub fn item<T: ToJson + ?Sized>(&mut self, value: &T)
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]"#);Sourcepub fn item_with(&mut self, value: impl FnOnce(ValueWriter<'_>) -> Written)
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]");Sourcepub fn items<I>(&mut self, items: I)
pub fn items<I>(&mut self, items: I)
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> 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