Struct struson::writer::simple::SimpleJsonWriter
source · pub struct SimpleJsonWriter<J: JsonWriter> { /* private fields */ }
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>
impl<J: JsonWriter> SimpleJsonWriter<J>
sourcepub fn from_json_writer(json_writer: J) -> Self
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>>
impl<W: Write> SimpleJsonWriter<JsonStreamWriter<W>>
sourcepub fn new(writer: W) -> Self
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>
impl<J: Debug + JsonWriter> Debug for SimpleJsonWriter<J>
source§impl<J: JsonWriter> ValueWriter<J> for SimpleJsonWriter<J>
impl<J: JsonWriter> ValueWriter<J> for SimpleJsonWriter<J>
source§fn write_string_with_writer(
self,
f: impl FnOnce(StringValueWriter<'_>) -> Result<(), Box<dyn Error>>
) -> Result<(), Box<dyn Error>>
fn write_string_with_writer( self, f: impl FnOnce(StringValueWriter<'_>) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
source§fn write_number<N: FiniteNumber>(self, value: N) -> Result<(), Error>
fn write_number<N: FiniteNumber>(self, value: N) -> Result<(), Error>
source§fn write_fp_number<N: FloatingPointNumber>(
self,
value: N
) -> Result<(), JsonNumberError>
fn write_fp_number<N: FloatingPointNumber>( self, value: N ) -> Result<(), JsonNumberError>
source§fn write_number_string(self, value: &str) -> Result<(), JsonNumberError>
fn write_number_string(self, value: &str) -> Result<(), JsonNumberError>
source§fn write_serialize<S: Serialize>(self, value: &S) -> Result<(), SerializerError>
fn write_serialize<S: Serialize>(self, value: &S) -> Result<(), SerializerError>
serde
only.