pub struct JsonObject<'a, 'b, W: BlockingWrite, F: JsonFormatter, FF: FloatFormat> { /* private fields */ }Expand description
A JsonObject is the API for writing a JSON object, i.e. a sequence of key/value pairs. The
closing } is written when the JsonObject instance goes out of scope, or when its end()
function is called.
For nested objects or arrays, the function calls return new JsonObject or JsonArray instances, respectively. Rust’s type system ensures that applications can only interact with the innermost such instance, and call outer instances only when all nested instances have gone out of scope.
A typical use of the library is to create a JsonWriter and then wrap it in a top-level JsonObject instance.
Implementations§
Source§impl<'a, 'b, W: BlockingWrite, F: JsonFormatter, FF: FloatFormat> JsonObject<'a, 'b, W, F, FF>
impl<'a, 'b, W: BlockingWrite, F: JsonFormatter, FF: FloatFormat> JsonObject<'a, 'b, W, F, FF>
Sourcepub fn new(writer: &'a mut JsonWriter<'b, W, F, FF>) -> Result<Self, W::Error>
pub fn new(writer: &'a mut JsonWriter<'b, W, F, FF>) -> Result<Self, W::Error>
Create a new JsonObject instance. Application code can do this explicitly only initially as a starting point for writing JSON. Nested objects are created by the library.
Sourcepub fn write_string_value(
&mut self,
key: &str,
value: &str,
) -> Result<(), W::Error>
pub fn write_string_value( &mut self, key: &str, value: &str, ) -> Result<(), W::Error>
Write a key/value pair with element type ‘string’, escaping the provided string value.
Sourcepub fn write_bool_value(
&mut self,
key: &str,
value: bool,
) -> Result<(), W::Error>
pub fn write_bool_value( &mut self, key: &str, value: bool, ) -> Result<(), W::Error>
Write a key/value pair with element type ‘bool’
Sourcepub fn write_null_value(&mut self, key: &str) -> Result<(), W::Error>
pub fn write_null_value(&mut self, key: &str) -> Result<(), W::Error>
Write a key with a null literal as its value
Sourcepub fn write_f64_value(&mut self, key: &str, value: f64) -> Result<(), W::Error>
pub fn write_f64_value(&mut self, key: &str, value: f64) -> Result<(), W::Error>
Write a key/value pair with an f64 value. If the value is not finite (i.e. infinite or NaN), a null literal is written instead. Different behavior (e.g. leaving out the whole key/value pair for non-finite numbers, representing them in some other way etc.) is the responsibility of application code.
Sourcepub fn write_f32_value(&mut self, key: &str, value: f32) -> Result<(), W::Error>
pub fn write_f32_value(&mut self, key: &str, value: f32) -> Result<(), W::Error>
Write a key/value pair with an f32 value. If the value is not finite (i.e. infinite or NaN), a null literal is written instead. Different behavior (e.g. leaving out the whole key/value pair for non-finite numbers, representing them in some other way etc.) is the responsibility of application code.
Sourcepub fn start_object<'c, 'x>(
&'x mut self,
key: &str,
) -> Result<JsonObject<'c, 'b, W, F, FF>, W::Error>where
'a: 'c,
'x: 'c,
pub fn start_object<'c, 'x>(
&'x mut self,
key: &str,
) -> Result<JsonObject<'c, 'b, W, F, FF>, W::Error>where
'a: 'c,
'x: 'c,
Start a nested object under a given key. This function returns a new JsonObject instance
for writing elements to the nested object. When the returned JsonObject goes out of scope
(per syntactic scope or an explicit call to end()), the nested object is closed, and
application code can continue adding elements to the owning self object.
Sourcepub fn start_array<'c, 'x>(
&'x mut self,
key: &str,
) -> Result<JsonArray<'c, 'b, W, F, FF>, W::Error>where
'a: 'c,
'x: 'c,
pub fn start_array<'c, 'x>(
&'x mut self,
key: &str,
) -> Result<JsonArray<'c, 'b, W, F, FF>, W::Error>where
'a: 'c,
'x: 'c,
Start a nested array under a given key. This function returns a new JsonArray instance
for writing elements to the nested object. When the returned JsonArray goes out of scope
(per syntactic scope or an explicit call to end()), the nested array is closed, and
application code can continue adding elements to the owning self object.