Skip to main content

JsonEmitter

Struct JsonEmitter 

Source
pub struct JsonEmitter<B: JsonBackend> { /* private fields */ }
Expand description

Fluent JSON emitter generic over a JsonBackend.

Drives the backend through valid JSON shapes only — invalid sequences (e.g. key outside an object, two values for one key) return errors before reaching the backend.

Provides two complementary APIs sharing the same state machine:

  • Closure formobject(f), array(f), value(v) plus keyed via KeyedEmitter.
  • Streaming formopen_object(), close_object(), open_array(), close_array().

Implementations§

Source§

impl<B: JsonBackend> JsonEmitter<B>

Source

pub fn array<F>(&mut self, f: F) -> Result<()>
where F: FnOnce(&mut Self) -> Result<()>,

Write an unkeyed array using a closure.

Best-effort scope guard: if the closure returns Err, Self::close_array is still attempted so that the backend sees a matching end_array and the emitter state machine is left in a consistent shape. The closure’s error takes precedence over any error produced by the close.

§Errors

Returns Err if a value is not allowed here, or if the closure or backend errors.

Source

pub fn close_array(&mut self) -> Result<()>

Close the current array frame. Errors if the current frame is not an array.

§Errors

Returns Err if the current frame is not an array, or if the backend errors.

Source

pub fn close_object(&mut self) -> Result<()>

Close the current object frame. Errors if the current frame is not an object.

§Errors

Returns Err if the current frame is not an object, or if the backend errors.

Source

pub fn finish(self) -> Result<B::Output>

Finish emission and return the backend’s output.

§Errors

Returns Err if emission is incomplete, or if the backend errors during finish.

Source

pub fn key<'a>(&'a mut self, name: &'a str) -> KeyedEmitter<'a, B>

Begin a keyed slot. Returns a single-use KeyedEmitter that must be consumed by .object(), .array(), .value(), .open_object(), or .open_array().

Source

pub fn new(backend: B) -> Self

Create a new emitter wrapping backend.

Source

pub fn object<F>(&mut self, f: F) -> Result<()>
where F: FnOnce(&mut Self) -> Result<()>,

Write an unkeyed object using a closure.

Best-effort scope guard: if the closure returns Err, Self::close_object is still attempted so that the backend sees a matching end_object and the emitter state machine is left in a consistent shape. The closure’s error takes precedence over any error produced by the close.

§Errors

Returns Err if a value is not allowed here, or if the closure or backend errors.

Source

pub fn open_array(&mut self) -> Result<()>

Open an unkeyed array. Caller must later call JsonEmitter::close_array.

§Errors

Returns Err if a value is not allowed here, or if the backend errors.

Source

pub fn open_object(&mut self) -> Result<()>

Open an unkeyed object. Caller must later call JsonEmitter::close_object.

§Errors

Returns Err if a value is not allowed here, or if the backend errors.

Source

pub fn value<V: WriteVal>(&mut self, v: V) -> Result<()>

Write an unkeyed scalar value.

§Errors

Returns Err if a value is not allowed here, or if the backend errors.

Auto Trait Implementations§

§

impl<B> Freeze for JsonEmitter<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for JsonEmitter<B>
where B: RefUnwindSafe,

§

impl<B> Send for JsonEmitter<B>
where B: Send,

§

impl<B> Sync for JsonEmitter<B>
where B: Sync,

§

impl<B> Unpin for JsonEmitter<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for JsonEmitter<B>
where B: UnsafeUnpin,

§

impl<B> UnwindSafe for JsonEmitter<B>
where B: UnwindSafe,

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.