A set of JSON common tools in Rust
```rust
use json_commons::JsonCommons;
fn main() { let jsonc = JsonCommons::new(); } ```
Parsing features allows to read a JSON content but in case of failure a panic error occurs
```rust
let content = "{\"car\": {\"model\": \"Gurgel Itaipu E400\", \"color\": \"red\"}}"; let json = jsonc.read_str(content);
```
```rust
let path = "/user/docs/myfile.json"; let json = jsonc.read_str(path);
```
The following examples uses this json content
json
{
"car": {
"model": "Gurgel Itaipu E400",
"color": "red"
}
}
You can check if the path exists
```rust
let json = jsonc.read_str(content);
jsonc.pathexists("car", json); // The output is True jsonc.pathexists("car.model", json); // The output is True
jsonc.pathexists("car.name", json); // The output is False jsonc.pathexists("person", json); // The output is False
```
You can get any path as an optional
```rust
let json = jsonc.read_str(content);
jsonc.getpath("car", json); // The output is Ok, containing a JsonValue jsonc.getpath("car.model", json); // The output is Ok, containing a JsonValue
jsonc.getpath("car.name", json); // The output is None jsonc.getpath("person", json); // The output is None
```
Attention: The dotted path does not work in lists
Consider the following content
json
{
"cars": [
{
"model": "Gurgel Itaipu E400",
"color": "red"
},
{
"model": "Gurgel Itaipu E400",
"color": "brown"
}
]
}
You can parse a list to a vec
```rust
let json = jsonc.read_str(content);
let cars = jsonc.getpath("cars", json).unwrap(); jsonc.parseto_vec(cars); // The output is a vec of JsonValue, with len 2
```