• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! [tinyjson](https://crates.io/crates/tinyjson) is a library to parse/generate JSON format document.
2 //!
3 //! Goals of this library are
4 //!
5 //! - **Simplicity**: This library uses standard containers like `Vec` or `HashMap` as its internal representation
6 //!   and exposes it to users. Users can operate JSON values via the standard APIs. And it keeps this crate as small
7 //!   as possible.
8 //! - **Explicit**: This library does not hide memory allocation from users. You need to allocate memory like `Vec`,
9 //!   `String`, `HashMap` by yourself. It is good for readers of your source code to show where memory allocations
10 //!   happen. And you can have control of how memory is allocated (e.g. allocating memory in advance with
11 //!   `with_capacity` method).
12 //! - **No dependencies**: This library is built on top of only standard libraries.
13 //! - **No unsafe code**: This library is built with Safe Rust.
14 //! - **Well tested**: This library is tested with famous test suites:
15 //!   - [JSON checker in json.org](http://www.json.org/JSON_checker/)
16 //!   - [JSONTestSuite](https://github.com/nst/JSONTestSuite)
17 //!   - [JSON-Schema-Test-Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite)
18 //!
19 //! Example:
20 //!
21 //! ```
22 //! use tinyjson::JsonValue;
23 //! use std::collections::HashMap;
24 //! use std::convert::TryInto;
25 //!
26 //! let s = r#"
27 //!     {
28 //!         "bool": true,
29 //!         "arr": [1, null, "test"],
30 //!         "nested": {
31 //!             "blah": false,
32 //!             "blahblah": 3.14
33 //!         },
34 //!         "unicode": "\u2764"
35 //!     }
36 //! "#;
37 //!
38 //! // Parse from strings
39 //! let parsed: JsonValue = s.parse().unwrap();
40 //!
41 //! // Access to inner value represented with standard containers
42 //! let object: &HashMap<_, _> = parsed.get().unwrap();
43 //! println!("Parsed HashMap: {:?}", object);
44 //!
45 //! // Generate JSON string
46 //! println!("{}", parsed.stringify().unwrap());
47 //! // Generate formatted JSON string with indent
48 //! println!("{}", parsed.format().unwrap());
49 //!
50 //! // Convert to inner value represented with standard containers
51 //! let object: HashMap<_, _> = parsed.try_into().unwrap();
52 //! println!("Converted into HashMap: {:?}", object);
53 //!
54 //! // Create JSON values from standard containers
55 //! let mut m = HashMap::new();
56 //! m.insert("foo".to_string(), true.into());
57 //! let mut v = JsonValue::from(m);
58 //!
59 //! // Access with `Index` and `IndexMut` operators quickly
60 //! println!("{:?}", v["foo"]);
61 //! v["foo"] = JsonValue::from("hello".to_string());
62 //! println!("{:?}", v["foo"]);
63 //! ```
64 //!
65 //! Any JSON value is represented with [`JsonValue`] enum.
66 //!
67 //! Each JSON types are mapped to Rust types as follows:
68 //!
69 //! | JSON    | Rust                         |
70 //! |---------|------------------------------|
71 //! | Number  | `f64`                        |
72 //! | Boolean | `bool`                       |
73 //! | String  | `String`                     |
74 //! | Null    | `()`                         |
75 //! | Array   | `Vec<JsonValue>`             |
76 //! | Object  | `HashMap<String, JsonValue>` |
77 
78 // This library is built with Safe Rust
79 #![forbid(unsafe_code)]
80 // Suppress warning which prefers `matches!` macro to `match` statement since the macro was
81 // introduced in recent Rust 1.42. This library should support older Rust.
82 #![allow(clippy::match_like_matches_macro)]
83 
84 mod generator;
85 mod json_value;
86 mod parser;
87 
88 pub use generator::*;
89 pub use json_value::{InnerAsRef, InnerAsRefMut, JsonValue, UnexpectedValue};
90 pub use parser::*;
91