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:

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>

source

pub fn new( json_entry_handler: Box<&mut dyn JsonEntryHandler>, ) -> DumbJsonProcessor<'_>

source

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

source

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

source

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

Auto Trait Implementations§

§

impl<'a> Freeze for DumbJsonProcessor<'a>

§

impl<'a> !RefUnwindSafe for DumbJsonProcessor<'a>

§

impl<'a> !Send for DumbJsonProcessor<'a>

§

impl<'a> !Sync for DumbJsonProcessor<'a>

§

impl<'a> Unpin for DumbJsonProcessor<'a>

§

impl<'a> !UnwindSafe for DumbJsonProcessor<'a>

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.