ObjectWriter

Struct ObjectWriter 

Source
pub struct ObjectWriter<'a, S: JsonBuffer> { /* private fields */ }
Expand description

A serializer for a JSON object.

Implementations§

Source§

impl<'a, S: JsonBuffer> ObjectWriter<'a, S>

Source

pub fn field<K: Key>(&mut self, key: K, val: impl WriteToJson<S>)

Adds a field to this object.

§Examples
let mut obj = ser.object();
obj.field("kind", "cat");
obj.field("has_been_fed", false);
obj.field("meow_decibels", 45);
obj.end();
Source

pub fn complex_field<K, F, O>(&mut self, key: K, encode: F) -> O
where K: Key, F: FnOnce(SingleValueSerializer<'_, &mut S>) -> O,

Adds an arbitrary JSON object to this object.

§Arguments
  • key - the key for the field
  • encoder - A closure that encodes a single value into the field. It may return an arbitrary value that will be passed back to the caller.
§Examples
let mut obj = ser.object();
obj.complex_field("numbers", |mut ser| {
    let mut arr = ser.array();
    arr.add(1);
    arr.add(2);
    arr.add("three");
});
obj.end()
Source

pub fn object_field<K: Key>(&mut self, key: K) -> ObjectWriter<'_, S>

Adds a JSON object field to this object.

§Examples
let mut obj = ser.object();
obj.field("kitten", true);
obj.field("cuteness", 10.0);

let mut bed = obj.object_field("bed");
bed.field("cozy", true);
bed.field("wear_and_tear", 2.0);
bed.end();

obj.end();
Source

pub fn array_field<K: Key>(&mut self, key: K) -> ArrayWriter<'_, S>

Adds a JSON array field to this object.

§Examples
let mut arr = ser.array();

let mut obj = arr.add_object();
obj.field("kitten", true);
obj.field("cuteness", 10.0);

let mut toys = obj.array_field("toys");
toys.extend(&["mouse", "ball", "string", "box", "scratcher"]);
toys.end();

obj.end();

arr.end();
Source

pub fn end(self)

Trait Implementations§

Source§

impl<S: JsonBuffer> Drop for ObjectWriter<'_, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, S> Freeze for ObjectWriter<'a, S>

§

impl<'a, S> RefUnwindSafe for ObjectWriter<'a, S>
where S: RefUnwindSafe,

§

impl<'a, S> Send for ObjectWriter<'a, S>
where S: Send,

§

impl<'a, S> Sync for ObjectWriter<'a, S>
where S: Sync,

§

impl<'a, S> Unpin for ObjectWriter<'a, S>

§

impl<'a, S> !UnwindSafe for ObjectWriter<'a, S>

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.