Forked from https://github.com/rhysd/tinyjson Simple JSON parser/generator for Rust https://crates.io/crates/tinyjson
Find a file
2023-01-10 19:57:03 +09:00
.github/workflows update old actions in CI workflow 2023-01-01 00:10:38 +09:00
bench add more practical benchmark data 2023-01-06 11:00:38 +09:00
examples add examples 2020-05-01 19:10:57 +09:00
fuzz add JsonValue::get_mut 2020-04-30 18:35:22 +09:00
src fix error message on unexpected character while parsing identifiers 2023-01-10 19:57:03 +09:00
tests refactor number parsing to reduce local flags 2023-01-10 19:49:26 +09:00
.gitattributes add more practical benchmark data 2023-01-06 11:00:38 +09:00
.gitignore add fuzzing for parser 2020-04-30 10:30:57 +09:00
.rustfmt.toml use Rust2018 edition 2020-04-28 12:23:07 +09:00
Cargo.toml bump up version to v2.5.1 2022-12-31 17:52:31 +09:00
LICENSE.txt first parser implementation without tests 2016-07-19 04:24:59 +09:00
README.md describe query API document 2023-01-07 23:32:29 +09:00

tinyjson

version CI

tinyjson is a library to parse/generate JSON format document.

Goals of this library are

  • Simplicity: This library uses standard containers like 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.
  • 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 with with_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:

Documentation

Requirements

Rust stable toolchain.

Installation

Add this crate to dependencies section of your Cargo.toml

[dependencies]
tinyjson = "2"

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());

// Access nested elements by .query() or .query_mut() without panic
let elem = parsed.query().child("arr").child(1).find();
println!("Second element of \"arr\": {:?}", elem);

// 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 (panic when no element)
println!("{:?}", v["foo"]);
v["foo"] = JsonValue::from("hello".to_string());
println!("{:?}", v["foo"]);

See the document to know all APIs.

Repository

https://github.com/rhysd/tinyjson

License

the MIT License