13 stable releases

2.5.1 Dec 31, 2022
2.5.0 Sep 18, 2022
2.3.0 Apr 26, 2021
2.2.0 Nov 8, 2020
0.0.2 Jul 18, 2016

#65 in Encoding

Download history 108178/week @ 2024-07-20 115290/week @ 2024-07-27 111720/week @ 2024-08-03 100832/week @ 2024-08-10 108594/week @ 2024-08-17 114317/week @ 2024-08-24 103654/week @ 2024-08-31 177343/week @ 2024-09-07 103443/week @ 2024-09-14 103208/week @ 2024-09-21 116700/week @ 2024-09-28 115914/week @ 2024-10-05 116193/week @ 2024-10-12 123803/week @ 2024-10-19 117344/week @ 2024-10-26 97131/week @ 2024-11-02

472,725 downloads per month
Used in 36 crates (17 directly)

MIT license

56KB
920 lines

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

// 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.

Repository

https://github.com/rhysd/tinyjson

License

the MIT License

No runtime deps