json-commons
A set of JSON common tools in Rust.
Disclaimer
Features
-
read_str
- return a
json::JsonValue
from str
- return a
-
read_file
- return a
json::JsonValue
from file
- return a
-
path_exists
- return a
bool
if dotted path exists
- return a
-
get_path
- return an
Option<json::JsonValue>
- return an
-
parse_to_vec
- return a
json::JsonValue
array asVec<json::JsonValue>
- return a
-
serialize
- return a JSON string, like a Javascript "JSON.stingify"
-
serialize_to_bson_hex
- return a Hex String that represents BSON
-
json_from_bson_hex
- return a
json::JsonValue
from a Hex String
- return a
-
parse_to_document
- return a
bson::Document
from ajson::JsonValue
- return a
-
json_from_document
- return a
json::JsonValue
from abson::Document
- return a
-
parse_to_bson
- return a
bson::Bson
from ajson::JsonValue
- return a
-
json_from_bson
- return a
json::JsonValue
from abson::Bson
- return a
Usage
use JsonCommons;
Parsing Features
Parsing features allows to read a JSON content but in case of failure a panic error occurs
Reading a JSON String
let content = "{\"car\": {\"model\": \"Gurgel Itaipu E400\", \"color\": \"red\"}}";
let json = jsonc.read_str;
Reading a JSON File
let path = "/user/docs/myfile.json";
let json = jsonc.read_file;
Dotted Paths Features
The following examples uses this json content
Path Exists
You can check if the path exists
let json = jsonc.read_str;
jsonc.path_exists; // The output is True
jsonc.path_exists; // The output is True
jsonc.path_exists; // The output is False
jsonc.path_exists; // The output is False
Get Path
You can get any path as an optional
let json = jsonc.read_str;
jsonc.get_path; // The output is Ok, containing a JsonValue
jsonc.get_path; // The output is Ok, containing a JsonValue
jsonc.get_path; // The output is None
jsonc.get_path; // The output is None
Dotted Paths with Lists
You can get a child element by accessing via index, see this example
let json = jsonc.read_str;
jsonc.get_path; // The output is Ok, containing a JsonValue
jsonc.get_path; // The output is Ok, containing a JsonValue
jsonc.get_path; // The output is Ok, containing a JsonValue
jsonc.get_path; // The output is None
jsonc.get_path; // The output is None
List Features
Consider the following content
Parse to Vec
You can parse a list to a vec
let json = jsonc.read_str;
let cars = jsonc.get_path.unwrap;
jsonc.parse_to_vec; // The output is a vec of JsonValue, with len 2
Serializer Features
Serialize
This feature is equivalent to Javascript JSON.stringify
Consider the following content
Using serialize the output is
let serialized = jsonc.serialize;
assert_eq!;
Serialize to BSON Hex
let bson_hex = jsonc.serialize_to_bson_hex;
assert_eq!
You can check bson hex using this tool
Parser Features
- json_from_bson_hex
- return a
json::JsonValue
from a Hex String
- return a
- parse_to_document
- return a
bson::Document
from ajson::JsonValue
- return a
- json_from_document
- return a
json::JsonValue
from abson::Document
- return a
- parse_to_bson
- return a
bson::Bson
from ajson::JsonValue
- return a
- json_from_bson
- return a
json::JsonValue
from abson::Bson
- return a