• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1tinyjson
2========
3[![version](https://img.shields.io/crates/v/tinyjson.svg)](https://crates.io/crates/tinyjson)
4[![CI](https://github.com/rhysd/tinyjson/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/rhysd/tinyjson/actions)
5
6[tinyjson](https://crates.io/crates/tinyjson) is a library to parse/generate JSON format document.
7
8Goals of this library are
9
10- **Simplicity**: This library uses standard containers like `Vec` or `HashMap` as its internal representation
11  and exposes it to users. Users can operate JSON values via the standard APIs. And it keeps this crate as small
12  as possible.
13- **Explicit**: This library does not hide memory allocation from users. You need to allocate memory like `Vec`,
14  `String`, `HashMap` by yourself. It is good for readers of your source code to show where memory allocations
15  happen. And you can have control of how memory is allocated (e.g. allocating memory in advance with
16  `with_capacity` method).
17- **No dependencies**: This library is built on top of only standard libraries.
18- **No unsafe code**: This library is built with Safe Rust.
19- **Well tested**: This library is tested with famous test suites:
20  - [JSON checker in json.org](http://www.json.org/JSON_checker/)
21  - [JSONTestSuite](https://github.com/nst/JSONTestSuite)
22  - [JSON-Schema-Test-Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite)
23
24[Documentation](https://docs.rs/tinyjson/latest/tinyjson)
25
26## Requirements
27
28Rust stable toolchain.
29
30## Installation
31
32Add this crate to `dependencies` section of your `Cargo.toml`
33
34```toml
35[dependencies]
36tinyjson = "2"
37```
38
39## Example
40
41```rust
42use tinyjson::JsonValue;
43use std::collections::HashMap;
44use std::convert::TryInto;
45
46let s = r#"
47    {
48        "bool": true,
49        "arr": [1, null, "test"],
50        "nested": {
51            "blah": false,
52            "blahblah": 3.14
53        },
54        "unicode": "\u2764"
55    }
56"#;
57
58// Parse from strings
59let parsed: JsonValue = s.parse().unwrap();
60
61// Access to inner value represented with standard containers
62let object: &HashMap<_, _> = parsed.get().unwrap();
63println!("Parsed HashMap: {:?}", object);
64
65// Generate JSON string
66println!("{}", parsed.stringify().unwrap());
67// Generate formatted JSON string with indent
68println!("{}", parsed.format().unwrap());
69
70// Convert to inner value represented with standard containers
71let object: HashMap<_, _> = parsed.try_into().unwrap();
72println!("Converted into HashMap: {:?}", object);
73
74// Create JSON values from standard containers
75let mut m = HashMap::new();
76m.insert("foo".to_string(), true.into());
77let mut v = JsonValue::from(m);
78
79// Access with `Index` and `IndexMut` operators quickly
80println!("{:?}", v["foo"]);
81v["foo"] = JsonValue::from("hello".to_string());
82println!("{:?}", v["foo"]);
83```
84
85See [the document](https://docs.rs/tinyjson/latest/tinyjson) to know all APIs.
86
87## Repository
88
89https://github.com/rhysd/tinyjson
90
91## License
92
93[the MIT License](LICENSE.txt)
94