Crates.io | serde_json_string_parse |
lib.rs | serde_json_string_parse |
version | 0.1.2 |
source | src |
created_at | 2022-06-08 11:34:57.547494 |
updated_at | 2022-06-08 11:40:23.818942 |
description | Parse string trait as json returning original string in case of error |
homepage | |
repository | https://github.com/DevNulPavel/serde_json_string_parse |
max_upload_size | |
id | 602027 |
size | 18,649 |
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.
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);