commit | ab53e1abcbf8ead8741b7f0e18e2bfc8833f207b | [log] [tgz] |
---|---|---|
author | Maciej Żenczykowski <maze@google.com> | Thu Sep 28 00:56:36 2023 +0000 |
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | Thu Sep 28 00:56:36 2023 +0000 |
tree | 4973938630bbb627309611a2f616e4a5ca2e7063 | |
parent | 8510b006a6a6473e19ee115c373e70595dbcfa5f [diff] | |
parent | f033c7b68768ca3e04fce1ec46c3d66c25713bf9 [diff] |
OWNERS: master -> main am: bd3c80f2d6 am: 165c8c9ee6 am: f033c7b687 Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/tinyjson/+/2765785 Change-Id: Ic67a4de794cd3125cbd79223d4080b1fe903f39f Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
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