microjson 0.1.0

No frills JSON parsing without allocations
Documentation

Small JSON Parser in no_std

Build Status Latest Version Coverage Status

This library reads and parses JSON strings.

Its intended use case is to read a JSON payload once

It does not serialise data.

Sample usage

Simply put this in your Cargo.toml:

[dependencies]
microjson = { git = "https://github.com/rspencer01/microjson" }

You can read strings and integers easily:

use microjson::JSONValue;

let integer = JSONValue::parse("42")
    .expect("Could not parse json");

let value = integer.read_integer();

let string = JSONValue::parse("\"hello there\"")
    .expect("Could not parse json");

let value = integer.read_string();

You can read arrays like this:

use microjson::JSONValue;

let input = r#" [0, 1, 2, 3, 4, 5] "#;

let array = JSONValue::parse(input)
    .expect("Could not parse json");

for (n, item) in array.iter_array().unwrap().enumerate() {
    let value = item.read_integer()
                  .expect("Item was not an integer");
    assert_eq!(value, n as isize);
}

And, of course, any combination of the above:

use microjson::JSONValue;

let input = r#" { "arr": [3, "foo", 3.625, false] } "#;

let object = JSONValue::parse(input)
              .expect("Could not parse json");

assert_eq!(
    object.get_key_value("arr").unwrap().iter_array().unwrap().nth(2).unwrap().read_float(),
    Ok(3.625)
);

If you are unsure what kind of data you have, you can query the [JSONValueType].

use microjson::{JSONValue, JSONValueType};

let input = r#" 3.1415 "#;

let object = JSONValue::parse(input)
              .expect("Could not parse json");

match object.value_type {
    JSONValueType::String => {},
    JSONValueType::Number => {},
    JSONValueType::Object => {},
    JSONValueType::Array => {},
    JSONValueType::Bool => {},
    JSONValueType::Null => {},
}