tinyjson is a library to parse/generate JSON format document.
- Using Stable APIs; using no experimental APIs, no compiler plugin
- Reasonable simple JSON object interface
- No crate dependencies at runtime
- Well tested with famous JSON test suites
- JSON checker in json.org
- My Rust practice :)
Rust stable toolchain (no dependency).
Add this crate to
dependencies section of your
String is parsed to
JsonValue struct via
str::parse() is available. It parses the target as JSON and creates
tinyjson::JsonValue object. It represents tree structure of parsed JSON.
JsonValue is an
enum struct and allocated on stack. So it doesn't require additional heap allocation.
Access to JSON Value
JsonValue is an
enum value. So we can access it with
Each JSON types correspond to Rust types as follows:
JSON is a tree structure and it's boring to write nested
match statement. So
std::ops::IndexMut traits in order to access to its nested values quickly.
Index access with
&str key is available when the value is an object. And index access with
usize is available when the value is an array. They return the
&JsonValue value if target value was found. And modifying inner value directly with index access at right hand side of
= is also available. Note that it can modify value of objects but cannot add new key. In both cases, it will call
panic! when the value for key or the element of index was not found.
get_mut() methods are provided to dereference the
enum value (e.g.
get() method returns its dereferenced raw value. It returns
T is corresponding value that you expected). If
None is returned, it means its type mismatched with your expected one. Which type
get() should dereference is inferred from how the returned value will be handled. So you don't need to specify it explicitly.
TryInto. It can convert
JsonValue into inner value.
PartialEq traits hence it can be checked with
If you want to check its type only, there are
is_xxx() shortcut methods in
JsonValue instead of using
match statement explicitly.
stringify() method can be used to create JSON string.
Working examples are put in
examples directory. They can be run with
cargo run --example.
- Equality of
- Index access to
the MIT License