1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! # jsonx_lib
//!
//! Strict RFC 8259 JSON: a parser that borrows from its input, a pull reader,
//! and a deterministic writer, with budgets that refuse hostile input before
//! it costs memory. No dependencies; `no_std` with `alloc`.
//!
//! ## Quick start
//!
//! ```
//! use jsonx_lib::{json, parse, to_string};
//!
//! let doc = parse(r#"{"name": "jsonx", "version": [0, 2, 0], "stable": false}"#)?;
//! assert_eq!(doc["name"], "jsonx");
//! assert_eq!(doc["version"][1].as_u64(), Some(2));
//!
//! let reply = json!({ "ok": true, "items": [1, 2, 3] });
//! assert_eq!(to_string(&reply), r#"{"ok":true,"items":[1,2,3]}"#);
//! # Ok::<(), jsonx_lib::Error>(())
//! ```
//!
//! ## The three ways in
//!
//! 1. **[`parse`] and [`to_string`]** (plus [`parse_bytes`] and
//! [`to_string_pretty`]): one call each, default budgets, duplicate keys
//! refused.
//! 2. **[`Parser`]**: the same parse with your own [`Limits`] and
//! [`DuplicateKeys`] policy, kept as a `const` and reused.
//! 3. **[`Reader`]**: the document as a stream of [`Event`]s, with
//! [`read_raw`](Reader::read_raw) to keep a value as its source text and
//! [`read_value`](Reader::read_value) to build just one part of it.
//!
//! ## What it guarantees
//!
//! - **Exactly RFC 8259.** Everything the grammar allows is accepted;
//! everything else is refused with an [`Error`] that names the first byte at
//! which the input stops being valid. Unpaired UTF-16 surrogate escapes,
//! which the grammar allows but which decode to no Unicode text, are
//! refused too.
//! - **Borrowing.** Strings without escapes and every number are slices of the
//! input; only strings with escapes are copied, once, into a buffer of
//! exactly the right size.
//! - **Exact numbers.** A [`Number`] keeps its text; conversion to `i64`,
//! `u64`, or `f64` happens when asked and never rounds silently into an
//! integer.
//! - **Budgets before allocation.** Depth, input size, string length, number
//! length, and value count are all checked before the memory they guard is
//! used ([`Limits`]).
//! - **No recursion on depth.** Parsing, writing, cloning, and comparing loop
//! over explicit stacks; dropping recurses at most a fixed 32 levels. A
//! million levels of nesting cannot overflow the stack.
//! - **Round trip.** Written output parses back to an equal value, and the
//! escaping cannot be broken out of by any string ([`write_string`]).
//! - **No panics** on any input, and `#![forbid(unsafe_code)]`.
//!
//! ## Feature flags
//!
//! - `std` (default): adds [`to_writer`] and [`to_writer_pretty`]. Without it
//! the crate is `no_std` and needs only `alloc`.
extern crate alloc;
pub use Array;
pub use ;
pub use Limits;
pub use Number;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// Parses one JSON document with the default [`Limits`], refusing duplicate
/// keys.
///
/// The result borrows from `text`: strings without escapes and all numbers are
/// slices of it. Call [`Value::into_owned`] to keep the value longer than the
/// text. To change a budget or the duplicate-key policy, use [`Parser`].
///
/// # Errors
///
/// The first syntax error or exceeded budget, at its byte offset. See
/// [`ErrorKind`] for every case.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{parse, ErrorKind};
///
/// let v = parse(r#"{"list": [1, 2.5, "three"]}"#)?;
/// assert_eq!(v["list"][2], "three");
///
/// let err = parse(r#"{"a": tru}"#).unwrap_err();
/// assert_eq!(err.kind(), ErrorKind::InvalidLiteral);
/// assert_eq!(err.offset(), 9);
/// # Ok::<(), jsonx_lib::Error>(())
/// ```
/// Parses one JSON document from bytes, which must be UTF-8, with the default
/// [`Limits`]. See [`Parser::parse_bytes`].
///
/// # Errors
///
/// [`ErrorKind::InvalidUtf8`] for bytes that are not UTF-8; otherwise as
/// [`parse`].
///
/// # Examples
///
/// ```
/// let body: &[u8] = br#"{"id": 1}"#;
/// assert_eq!(jsonx_lib::parse_bytes(body)?["id"], 1);
/// # Ok::<(), jsonx_lib::Error>(())
/// ```
/// The examples in `README.md`, compiled and run as doctests.
;
/// The examples in `docs/API.md`, compiled and run as doctests (with `std`,
/// which its `to_writer` example needs).
;
/// Support for the [`json!`] macro. Not public API.