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 form —
object(f),array(f),value(v)plus keyed viaKeyedEmitter. - Streaming form —
open_object(),close_object(),open_array(),close_array().
Implementations§
Source§impl<B: JsonBackend> JsonEmitter<B>
impl<B: JsonBackend> JsonEmitter<B>
Sourcepub fn array<F>(&mut self, f: F) -> Result<()>
pub fn array<F>(&mut self, f: F) -> 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.
Sourcepub fn close_array(&mut self) -> Result<()>
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.
Sourcepub fn close_object(&mut self) -> Result<()>
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.
Sourcepub fn finish(self) -> Result<B::Output>
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.
Sourcepub fn key<'a>(&'a mut self, name: &'a str) -> KeyedEmitter<'a, B>
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().
Sourcepub fn object<F>(&mut self, f: F) -> Result<()>
pub fn object<F>(&mut self, f: F) -> 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.
Sourcepub fn open_array(&mut self) -> Result<()>
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.
Sourcepub fn open_object(&mut self) -> Result<()>
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.