| Crates.io | jvars |
| lib.rs | jvars |
| version | 0.1.0 |
| created_at | 2025-05-28 10:43:40.394762+00 |
| updated_at | 2025-05-28 10:43:40.394762+00 |
| description | Tools for working with JSON via data paths |
| homepage | https://github.com/akmitrich/jvars |
| repository | https://github.com/akmitrich/jvars |
| max_upload_size | |
| id | 1692661 |
| size | 18,613 |
Simple tools to deal with JSON values via data paths.
The crate has DataPathExt trait implementation for
serde_json::Value.Pull requests are welcomed.
Let's say we have
{
"registered": "2018-07-24T06:26:18 -03:00",
"latitude": -1.198644,
"longitude": 18.3947,
"tags": [
"non",
"aute",
"amet",
"irure",
"officia",
"ea",
"cillum"
],
"friends": [
{
"id": 0,
"name": "Holt Stewart"
},
{
"id": 1,
"name": "Fuentes Carroll"
},
{
"id": 2,
"name": "Greta Kane"
}
],
"greeting": "Hello, Sanchez Daniels! You have 1 unread messages.",
"favoriteFruit": "apple"
}
Examples of paths:
registered -> "2018-07-24T06:26:18 -03:00"latitude -> -1.198644tags.3 -> "irure"friends.1 ->{
"id": 1,
"name": "Fuentes Carroll"
}
friends.1.id -> 1friends.1.name -> "Fuentes Carroll"tags ->[
"non",
"aute",
"amet",
"irure",
"officia",
"ea",
"cillum"
]
Hope you see the idea of data path clearly.
Update value in path friends.2.name="Мама". friends array becomes
[
{
"id": 0,
"name": "Holt Stewart"
},
{
"id": 1,
"name": "Fuentes Carroll"
},
{
"id": 2,
"name": "Мама"
}
]
Create value in path friends.3.name="Юлия". friends array becomes
[
{
"id": 0,
"name": "Holt Stewart"
},
{
"id": 1,
"name": "Fuentes Carroll"
},
{
"id": 2,
"name": "Мама"
},
{
"name": "Юлия"
}
]
Creating value inside array you expect that enough nulls will be added. Let's take
{
"array1": []
}
and create 5 in path array1.5. We end up with
{
"array1": [ null, null, null, null, null, 5 ]
}
I hope you get the idea behind update_or_create function.
let mut data = json!({
"friends": [
{
"id": 0,
"name": "Holt Stewart"
},
{
"id": 1,
"name": "Fuentes Carroll"
}
]
});
let id = data.path("friends.0.id").unwrap();
assert_eq!(0, id.as_i64().unwrap());
let name = data.path_mut("friends.1.name").unwrap();
*name = json!(42);
assert_eq!(42, data["friends"][1]["name"]);
let mut data = json!({
"friends": [
{
"id": 0,
"name": "Holt Stewart"
},
{
"id": 1,
"name": "Fuentes Carroll"
},
{
"id": 2,
"name": "Greta Kane"
}
]
});
data.update_or_create("friends.2.name", json!("Мама"))
.unwrap();
assert_eq!("Мама", data["friends"][2]["name"]);
data.update_or_create("friends.3.id", 42.into()).unwrap();
data.update_or_create("friends.3.name", "Юлия".into())
.unwrap();
assert_eq!(
json!({
"id": 42,
"name": "Юлия"
}),
data["friends"][3]
);
data.update_or_create("", Value::Bool(true)).unwrap();
assert!(data.as_bool().unwrap());
let mut data = json!({});
data.update_or_create("a.b.c.d.e.f", 543.into()).unwrap();
println!("{}", serde_json::to_string_pretty(&data).unwrap());
Output for above snippet:
{
"a": {
"b": {
"c": {
"d": {
"e": {
"f": 543
}
}
}
}
}
}
let mut data = json!({});
data.update_or_create("array1", json!([])).unwrap();
assert!(data["array1"].is_array());
let n = 501;
data.update_or_create(format!("array1.{}", n), true.into())
.unwrap();
for i in 0..(n - 1) {
assert!(data["array1"][i].is_null());
}
assert!(data["array1"][n].as_bool().unwrap());
Hope you may write some code which always gets None from nonexistent path inside serde_json::Value.