no_std
This library reads and parses JSON strings.
Its intended use case is to read a JSON payload once.
It does not serialise data.
Simply put this in your Cargo.toml
:
toml
[dependencies]
microjson = "0.1"
You can read strings and integers easily: ```rust
let integer = JSONValue::parse("42")?;
let value : isize = integer.read_integer()?;
let string = JSONValue::parse("\"hello there\"")?;
let value : &str = string.read_string()?;
```
You can read arrays like this: ```rust
let input = r#" [0, 1, 2, 3, 4, 5] "#;
let array = JSONValue::parse(input)?;
for (n, item) in array.iterarray()?.enumerate() { let value = item.readinteger()?; assert_eq!(value, n as isize); }
```
And, of course, any combination of the above: ```rust
let input = r#" { "arr": [3, "foo", 3.625, false] } "#;
let object = JSONValue::parse(input)?;
asserteq!( object.getkeyvalue("arr")?.iterarray()?.nth(2).unwrap().read_float()?, 3.625 );
```
If you are unsure what kind of data you have, you can query the [JSONValueType
].
```rust
let input = r#" 3.1415 "#;
let object = JSONValue::parse(input)?;
match object.value_type { JSONValueType::String => {}, JSONValueType::Number => {}, JSONValueType::Object => {}, JSONValueType::Array => {}, JSONValueType::Bool => {}, JSONValueType::Null => {}, }
```
To load some JSON, you need only call ```rust
let value = JSONValue::parse(r#" [1,2,3,5"foo"] "#);
``
However, this data is malformed. [
JSONValue::parse] will return an
Ok` result, as to determine that the data was corrupt would require scanning through the entire string.
The error would only be reported when you attempted to iterate to the fourth item and parse it as a value.
If you need to know that the data is sound, use [JSONValue::verify
]. Alternatively, you can parse and verify in one step.
```rust
let value = JSONValue::parseandverify(r#" [1,2,3,5"foo"] "#); ```