Crates.io | plainjson |
lib.rs | plainjson |
version | 0.1.2 |
source | src |
created_at | 2021-06-15 06:51:00.077275 |
updated_at | 2022-04-12 10:46:21.44565 |
description | A library simply provides low-level access and JSONPath way to query or set JSON values. |
homepage | |
repository | https://github.com/longrunsoul/plainjson |
max_upload_size | |
id | 410270 |
size | 68,711 |
This library simply provides:
Note: Filter expression in JSONPath is not implemented yet, so currently filter expression is not supported.
Getting value by JSONPath is like:
use plainjson::JsonNode;
fn get_value_by_json_path() {
let json = r#"{"a": 123, "b": {"c": "hello"}}"#;
let mut json = JsonNode::parse_single_node(json.as_bytes()).unwrap();
let c = json.get_str("$.b.c").unwrap();
assert_eq!(c, Some(String::from("hello")));
}
Setting value by JSONPath is like:
use plainjson::JsonNode;
fn set_value_by_json_path() {
let json = r#"{"a": 123, "b": [3, 2, 1]}"#;
let mut json = JsonNode::parse_single_node(json.as_bytes()).unwrap();
json.set_bool("$.b[1]", true).unwrap();
assert_eq!(json.to_string(), r#"{"a": 123, "b": [3, true, 1]}"#)
}
If you need to access low-level JSON tags, use JsonTag:
use plainjson::JsonTag;
fn fix_json() {
let json = r#"{"a": test, "b": "world"}"#;
let mut tags = JsonTag::parse(json.as_bytes()).unwrap();
tags[3] = JsonTag::Literal(String::from(r#""test""#));
assert_eq!(JsonTag::to_string(&tags), r#"{"a": "test", "b": "world"}"#);
}
This library is licensed under MIT license.