Struct rusty_dumb_tools::json::DumbJsonProcessor
source · pub struct DumbJsonProcessor<'a> { /* private fields */ }Expand description
A simple JSON processor / stream parser, that processes input JSON (possibly streamed piece by piece). As soon as JSON entries are recognized, the configured callback is called for those recognized JSON entries
For example:
use rusty_dumb_tools::prelude::*;
let mut handler = InPlaceJsonEntryHandler::new(|json_entry| {
println!(
"In-Place JSON entry: `{}` => `{}`",
json_entry.field_name, json_entry.field_value
);
assert!(json_entry.field_name == "greeting");
assert!(json_entry.field_value.to_string() == "Hi❗ How are youüúüUÜÙÛ❓ 👩⚕👨👩👧👦🇭🇰👍🏽😆");
});
let mut json_processor = DumbJsonProcessor::new(Box::new(&mut handler));
let json = r#"{ "greeting" : "Hi❗ How are youüúüUÜÙÛ❓ 👩⚕👨👩👧👦🇭🇰👍🏽😆" }"#;
let res = json_processor.push_json(json);
assert!(res.is_ok() && res.unwrap().is_empty());
print!("~~~");The output will be like:
In-Place JSON entry: `greeting` => `Hi❗ How are youüúüUÜÙÛ❓ 👩⚕👨👩👧👦🇭🇰👍🏽😆`
~~~
Note that InPlaceJsonEntryHandler is simply a helper that implements the JsonEntryHandler trait,
which acts as a callback to handle JsonEntry as soon as it comes:
JsonEntryHandler::handle_json_entryis called when a JSON entry comes to be handledJsonEntryis passed as argument whenJsonEntryHandler::handle_json_entryis calledJsonEntry::field_nametells the “path” of the JSON entry; see the example belowJsonEntry::field_valueis the value (JsonFieldValue) of the JSON entry:JsonFieldValue::Stringfor string valueJsonFieldValue::Wholefor integer valueJsonFieldValue::Decimalfor float valueJsonFieldValue::Booleanfor boolean valueJsonFieldValue::Nullfor null value
For example, for the following JSON:
{
"simple_key": "simple_value,
"nested": {
"nested_key": "nested_value"
},
"array": [ "item0", "item1" ],
"obj_array": [
{ "obj_key": "obj0" },
{ "obj_key": "obj1" }
]
}
The field-name/field-value pairs are:
- “simple_key” => “simple_value”
- “nested.nested_key” => “nested_value”
- “array.0” => “item0”
- “array.1” => “item1”
- “obj_array.0.obj_key” => “obj0”
- “obj_array.1.obj_key” => “obj1”
Implementations§
source§impl<'a> DumbJsonProcessor<'a>
impl<'a> DumbJsonProcessor<'a>
pub fn new( json_entry_handler: Box<&mut dyn JsonEntryHandler>, ) -> DumbJsonProcessor<'_>
sourcepub fn push_json_piece<'b>(
&mut self,
json_piece: &str,
progress: &'b mut ProcessJsonProgress,
) -> Result<&'b mut ProcessJsonProgress, DumbError>
pub fn push_json_piece<'b>( &mut self, json_piece: &str, progress: &'b mut ProcessJsonProgress, ) -> Result<&'b mut ProcessJsonProgress, DumbError>
push a JSON piece to the processor; note that the JSON piece can be a complete JSON, or part of a JSON; as soon as JSON entries are recognized, callback is called for those recognized JSON entries
it requires an input ProcessJsonProgress to keep track of the progress
- the initial
ProcessJsonProgresscan be created byProcessJsonProgress::new - after each push, the progress will be updated;
and you can use
ProcessJsonProgress::is_doneto check if the input ended up a complete JSON, or needing additional JSON pieces - if it is done, you can use
ProcessJsonProgress::get_remainingto get the remaining of the input outside of the JSON e.g. an empty string if “}” is the last character of the last input JSON piece
sourcepub fn push_json(&mut self, json: &str) -> Result<String, DumbError>
pub fn push_json(&mut self, json: &str) -> Result<String, DumbError>
like DumbJsonProcessor::push_json_piece but for a complete JSON
It returns the remaining after processing the complete JSON; e.g. an empty string if “}” is the last character of the last input JSON
sourcepub fn push_json_bytes<'b>(
&mut self,
bytes: &[u8],
progress: &'b mut ProcessJsonProgress,
) -> Result<&'b mut ProcessJsonProgress, DumbError>
pub fn push_json_bytes<'b>( &mut self, bytes: &[u8], progress: &'b mut ProcessJsonProgress, ) -> Result<&'b mut ProcessJsonProgress, DumbError>
like DumbJsonProcessor::push_json_piece but accepts [u8] bytes