A set of JSON common tools in Rust.
Disclaimer
read_str
json::JsonValue
from str read_file
json::JsonValue
from filepath_exists
bool
if dotted path existsget_path
Option<json::JsonValue>
parsetovec
json::JsonValue
array as Vec<json::JsonValue>
serialize
serializetobson_hex
jsonfrombson_hex
json::JsonValue
from a Hex Stringparsetodocument
bson::Document
from a json::JsonValue
jsonfromdocument
json::JsonValue
from a bson::Document
parsetobson
bson::Bson
from a json::JsonValue
jsonfrombson
json::JsonValue
from a bson::Bson
```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_file(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
```
You can get a child element by accessing via index, see this example
json
{
"car": {
"model": "Gurgel Itaipu E400",
"variants": [
{
"fuel": "gasoline",
"color": "yellow"
},
{
"fuel": "eletric",
"color": "red"
}
]
}
}
```rust let json = jsonc.read_str(content);
jsonc.getpath("car.variants", json); // The output is Ok, containing a JsonValue jsonc.getpath("car.variants.0", json); // The output is Ok, containing a JsonValue jsonc.get_path("car.variants.1.fuel", json); // The output is Ok, containing a JsonValue
jsonc.getpath("car.variants.99.fuel", json); // The output is None jsonc.getpath("car.variants.one", json); // The output is None ```
Consider the following content
json
{
"cars": [
{
"model": "Gurgel Itaipu E400",
"color": "red"
},
{
"model": "Gurgel X15",
"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
```
This feature is equivalent to Javascript _JSON.stringify_
Consider the following content
json
{
"car": {
"model": "Gurgel Itaipu E4000",
"color": "red"
}
}
Using serialize the output is
rust
let serialized = jsonc.serialize(myjson);
assert_eq!("{\"car\":{\"model\":\"Gurgel Itaipu E400\",\"color\":\"red\"}}", serialized);
rust
let bson_hex = jsonc.serialize_to_bson_hex(myjson);
assert_eq!("3c000000036361720032000000026d6f64656c001300000047757267656c2049746169707520453430300002636f6c6f720004000000726564000000", bson_hex)
You can check bson hex using this tool
json::JsonValue
from a Hex Stringbson::Document
from a json::JsonValue
json::JsonValue
from a bson::Document
bson::Bson
from a json::JsonValue
json::JsonValue
from a bson::Bson