pub struct SimpleJsonWriter<J: JsonWriter> { /* private fields */ }
Available on crate feature experimental only.
Expand description

JSON writer variant which is easier to use than JsonWriter

This JSON writer variant ensures correct usage at compile-time making it easier and less error-prone to use than JsonWriter, which validates correct usage at runtime and panics on incorrect usage. However, this comes at the cost of SimpleJsonWriter being less flexible to use, and it not offerring all features of JsonWriter.

When an error is returned by one of the methods of the writer, the error should be propagated (for example by using Rust’s ? operator), processing should be aborted and the writer should not be used any further.

§Examples

// In this example JSON bytes are stored in a Vec;
// normally they would be written to a file or network connection
let mut writer = Vec::<u8>::new();
let json_writer = SimpleJsonWriter::new(&mut writer);
json_writer.write_object(|object_writer| {
    object_writer.write_number_member("a", 1)?;
    object_writer.write_bool_member("b", true)?;
    Ok(())
})?;

let json = String::from_utf8(writer)?;
assert_eq!(json, r#"{"a":1,"b":true}"#);

Implementations§

source§

impl<J: JsonWriter> SimpleJsonWriter<J>

source

pub fn from_json_writer(json_writer: J) -> Self

Creates a new SimpleJsonWriter from the given JsonWriter

The JsonWriter acts as delegate which performs the actual JSON writing. It should not have written any JSON data yet, otherwise the behavior of the created SimpleJsonWriter is unspecified and it may panic.

§Examples
let mut writer = Vec::<u8>::new();
let json_writer = SimpleJsonWriter::from_json_writer(
    JsonStreamWriter::new_custom(
        &mut writer,
        WriterSettings {
            pretty_print: true,
            // For all other settings use the default
            ..Default::default()
        },
    )
);
source§

impl<W: Write> SimpleJsonWriter<JsonStreamWriter<W>>

source

pub fn new(writer: W) -> Self

Creates a new JSON writer

The JSON data will be written UTF-8 encoded to the writer. Internally this creates a JsonStreamWriter which acts as delegate; see its documentation for more information about the writing behavior.

Trait Implementations§

source§

impl<J: Debug + JsonWriter> Debug for SimpleJsonWriter<J>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<J: JsonWriter> ValueWriter<J> for SimpleJsonWriter<J>

source§

fn write_null(self) -> Result<(), Error>

Writes a JSON null value
source§

fn write_bool(self, value: bool) -> Result<(), Error>

Writes a JSON boolean value
source§

fn write_string(self, value: &str) -> Result<(), Error>

Writes a JSON string value Read more
source§

fn write_string_with_writer( self, f: impl FnOnce(StringValueWriter<'_>) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>

Writes a JSON string value using a Write Read more
source§

fn write_number<N: FiniteNumber>(self, value: N) -> Result<(), Error>

Writes a JSON number value
source§

fn write_fp_number<N: FloatingPointNumber>( self, value: N ) -> Result<(), JsonNumberError>

Writes a floating point JSON number value Read more
source§

fn write_number_string(self, value: &str) -> Result<(), JsonNumberError>

Writes the string representation of a JSON number value Read more
source§

fn write_serialize<S: Serialize>(self, value: &S) -> Result<(), SerializerError>

Available on crate feature serde only.
Serializes a Serde Serialize as next value Read more
source§

fn write_array( self, f: impl FnOnce(&mut ArrayWriter<'_, J>) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>

Writes a JSON array Read more
source§

fn write_object( self, f: impl FnOnce(&mut ObjectWriter<'_, J>) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>

Writes a JSON object Read more

Auto Trait Implementations§

§

impl<J> Freeze for SimpleJsonWriter<J>
where J: Freeze,

§

impl<J> RefUnwindSafe for SimpleJsonWriter<J>
where J: RefUnwindSafe,

§

impl<J> Send for SimpleJsonWriter<J>
where J: Send,

§

impl<J> Sync for SimpleJsonWriter<J>
where J: Sync,

§

impl<J> Unpin for SimpleJsonWriter<J>
where J: Unpin,

§

impl<J> UnwindSafe for SimpleJsonWriter<J>
where J: 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>,

§

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>,

§

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.