pub struct Context;Expand description
A context for reading and writing values.
This is created by calling Context::new, and is used to read values from the input and write values to the output.
Implementations§
Source§impl Context
impl Context
Sourcepub fn write_null(&mut self) -> Result<(), Error>
pub fn write_null(&mut self) -> Result<(), Error>
Write a null value.
Sourcepub fn write_interned_utf8_str(
&mut self,
id: InternedStringId,
) -> Result<(), Error>
pub fn write_interned_utf8_str( &mut self, id: InternedStringId, ) -> Result<(), Error>
Write an interned UTF-8 string value.
Sourcepub fn write_object<F: FnOnce(&mut Self) -> Result<(), Error>>(
&mut self,
f: F,
len: usize,
) -> Result<(), Error>
pub fn write_object<F: FnOnce(&mut Self) -> Result<(), Error>>( &mut self, f: F, len: usize, ) -> Result<(), Error>
Write an object. You must provide the exact number of key-value pairs you will write.
Sourcepub fn write_array<F: FnOnce(&mut Self) -> Result<(), Error>>(
&mut self,
f: F,
len: usize,
) -> Result<(), Error>
pub fn write_array<F: FnOnce(&mut Self) -> Result<(), Error>>( &mut self, f: F, len: usize, ) -> Result<(), Error>
Write an array. You must provide the exact number of values you will write.
Sourcepub fn finalize_output_and_return(self) -> Result<Value, Error>
pub fn finalize_output_and_return(self) -> Result<Value, Error>
Finalize the output and return the serialized value as a serde_json::Value.
This is only available in non-Wasm targets, and therefore only recommended for use in tests.
Source§impl Context
impl Context
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new context.
This is only intended to be invoked when compiled to a Wasm target.
§Panics
This will panic if called from a non-Wasm environment.
Sourcepub fn new_with_input(input: Value) -> Self
pub fn new_with_input(input: Value) -> Self
Create a new context from a JSON value, which will be the top-level value of the input.
This is only available when compiled to a non-Wasm target, for usage in unit tests.
Sourcepub fn input_get(&self) -> Result<Value, ContextError>
pub fn input_get(&self) -> Result<Value, ContextError>
Get the top-level value of the input.
Sourcepub fn intern_utf8_str(&self, s: &str) -> InternedStringId
pub fn intern_utf8_str(&self, s: &str) -> InternedStringId
Intern a string. This can lead to performance gains if you are using the same string multiple times, as it saves unnecessary string copies. For example, if you are reading the same property from multiple objects, or serializing the same key on an object, you can intern the string once and reuse it.