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
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Verifies the unified JSON decoding error model.

use std::str::FromStr;

use qubit_budget::json::JsonDecodeLimits;
use qubit_budget::json::JsonResource;
use qubit_json::decode::DiagnosticPolicy;
use qubit_json::decode::JsonDecodeErrorKind;
use qubit_json::decode::JsonDecodeErrorSource;
use qubit_json::decode::JsonDecodeStage;
use qubit_json::decode::JsonDecoder;
use qubit_json::decode::JsonMaterializationErrorReason;
use qubit_json::decode::JsonRootKind;
use qubit_json::decode::JsonSyntaxErrorReason;
use qubit_json::decode::MarkdownFencePolicy;
use qubit_json::decode::NormalizingJsonDecodePolicy;
use qubit_json::decode::NormalizingJsonDecoder;

/// Creates a normalization policy that leaves the JSON text unchanged.
fn no_normalization_policy() -> NormalizingJsonDecodePolicy {
    NormalizingJsonDecodePolicy::builder()
        .trim_whitespace(false)
        .strip_utf8_bom(false)
        .markdown_fence_policy(MarkdownFencePolicy::Disabled)
        .escape_control_chars_in_strings(false)
        .build()
}

/// Verifies strict syntax failures use the unified parse diagnostics.
#[test]
fn test_json_decode_error_reports_strict_parse_failure() {
    let mut decoder = JsonDecoder::unlimited();
    let error = decoder
        .validate_str("{")
        .expect_err("an incomplete object must fail validation");

    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
    assert_eq!(error.stage(), JsonDecodeStage::Parse);
    assert_eq!(error.raw_input_bytes(), 1);
    assert!(error.syntax_error().is_some());
    assert!(error.budget_error().is_none());
}

/// Verifies default diagnostics classify an unexpected byte without retaining
/// that byte in their structured or formatted public representation.
#[test]
fn test_redacted_syntax_diagnostics_do_not_reveal_unexpected_input_bytes() {
    let mut decoder = JsonDecoder::unlimited();
    let error = decoder
        .validate_utf8(b"@")
        .expect_err("an unexpected byte must fail validation");
    let syntax = error.syntax_error().expect("expected a syntax error");

    assert_eq!(error.diagnostic_policy(), DiagnosticPolicy::Redacted);
    assert_eq!(syntax.reason(), JsonSyntaxErrorReason::UnexpectedByte);
    assert_eq!(
        syntax.to_string(),
        "unexpected byte at line 1 column 1 (byte offset 0)"
    );
    assert!(!format!("{error:?}").contains("0x40"));
    assert!(std::error::Error::source(&error).is_none());
}

#[test]
fn test_redacted_deserialize_without_coordinates_omits_location() {
    let mut decoder = JsonDecoder::unlimited();
    let error = decoder
        .decode_unique_str::<bool>("1")
        .expect_err("type mismatch");
    assert_eq!(error.kind(), JsonDecodeErrorKind::Deserialize);
    assert_eq!(error.line(), None);
    assert_eq!(error.column(), None);
    assert_eq!(error.to_string(), "Failed to deserialize JSON value");
    assert!(std::error::Error::source(&error).is_none());
}

#[test]
fn test_deserialize_without_coordinates_preserves_diagnostic_policy() {
    for policy in [DiagnosticPolicy::Redacted, DiagnosticPolicy::Detailed] {
        let mut decoder = JsonDecoder::unlimited().with_diagnostic_policy(policy);
        let error = decoder
            .decode_unique_str::<bool>(r#""RJ_SECRET""#)
            .expect_err("type mismatch");
        assert_eq!(error.line(), None);
        assert_eq!(error.column(), None);
        let message = error.to_string();
        assert!(!message.contains("line 0"));
        assert!(!message.contains("column 0"));
        match policy {
            DiagnosticPolicy::Redacted => {
                assert!(!message.contains("RJ_SECRET"));
                assert!(!format!("{error:?}").contains("RJ_SECRET"));
                assert!(std::error::Error::source(&error).is_none());
            }
            DiagnosticPolicy::Detailed => {
                assert!(std::error::Error::source(&error).is_some());
            }
        }
    }
}

#[test]
fn test_deserialize_with_coordinates_retains_location() {
    let mut strict = JsonDecoder::unlimited();
    let error = strict.decode_str::<bool>("1").expect_err("type mismatch");
    let line = error.line().expect("known line");
    let column = error.column().expect("known column");
    assert!(
        error
            .to_string()
            .ends_with(&format!("at line {line} column {column}"))
    );

    let mut normalizing = NormalizingJsonDecoder::with_limits(
        no_normalization_policy(),
        JsonDecodeLimits::<JsonResource, usize>::default(),
    );
    let error = normalizing
        .decode_str::<bool>("1")
        .expect_err("type mismatch");
    let line = error.line().expect("known normalized line");
    let column = error.column().expect("known normalized column");
    assert!(
        error
            .to_string()
            .ends_with(&format!("at normalized line {line} column {column}"))
    );
}

/// Verifies normalizing failures use the same public kind and stage types.
#[test]
fn test_json_decode_error_reports_normalization_failure() {
    let mut decoder = NormalizingJsonDecoder::with_limits(
        NormalizingJsonDecodePolicy::lenient(),
        JsonDecodeLimits::<JsonResource, usize>::default(),
    );
    let error = decoder
        .decode_value("")
        .expect_err("empty normalized input must fail");

    assert_eq!(error.kind(), JsonDecodeErrorKind::EmptyInput);
    assert_eq!(error.stage(), JsonDecodeStage::Normalize);
    assert_eq!(error.raw_input_bytes(), 0);
}

/// Verifies equivalent facade failures expose identical semantic kinds and
/// stages even though one facade has an explicit normalization phase.
#[test]
fn test_decoder_facades_share_error_classification() {
    let mut strict = JsonDecoder::unlimited();
    let mut normalizing = NormalizingJsonDecoder::with_limits(
        no_normalization_policy(),
        JsonDecodeLimits::<JsonResource, usize>::default(),
    );

    let strict_syntax = strict
        .decode_str::<serde_json::Value>("{")
        .expect_err("syntax error");
    let normalizing_syntax = normalizing
        .decode_str::<serde_json::Value>("{")
        .expect_err("syntax error");
    assert_eq!(strict_syntax.kind(), normalizing_syntax.kind());
    assert_eq!(strict_syntax.stage(), normalizing_syntax.stage());

    let strict_target = strict.decode_str::<bool>("1").expect_err("target error");
    let normalizing_target = normalizing
        .decode_str::<bool>("1")
        .expect_err("target error");
    assert_eq!(strict_target.kind(), normalizing_target.kind());
    assert_eq!(strict_target.stage(), normalizing_target.stage());

    let limits = JsonDecodeLimits::builder().max_input_bytes(1_usize).build();
    let mut strict = JsonDecoder::with_limits(limits);
    let mut normalizing = NormalizingJsonDecoder::with_limits(no_normalization_policy(), limits);
    let strict_budget = strict.decode_str::<bool>("true").expect_err("input budget");
    let normalizing_budget = normalizing
        .decode_str::<bool>("true")
        .expect_err("input budget");
    assert_eq!(strict_budget.kind(), normalizing_budget.kind());
    assert_eq!(strict_budget.stage(), normalizing_budget.stage());
}

/// Verifies every public error kind and stage has a stable textual round trip.
#[test]
fn test_json_decode_error_kind_and_stage_round_trip() {
    let kinds = [
        (JsonDecodeErrorKind::Budget, "budget"),
        (JsonDecodeErrorKind::EmptyInput, "empty_input"),
        (JsonDecodeErrorKind::InvalidUtf8, "invalid_utf8"),
        (JsonDecodeErrorKind::InvalidJson, "invalid_json"),
        (
            JsonDecodeErrorKind::UnexpectedTopLevel,
            "unexpected_top_level",
        ),
        (JsonDecodeErrorKind::Deserialize, "deserialize"),
    ];
    for (kind, text) in kinds {
        assert_eq!(kind.to_string(), text);
        assert_eq!(JsonDecodeErrorKind::from_str(text), Ok(kind));
    }
    assert!(JsonDecodeErrorKind::from_str("unknown").is_err());

    let stages = [
        (JsonDecodeStage::Input, "input"),
        (JsonDecodeStage::DecodeText, "decode_text"),
        (JsonDecodeStage::Normalize, "normalize"),
        (JsonDecodeStage::Admission, "admission"),
        (JsonDecodeStage::Parse, "parse"),
        (JsonDecodeStage::TopLevelCheck, "top_level_check"),
        (JsonDecodeStage::Deserialize, "deserialize"),
    ];
    for (stage, text) in stages {
        assert_eq!(stage.to_string(), text);
        assert_eq!(JsonDecodeStage::from_str(text), Ok(stage));
    }
    assert!(JsonDecodeStage::from_str("unknown").is_err());
}

/// Verifies representative failures cover semantic categories not exercised
/// by the syntax and normalization-specific tests above.
#[test]
fn test_json_decode_error_model_representative_matrix() {
    let mut invalid_utf8 = JsonDecoder::unlimited();
    let error = invalid_utf8
        .validate_utf8(&[0xff])
        .expect_err("invalid UTF-8");
    assert_eq!(
        (error.kind(), error.stage()),
        (
            JsonDecodeErrorKind::InvalidUtf8,
            JsonDecodeStage::DecodeText
        ),
    );
    assert_eq!(error.utf8_valid_up_to(), Some(0));

    let mut decoder = NormalizingJsonDecoder::with_limits(
        NormalizingJsonDecodePolicy::lenient(),
        JsonDecodeLimits::<JsonResource, usize>::default(),
    );
    let document = decoder.prepare_str("null").expect("prepare scalar");
    let error = decoder
        .decode_precharged_object_document::<serde_json::Value>(&document)
        .expect_err("object contract");
    assert_eq!(
        (error.kind(), error.stage()),
        (
            JsonDecodeErrorKind::UnexpectedTopLevel,
            JsonDecodeStage::TopLevelCheck,
        ),
    );

    let error = decoder
        .decode_precharged_document::<bool>(&document)
        .expect_err("target mismatch");
    assert_eq!(
        (error.kind(), error.stage()),
        (
            JsonDecodeErrorKind::Deserialize,
            JsonDecodeStage::Deserialize,
        ),
    );
}

/// Verifies consuming a decoding error exposes every owned semantic source
/// without a kind-plus-extractor assertion pair.
#[test]
fn test_json_decode_error_into_source_exposes_owned_variants() {
    let limits = JsonDecodeLimits::builder().max_input_bytes(1_usize).build();
    let mut decoder = JsonDecoder::with_limits(limits);
    match decoder
        .validate_str("null")
        .expect_err("input budget")
        .into_source()
    {
        JsonDecodeErrorSource::Budget {
            stage,
            raw_input_bytes,
            source,
            ..
        } => {
            assert_eq!(stage, JsonDecodeStage::Input);
            assert_eq!(raw_input_bytes, 4);
            assert_eq!(source.resource(), &JsonResource::InputBytes);
        }
        source => panic!("expected budget source, got {source:?}"),
    }

    let mut normalizing = NormalizingJsonDecoder::with_limits(
        NormalizingJsonDecodePolicy::lenient(),
        JsonDecodeLimits::<JsonResource, usize>::default(),
    );
    match normalizing
        .decode_value("")
        .expect_err("empty input")
        .into_source()
    {
        JsonDecodeErrorSource::EmptyInput {
            stage,
            raw_input_bytes,
            ..
        } => {
            assert_eq!(stage, JsonDecodeStage::Normalize);
            assert_eq!(raw_input_bytes, 0);
        }
        source => panic!("expected empty-input source, got {source:?}"),
    }

    let mut decoder = JsonDecoder::unlimited().with_diagnostic_policy(DiagnosticPolicy::Detailed);
    match decoder
        .validate_utf8(&[0xff])
        .expect_err("invalid UTF-8")
        .into_source()
    {
        JsonDecodeErrorSource::InvalidUtf8 {
            valid_up_to,
            error_len,
            source,
            ..
        } => {
            assert_eq!(valid_up_to, 0);
            assert_eq!(error_len, Some(1));
            assert!(source.is_some());
        }
        source => panic!("expected invalid-UTF-8 source, got {source:?}"),
    }

    match decoder
        .validate_str("{")
        .expect_err("invalid JSON")
        .into_source()
    {
        JsonDecodeErrorSource::InvalidJson { syntax, source, .. } => {
            assert_eq!(syntax.offset(), 1);
            assert!(source.is_some());
        }
        source => panic!("expected invalid-JSON source, got {source:?}"),
    }

    match decoder
        .decode_object_str::<serde_json::Value>("null")
        .expect_err("object contract")
        .into_source()
    {
        JsonDecodeErrorSource::UnexpectedTopLevel {
            expected, actual, ..
        } => {
            assert_eq!(expected, JsonRootKind::Object);
            assert_eq!(actual, JsonRootKind::Other);
        }
        source => panic!("expected top-level source, got {source:?}"),
    }

    match decoder
        .decode_str::<bool>("1")
        .expect_err("target mismatch")
        .into_source()
    {
        JsonDecodeErrorSource::Deserialize {
            line,
            column,
            source,
            ..
        } => {
            assert_eq!(line, 1);
            assert_eq!(column, 1);
            assert!(source.is_some());
        }
        source => panic!("expected deserialization source, got {source:?}"),
    }
}

/// Verifies unique-key construction failures expose safe structured reasons.
#[test]
fn test_unique_decode_materialization_reason_matrix() {
    let mut decoder = JsonDecoder::unlimited();
    let error = decoder
        .decode_unique_str::<serde_json::Value>(r#"{"RJ_SECRET":1,"RJ_SECRET":2}"#)
        .expect_err("duplicate decoded keys must fail construction");
    assert_eq!(error.kind(), JsonDecodeErrorKind::Deserialize);
    assert_eq!(error.stage(), JsonDecodeStage::Deserialize);
    assert_eq!(
        error.materialization_error_reason(),
        Some(JsonMaterializationErrorReason::DuplicateKey)
    );
    assert_eq!(
        error.clone().materialization_error_reason(),
        error.materialization_error_reason()
    );
    assert!(!error.to_string().contains("RJ_SECRET"));
    assert!(!format!("{error:?}").contains("RJ_SECRET"));
    assert!(std::error::Error::source(&error).is_none());
    let reason = error.materialization_error_reason();
    assert!(matches!(
        error.into_source(),
        JsonDecodeErrorSource::Deserialize { .. }
    ));
    assert_eq!(reason, Some(JsonMaterializationErrorReason::DuplicateKey));

    let mut detailed = JsonDecoder::unlimited().with_diagnostic_policy(DiagnosticPolicy::Detailed);
    let error = detailed
        .decode_unique_str::<serde_json::Value>(r#"{"x":1,"x":2}"#)
        .expect_err("detailed duplicate construction must fail");
    assert_eq!(
        error.materialization_error_reason(),
        Some(JsonMaterializationErrorReason::DuplicateKey)
    );
    assert!(std::error::Error::source(&error).is_some());

    let legacy_depth_reason = JsonMaterializationErrorReason::DepthLimit { limit: 127 };
    assert_eq!(
        format!("{legacy_depth_reason:?}"),
        "DepthLimit { limit: 127 }"
    );

    let mut decoder = JsonDecoder::unlimited();
    let target_error = decoder
        .decode_unique_str::<bool>("1")
        .expect_err("target type mismatch");
    assert_eq!(target_error.materialization_error_reason(), None);
    let syntax_error = decoder
        .decode_unique_str::<serde_json::Value>("{")
        .expect_err("invalid syntax");
    assert_eq!(syntax_error.materialization_error_reason(), None);
    let ordinary_error = decoder
        .decode_str::<serde_json::Value>("{")
        .expect_err("ordinary syntax error");
    assert_eq!(ordinary_error.materialization_error_reason(), None);
    let utf8_error = decoder
        .decode_unique_utf8::<serde_json::Value>(&[0xff])
        .expect_err("invalid UTF-8");
    assert_eq!(utf8_error.materialization_error_reason(), None);

    let limits = JsonDecodeLimits::builder().max_depth(2_usize).build();
    let mut decoder = JsonDecoder::with_limits(limits);
    let budget_error = decoder
        .decode_unique_str::<serde_json::Value>("[[[0]]]")
        .expect_err("configured depth budget is checked before materialization");
    assert_eq!(budget_error.kind(), JsonDecodeErrorKind::Budget);
    assert_eq!(budget_error.materialization_error_reason(), None);

    let input_limits = JsonDecodeLimits::builder().max_input_bytes(0_usize).build();
    let mut decoder = JsonDecoder::with_limits(input_limits);
    let input_error = decoder
        .decode_unique_str::<serde_json::Value>("null")
        .expect_err("input budget must reject the document");
    assert_eq!(input_error.materialization_error_reason(), None);
}