qubit-json 0.12.3

Resource-aware infrastructure for lenient and strict JSON processing
Documentation
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Serde visitor that builds and accounts a JSON value.

use std::fmt;
use std::fmt::Debug;

use qubit_budget::ResourceQuantity;
use qubit_budget::json::JsonContainerKind;
use qubit_budget::json::JsonMeasurement;
use qubit_budget::json::JsonValueTransaction;
use serde::Deserializer;
use serde::de::DeserializeSeed;
use serde::de::Error;
use serde::de::MapAccess;
use serde::de::SeqAccess;
use serde::de::Visitor;
use serde_json::Map;
use serde_json::Number;
use serde_json::Value;

use super::JsonKeySeed;
use super::JsonValueChildSeed;
use super::json_number_lexeme_length;
use crate::internal::StrictJsonNumber;

/// Visitor used to construct and account one JSON value.
///
/// # Type Parameters
///
/// * `R` - Resource identity retained by the transaction's limits and errors.
/// * `Q` - Exact unsigned quantity used for measurements and accounting.
pub(in crate::value) struct JsonValueVisitor<'transaction, 'budget, R, Q>
where
    Q: ResourceQuantity,
{
    /// Transaction receiving measurements for the value under construction.
    transaction: &'transaction mut JsonValueTransaction<'budget, R, Q>,

    /// Root-inclusive depth of the value currently being visited.
    depth: usize,

    /// Whether duplicate decoded object keys are rejected before their values.
    reject_duplicate_keys: bool,
}

impl<'transaction, 'budget, R, Q> JsonValueVisitor<'transaction, 'budget, R, Q>
where
    R: Clone + Debug,
    Q: ResourceQuantity,
{
    /// Creates a visitor that accounts a value at `depth`.
    ///
    /// # Parameters
    ///
    /// * `transaction` - Transaction that receives measurements for the value.
    /// * `depth` - Root-inclusive depth assigned to the value.
    /// * `reject_duplicate_keys` - Whether repeated decoded object keys fail.
    #[inline]
    pub(in crate::value) fn new(
        transaction: &'transaction mut JsonValueTransaction<'budget, R, Q>,
        depth: usize,
        reject_duplicate_keys: bool,
    ) -> Self {
        Self {
            transaction,
            depth,
            reject_duplicate_keys,
        }
    }

    /// Stages one complete JSON measurement.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used to report accounting failures.
    ///
    /// # Parameters
    ///
    /// * `measurement` - Measurement to admit into the transaction.
    fn admit<E>(&mut self, measurement: JsonMeasurement) -> Result<(), E>
    where
        E: Error,
    {
        self.transaction.try_admit(measurement).map_err(E::custom)
    }

    /// Charges a number's decoded text representation.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used to report accounting failures.
    ///
    /// # Parameters
    ///
    /// * `number` - JSON number whose representation is charged.
    fn enter_number<E>(&mut self, number: Number) -> Result<Value, E>
    where
        E: Error,
    {
        self.admit(JsonMeasurement::Number {
            depth: self.depth,
            bytes: json_number_lexeme_length(&number),
        })?;
        Ok(Value::Number(number))
    }

    /// Creates a child visitor borrowing the same transaction.
    fn child<'child>(&'child mut self) -> JsonValueVisitor<'child, 'budget, R, Q> {
        JsonValueVisitor::new(
            self.transaction,
            self.depth.saturating_add(1),
            self.reject_duplicate_keys,
        )
    }

    /// Creates a child seed that checks a container limit before decoding.
    ///
    /// # Parameters
    ///
    /// * `kind` - Container kind whose prospective child is checked.
    /// * `prospective` - One-based child count to validate before decoding.
    fn prospective_child<'child>(
        &'child mut self,
        kind: JsonContainerKind,
        prospective: usize,
    ) -> JsonValueChildSeed<'child, 'budget, R, Q> {
        JsonValueChildSeed {
            transaction: self.transaction,
            depth: self.depth.saturating_add(1),
            kind,
            prospective,
            reject_duplicate_keys: self.reject_duplicate_keys,
        }
    }
}

impl<'de, R, Q> DeserializeSeed<'de> for JsonValueVisitor<'_, '_, R, Q>
where
    R: Clone + Debug,
    Q: ResourceQuantity,
{
    /// JSON value produced after the seed delegates deserialization.
    type Value = Value;

    /// Starts an `deserialize_any` visit for the requested JSON value.
    ///
    /// # Type Parameters
    ///
    /// * `D` - Deserializer supplying the JSON value.
    ///
    /// # Parameters
    ///
    /// * `deserializer` - Source that presents the value to this seed.
    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(self)
    }
}

impl<'de, 'transaction, 'budget, R, Q> Visitor<'de>
    for JsonValueVisitor<'transaction, 'budget, R, Q>
where
    R: Clone + Debug,
    Q: ResourceQuantity,
{
    /// JSON value assembled from the deserializer's visitor events.
    type Value = Value;

    /// Describes the JSON value shape accepted by this visitor.
    ///
    /// # Parameters
    ///
    /// * `formatter` - Formatter receiving the human-readable expectation.
    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a value representable as JSON")
    }

    /// Accounts and constructs a JSON boolean.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for accounting failures.
    ///
    /// # Parameters
    ///
    /// * `value` - Boolean supplied by the deserializer.
    fn visit_bool<E>(mut self, value: bool) -> Result<Self::Value, E>
    where
        E: Error,
    {
        self.admit(JsonMeasurement::Boolean { depth: self.depth })?;
        Ok(Value::Bool(value))
    }

    /// Accounts and constructs a signed JSON integer.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for accounting failures.
    ///
    /// # Parameters
    ///
    /// * `value` - Signed integer supplied by the deserializer.
    fn visit_i64<E>(mut self, value: i64) -> Result<Self::Value, E>
    where
        E: Error,
    {
        self.enter_number(Number::from(value))
    }

    /// Rejects unsupported wide signed integers or accounts the representable
    /// value.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for conversion failures.
    ///
    /// # Parameters
    ///
    /// * `value` - Wide signed integer supplied by the deserializer.
    #[inline(never)]
    fn visit_i128<E>(mut self, value: i128) -> Result<Self::Value, E>
    where
        E: Error,
    {
        let number = StrictJsonNumber::from_i128(value).map_err(E::custom)?;
        self.enter_number(number)
    }

    /// Accounts and constructs an unsigned JSON integer.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for accounting failures.
    ///
    /// # Parameters
    ///
    /// * `value` - Unsigned integer supplied by the deserializer.
    fn visit_u64<E>(mut self, value: u64) -> Result<Self::Value, E>
    where
        E: Error,
    {
        self.enter_number(Number::from(value))
    }

    /// Rejects unsupported wide unsigned integers or accounts the representable
    /// value.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for conversion failures.
    ///
    /// # Parameters
    ///
    /// * `value` - Wide unsigned integer supplied by the deserializer.
    #[inline(never)]
    fn visit_u128<E>(mut self, value: u128) -> Result<Self::Value, E>
    where
        E: Error,
    {
        let number = StrictJsonNumber::from_u128(value).map_err(E::custom)?;
        self.enter_number(number)
    }

    /// Rejects non-finite floats or accounts the representable JSON number.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for conversion failures.
    ///
    /// # Parameters
    ///
    /// * `value` - Floating-point value supplied by the deserializer.
    fn visit_f64<E>(mut self, value: f64) -> Result<Self::Value, E>
    where
        E: Error,
    {
        let number = Number::from_f64(value)
            .ok_or_else(|| E::custom("non-finite float is not representable as JSON"))?;
        self.enter_number(number)
    }

    /// Accounts and copies a borrowed JSON string.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for accounting failures.
    ///
    /// # Parameters
    ///
    /// * `value` - Borrowed string supplied by the deserializer.
    fn visit_str<E>(mut self, value: &str) -> Result<Self::Value, E>
    where
        E: Error,
    {
        self.admit(JsonMeasurement::String {
            depth: self.depth,
            bytes: value.len(),
        })?;
        Ok(Value::String(value.to_owned()))
    }

    /// Accounts and adopts an owned JSON string.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for accounting failures.
    ///
    /// # Parameters
    ///
    /// * `value` - Owned string supplied by the deserializer.
    fn visit_string<E>(mut self, value: String) -> Result<Self::Value, E>
    where
        E: Error,
    {
        self.admit(JsonMeasurement::String {
            depth: self.depth,
            bytes: value.len(),
        })?;
        Ok(Value::String(value))
    }

    /// Accounts a missing optional value as JSON null.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for accounting failures.
    fn visit_none<E>(mut self) -> Result<Self::Value, E>
    where
        E: Error,
    {
        self.admit(JsonMeasurement::Null { depth: self.depth })?;
        Ok(Value::Null)
    }

    /// Accounts a unit value as JSON null.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Deserialization error type used for accounting failures.
    fn visit_unit<E>(mut self) -> Result<Self::Value, E>
    where
        E: Error,
    {
        self.admit(JsonMeasurement::Null { depth: self.depth })?;
        Ok(Value::Null)
    }

    /// Delegates an optional value to the same JSON visitor.
    ///
    /// # Type Parameters
    ///
    /// * `D` - Deserializer supplying the optional payload.
    ///
    /// # Parameters
    ///
    /// * `deserializer` - Source that presents the optional payload.
    fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(self)
    }

    /// Delegates a newtype payload to the same JSON visitor.
    ///
    /// # Type Parameters
    ///
    /// * `D` - Deserializer supplying the newtype payload.
    ///
    /// # Parameters
    ///
    /// * `deserializer` - Source that presents the wrapped value.
    fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(self)
    }

    /// Accounts and materializes a JSON array from sequence events.
    ///
    /// # Type Parameters
    ///
    /// * `A` - Sequence access implementation supplied by the deserializer.
    ///
    /// # Parameters
    ///
    /// * `sequence` - Accessor yielding the array's elements.
    fn visit_seq<A>(mut self, mut sequence: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        self.transaction
            .try_enter_container(JsonContainerKind::Sequence, self.depth)
            .map_err(A::Error::custom)?;
        let mut values = Vec::new();
        loop {
            let next = values
                .len()
                .checked_add(1)
                .ok_or_else(|| A::Error::custom("JSON sequence item count overflowed usize"))?;
            let Some(value) = sequence
                .next_element_seed(self.prospective_child(JsonContainerKind::Sequence, next))?
            else {
                break;
            };
            values.push(value);
        }
        Ok(Value::Array(values))
    }

    /// Accounts and materializes a JSON object from map events.
    ///
    /// # Type Parameters
    ///
    /// * `A` - Map access implementation supplied by the deserializer.
    ///
    /// # Parameters
    ///
    /// * `map` - Accessor yielding the object's keys and values.
    fn visit_map<A>(mut self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        self.transaction
            .try_enter_container(JsonContainerKind::Map, self.depth)
            .map_err(A::Error::custom)?;
        let mut values = Map::new();
        let mut entries = 0_usize;
        loop {
            let next = entries
                .checked_add(1)
                .ok_or_else(|| A::Error::custom("JSON map entry count overflowed usize"))?;
            let Some(key) = map.next_key_seed(JsonKeySeed {
                transaction: self.transaction,
                prospective: next,
            })?
            else {
                break;
            };
            if self.reject_duplicate_keys && values.contains_key(&key) {
                return Err(A::Error::custom("duplicate JSON object key"));
            }
            let value = map.next_value_seed(self.child())?;
            entries = next;
            values.insert(key, value);
        }
        Ok(Value::Object(values))
    }
}