serde_json_string_parse

Crates.ioserde_json_string_parse
lib.rsserde_json_string_parse
version0.1.2
sourcesrc
created_at2022-06-08 11:34:57.547494
updated_at2022-06-08 11:40:23.818942
descriptionParse string trait as json returning original string in case of error
homepage
repositoryhttps://github.com/DevNulPavel/serde_json_string_parse
max_upload_size
id602027
size18,649
(DevNulPavel)

documentation

https://docs.rs/crate/serde_json_string_parse/

README

Serde json string parse

Provides ParseJson trait and JsonParseError struct which allow parse String or &str types to struct using parse_json_with_data_err trait method.

In case of error JsonParseError contains original String or &str and original parsing error from serde.

Examples

use serde_json_string_parse::{JsonParseError, ParseJson};

#[derive(Deserialize, Debug)]
struct TestStruct {
    key: String,
}

#[rustfmt::skip]
let text = String::from(r#"{
    "key": "value"
}"#);

let parse_result: TestStruct = text
    .parse_json_with_data_err()
    .expect("Parsing failed");
assert_eq!(parse_result.key, "value");
use serde_json_string_parse::{JsonParseError, ParseJson};

#[derive(Deserialize, Debug)]
struct TestStruct {
    key: String,
}

#[rustfmt::skip]
let text = String::from(r#"{
    "key" ___ "value"
}"#);

let parse_error: JsonParseError<String> = text
    .clone()
    .parse_json_with_data_err::<TestStruct>()
    .expect_err("Parsing must fail");

// `original_data` field contains source text
assert_eq!(parse_error.original_data, text);
Commit count: 4

cargo fmt