commit | a78298290b4b4cbf92eb1511d1fab6646ef0b76c | [log] [tgz] |
---|---|---|
author | Android Build Coastguard Worker <[email protected]> | Wed Dec 06 00:17:09 2023 +0000 |
committer | Android Build Coastguard Worker <[email protected]> | Wed Dec 06 00:17:09 2023 +0000 |
tree | 36b7860d168f0946cbec125ce85feec1522a02b5 | |
parent | 9808a2e5e44bca4a028f6a939c3cd5f528b4f63d [diff] | |
parent | 2fae679acc7d129df20107a59b7df447dfbc244d [diff] |
Snap for 11180250 from 2fae679acc7d129df20107a59b7df447dfbc244d to 24Q1-release Change-Id: I098acf6c66e346265fd51e7dd05df8f9281580e7
tinyjson is a library to parse/generate JSON format document.
Goals of this library are
Vec
or HashMap
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.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 with with_capacity
method).Rust stable toolchain.
Add this crate to dependencies
section of your Cargo.toml
[dependencies] tinyjson = "2"
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"]);
See the document to know all APIs.
https://github.com/rhysd/tinyjson