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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Example demonstrating ArrayBitStack for large nesting depths
use picojson::{ArrayBitStack, Event, ParseError, PullParser, SliceParser};
fn main() -> Result<(), ParseError> {
println!("=== ArrayBitStack Demo ===\n");
// Generate deeply nested JSON with mixed objects and arrays (70+ levels)
let deep_json = generate_deep_mixed_json(65);
println!("1. ArrayBitStack<3, u32> (96-bit depth) - Mixed {{}} and [] nesting to depth ~65:");
println!(
" Generated JSON (first 100 chars): {}",
&deep_json[..deep_json.len().min(100)]
);
println!(" JSON structure: obj->arr->obj->arr->... (alternating pattern)");
let mut scratch = [0u8; 2048];
let mut parser =
SliceParser::<ArrayBitStack<3, u32, u16>>::with_config_and_buffer(&deep_json, &mut scratch);
let mut depth = 0;
let mut max_depth = 0;
loop {
match parser.next() {
Some(Ok(event)) => match event {
Event::StartObject => {
depth += 1;
max_depth = max_depth.max(depth);
if depth <= 5 || depth % 10 == 0 {
println!(
" {}StartObject (depth: {})",
" ".repeat((depth - 1).min(3)),
depth
);
}
}
Event::StartArray => {
depth += 1;
max_depth = max_depth.max(depth);
if depth <= 5 || depth % 10 == 0 {
println!(
" {}StartArray (depth: {})",
" ".repeat((depth - 1).min(3)),
depth
);
}
}
Event::EndObject => {
if depth <= 5 || depth % 10 == 0 {
println!(
" {}EndObject (depth: {})",
" ".repeat((depth - 1).min(3)),
depth
);
}
depth -= 1;
}
Event::EndArray => {
if depth <= 5 || depth % 10 == 0 {
println!(
" {}EndArray (depth: {})",
" ".repeat((depth - 1).min(3)),
depth
);
}
depth -= 1;
}
Event::Key(key) => {
if depth <= 5 {
println!(" {}Key: '{}'", " ".repeat(depth.min(3)), key);
}
}
Event::String(s) => {
println!(
" {}String: '{}' (at max depth: {})",
" ".repeat(depth.min(3)),
s,
depth
);
}
Event::Number(num) => {
println!(
" {}Number: {} (at max depth: {})",
" ".repeat(depth.min(3)),
num,
depth
);
}
Event::EndDocument => break,
_ => {}
},
Some(Err(_)) => {
println!(
" ! Parse error encountered at depth {}, continuing...",
depth
);
break;
}
None => break,
}
}
println!(
" ✅ Successfully parsed {} levels of mixed nesting!\n",
max_depth
);
// Test ArrayBitStack with smaller elements for memory efficiency
println!("2. ArrayBitStack<8, u8> (64-bit depth tracking) - Complex nested structure:");
let complex_json = generate_complex_nested_json(25);
println!(" JSON structure: Objects with arrays containing objects with data");
let mut scratch = [0u8; 1024];
let mut parser = SliceParser::<ArrayBitStack<8, u8, u8>>::with_config_and_buffer(
&complex_json,
&mut scratch,
);
let mut depth = 0;
let mut max_depth = 0;
while let Some(event) = parser.next() {
match event? {
Event::StartArray => {
depth += 1;
max_depth = max_depth.max(depth);
if depth <= 8 {
println!(" {}StartArray (depth: {})", " ".repeat(depth), depth);
}
}
Event::StartObject => {
depth += 1;
max_depth = max_depth.max(depth);
if depth <= 8 {
println!(" {}StartObject (depth: {})", " ".repeat(depth), depth);
}
}
Event::EndArray => {
if depth <= 8 {
println!(" {}EndArray (depth: {})", " ".repeat(depth), depth);
}
depth -= 1;
}
Event::EndObject => {
if depth <= 8 {
println!(" {}EndObject (depth: {})", " ".repeat(depth), depth);
}
depth -= 1;
}
Event::Key(key) => {
if depth <= 8 {
println!(" {}Key: '{}'", " ".repeat(depth), key);
}
}
Event::Number(num) => {
if depth <= 8 {
println!(" {}Number: {}", " ".repeat(depth), num);
}
}
Event::String(s) => {
if depth <= 8 {
println!(" {}String: '{}'", " ".repeat(depth), s);
}
}
Event::EndDocument => break,
_ => {}
}
}
println!(
" ✅ Successfully parsed {} levels of complex nesting!\n",
max_depth
);
println!("✅ ArrayBitStack configurations working!");
println!();
println!("ArrayBitStack Summary:");
println!("• ArrayBitStack<3, u32>: 96-bit depth (3 × 32 bits)");
println!("• ArrayBitStack<8, u8>: 64-bit depth (8 × 8 bits) - memory efficient");
println!("• ArrayBitStack<16, u32>: 512-bit depth (16 × 32 bits) - ultra deep");
println!("• Configurable element type (u8, u16, u32, u64) and array size");
Ok(())
}
/// Generate deeply nested JSON with alternating objects and arrays
/// Pattern: {"level0": [{"level2": [{"level4": ... "data"}]}]}
fn generate_deep_mixed_json(depth: usize) -> String {
let mut json = String::new();
// Opening structures (alternating object/array)
for i in 0..depth {
if i % 2 == 0 {
// Object level
json.push_str(&format!(r#"{{"level{}":"#, i));
} else {
// Array level
json.push('[');
}
}
// Core data at the deepest level
json.push_str(r#""reached_the_deep_end\n""#);
// Closing structures (reverse order)
for i in (0..depth).rev() {
if i % 2 == 0 {
// Close object
json.push('}');
} else {
// Close array
json.push(']');
}
}
json
}
/// Generate complex nested JSON with realistic structure
/// Pattern: [{"data": [{"data": [{"value": 123}]}]}]
fn generate_complex_nested_json(depth: usize) -> String {
let mut json = String::new();
// Start with array
json.push('[');
for i in 0..depth {
if i % 3 == 0 {
// Object with "data" key
json.push_str(r#"{"data":"#);
} else if i % 3 == 1 {
// Array
json.push('[');
} else {
// Object with "nested" key
json.push_str(r#"{"nested":"#);
}
}
// Core data
json.push_str(&format!(
r#"{{"value": {}, "msg": "depth_{}_reached"}}"#,
depth * 42,
depth
));
// Close all structures
for i in (0..depth).rev() {
if i % 3 == 0 || i % 3 == 2 {
// Close object
json.push('}');
} else {
// Close array
json.push(']');
}
}
// Close initial array
json.push(']');
json
}