Expand description
tinyjson is a library to parse/generate JSON format document.
Goals of this library are
- Simplicity: This library uses standard containers like
Vec
orHashMap
as its internal representation and exposes it to users. Users can operate JSON values via the standard APIs. And it keeps this crate as small as possible. - Explicit: This library does not hide memory allocation from users. You need to allocate memory like
Vec
,String
,HashMap
by yourself. It is good for readers of your source code to show where memory allocations happen. And you can have control of how memory is allocated (e.g. allocating memory in advance withwith_capacity
method). - No dependencies: This library is built on top of only standard libraries.
- No unsafe code: This library is built with Safe Rust.
- Well tested: This library is tested with famous test suites:
Example:
use tinyjson::JsonValue;
use std::collections::HashMap;
use std::convert::TryInto;
let s = r#"
{
"bool": true,
"arr": [1, null, "test"],
"nested": {
"blah": false,
"blahblah": 3.14
},
"unicode": "\u2764"
}
"#;
// Parse from strings
let parsed: JsonValue = s.parse().unwrap();
// Access to inner value represented with standard containers
let object: &HashMap<_, _> = parsed.get().unwrap();
println!("Parsed HashMap: {:?}", object);
// Generate JSON string
println!("{}", parsed.stringify().unwrap());
// Generate formatted JSON string with indent
println!("{}", parsed.format().unwrap());
// Convert to inner value represented with standard containers
let object: HashMap<_, _> = parsed.try_into().unwrap();
println!("Converted into HashMap: {:?}", object);
// Create JSON values from standard containers
let mut m = HashMap::new();
m.insert("foo".to_string(), true.into());
let mut v = JsonValue::from(m);
// Access with `Index` and `IndexMut` operators quickly
println!("{:?}", v["foo"]);
v["foo"] = JsonValue::from("hello".to_string());
println!("{:?}", v["foo"]);
Any JSON value is represented with JsonValue
enum.
Each JSON types are mapped to Rust types as follows:
JSON | Rust |
---|---|
Number | f64 |
Boolean | bool |
String | String |
Null | () |
Array | Vec<JsonValue> |
Object | HashMap<String, JsonValue> |
Structs§
- Json
Generate Error - Serialization error. This error only happens when some write error happens on writing the serialized byte sequence
to the given
io::Write
object. - Json
Generator - JSON serializer for
JsonValue
. - Json
Parse Error - Parse error.
- Json
Parser - JSON parser to parse UTF-8 string into
JsonValue
value. - Unexpected
Value - Error caused when trying to convert
JsonValue
into some wrong type value.
Enums§
- Json
Value - Enum to represent one JSON value. Each variant represents corresponding JSON types.
Traits§
- Inner
AsRef - Trait to access to inner value of
JsonValue
as reference. - Inner
AsRef Mut - Trait to access to inner value of
JsonValue
as mutable reference.
Functions§
- format
- Serialize the given
JsonValue
value toString
with 2-spaces indentation. This method is almost identical toJsonValue::format
but exists for a historical reason. - stringify
- Serialize the given
JsonValue
value toString
without indentation. This method is almost identical toJsonValue::stringify
but exists for a historical reason.
Type Aliases§
- Json
Generate Result - Convenient type alias for serialization results.
- Json
Parse Result - Convenient type alias for parse results.