simple_json
is a JSON run-time recursive parser created for simplyfing both reading and writing JSON-encoded data. It's also supposed to be compatible with the current stable release so if you detect any problems, please report them.
Although I believe this package runs fast enough for most cases, it wasn't made with performance or big data sets in mind. In those cases you may be better using Serde instead.
This package abuses Rust's Option
feature and creates a tree, where all nodes fit into one of 6 types:
Json::Object(HashMap<String, Json>)
Json::Array(Vec<Json>)
Json::String(String)
Json::Number(simple_json::Number)
Json::Boolean(bool)
Json::Null
All of them implement the From
trait in both ways, so converting between them and their native counterparts should be as easy as calling into()
.
Number
is another enum created to represent the multiple different types a JSON Number can hold. The parser will try to choose the most sensible type for each situation, so this should be transparent to user.
A Number
can be one of 3 types:
Number::Float(f64)
, if its a floating number (ie.3.2
) or if its written in scientific notation (ie.3e2
);Number::Integer(i64)
, if the parser receives a signed number (ie.-1
);Number::Unsigned(u64)
, for everything else (ie.42
).
Here's a simple example:
extern crate simple_json;
use simple_json::Json;
fn main() {
let mut text = "{ \"integer\": 12, \"float\": 80.5, \"string\": \"A JSON sample\", \"array\": [ 1, 2, 3 ], \"object\": { \"a\": \"b\" } }";
let result = Json::parse(text).unwrap();
println!("Result: {}", result.to_string());
}
Output:
Result: {
"integer": 12,
"float": 80.5,
"string": "A JSON sample",
"array": [ 1, 2, 3 ],
"object": {
"a": "b"
}
}