qubit-json 0.7.0

Lenient JSON decoder for non-fully-trusted JSON text inputs
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// =============================================================================
//    Copyright (c) 2025 - 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Tests for the public API in `lenient_json_decoder.rs`.

use serde_json::json;

use crate::fixtures::{
    ByteBuffer,
    CountedFailure,
    ExactInteger,
    Message,
    SingleValue,
    User,
    deserialize_calls,
    reset_deserialize_calls,
};
use qubit_json::{
    ErrorPrivacyPolicy,
    JsonDecodeErrorKind,
    JsonDecodeOptions,
    JsonDecodeStage,
    JsonTopLevelKind,
    LenientJsonDecoder,
    MarkdownFencePolicy,
};

/// Verifies that new exposes configured options.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_new_exposes_configured_options() {
    let options = JsonDecodeOptions::default()
        .with_markdown_fence_policy(MarkdownFencePolicy::Disabled);
    let decoder = LenientJsonDecoder::new(options.clone());
    assert_eq!(decoder.options(), &options);
}

/// Verifies that default uses default options.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_default_uses_default_options() {
    let decoder = LenientJsonDecoder::default();
    assert_eq!(decoder.options(), &JsonDecodeOptions::default());
}

/// Verifies that strict decoder preserves serde json grammar.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_strict_decoder_preserves_serde_json_grammar() {
    let decoder = LenientJsonDecoder::new(JsonDecodeOptions::strict());

    let canonical: serde_json::Value = decoder
        .decode(" \n{\"ok\":true}\t")
        .expect("strict mode must preserve whitespace accepted by serde_json");
    assert_eq!(canonical, serde_json::json!({"ok": true}));

    for input in [
        "\u{feff}{\"ok\":true}",
        "```json\n{\"ok\":true}\n```",
        "{\"text\":\"line one\nline two\"}",
    ] {
        let error = decoder
            .decode_value(input)
            .expect_err("strict mode must reject lenient-only input forms");
        assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
        assert_eq!(
            error.privacy_policy(),
            qubit_json::ErrorPrivacyPolicy::Redacted,
        );
    }
}

/// Verifies that decode value parses normalized json.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_value_parses_normalized_json() {
    let decoder = LenientJsonDecoder::default();
    let value = decoder
        .decode_value("```json\n{\"name\":\"alice\",\"age\":30}\n```")
        .expect("default decoder should parse JSON wrapped in a Markdown code fence");
    assert_eq!(value, json!({"name": "alice", "age": 30}));
}

/// Verifies that decode typed value succeeds.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_typed_value_succeeds() {
    let decoder = LenientJsonDecoder::default();
    let person: User = decoder
        .decode("{\"name\":\"alice\",\"age\":30}")
        .expect("valid JSON object should deserialize into User");
    assert_eq!(
        person,
        User {
            name: "alice".to_string(),
            age: 30,
        }
    );
}

/// Verifies that decode slice decodes valid utf8 without changing semantics.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_slice_decodes_valid_utf8_without_changing_semantics() {
    let value: serde_json::Value = LenientJsonDecoder::default()
        .decode_slice(b"{\"ok\":true}")
        .expect("valid UTF-8 JSON bytes must decode");
    assert_eq!(value, serde_json::json!({"ok": true}));
}

/// Verifies that decode slice rejects invalid utf8 for byte target.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_slice_rejects_invalid_utf8_for_byte_target() {
    let error = LenientJsonDecoder::new(JsonDecodeOptions::strict())
        .decode_slice::<ByteBuffer>(b"\"\xff\"")
        .expect_err(
            "invalid UTF-8 must be rejected before byte deserialization",
        );
    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidUtf8);
}

/// Verifies that decode slice invokes target deserializer once on failure.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_slice_invokes_target_deserializer_once_on_failure() {
    reset_deserialize_calls();
    let error = LenientJsonDecoder::new(JsonDecodeOptions::strict())
        .decode_slice::<CountedFailure>(br#""value""#)
        .expect_err("the counted target intentionally rejects valid JSON");
    assert_eq!(error.kind(), JsonDecodeErrorKind::Deserialize);
    assert_eq!(deserialize_calls(), 1);
}

/// Verifies that decode slice accepts non rewrite strict overrides.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_slice_accepts_non_rewrite_strict_overrides() {
    let decoder = LenientJsonDecoder::new(
        JsonDecodeOptions::strict()
            .with_max_input_bytes(Some(64))
            .with_error_privacy_policy(ErrorPrivacyPolicy::Detailed),
    );
    let value: serde_json::Value = decoder
        .decode_slice(b"{\"ok\":true}")
        .expect("non-rewrite options must preserve successful byte decoding");
    assert_eq!(value, serde_json::json!({"ok": true}));
}

/// Verifies that decode slice preserves deserialize error mapping.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_slice_preserves_deserialize_error_mapping() {
    let decoder = LenientJsonDecoder::new(
        JsonDecodeOptions::strict()
            .with_error_privacy_policy(ErrorPrivacyPolicy::Detailed),
    );
    let error = decoder.decode_slice::<Message>(b"{\"text\":7}").expect_err(
        "valid JSON with the wrong field type must fail deserialization",
    );
    assert_eq!(error.kind(), JsonDecodeErrorKind::Deserialize);
    assert_eq!(error.privacy_policy(), ErrorPrivacyPolicy::Detailed);
    assert!(std::error::Error::source(&error).is_some());
}

/// Verifies that decode slice preserves invalid json mapping.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_slice_preserves_invalid_json_mapping() {
    let decoder = LenientJsonDecoder::new(JsonDecodeOptions::strict());
    let error = decoder
        .decode_slice::<Message>(b"{\"text\":\"broken\"")
        .expect_err("malformed typed JSON must remain an invalid JSON error");
    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
}

/// Verifies that decode slice checks raw size before utf8.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_slice_checks_raw_size_before_utf8() {
    let decoder = LenientJsonDecoder::new(
        JsonDecodeOptions::strict().with_max_input_bytes(Some(1)),
    );
    let error = decoder
        .decode_slice::<serde_json::Value>(&[0xff, 0xfe])
        .expect_err("raw size must be checked before UTF-8");
    assert_eq!(error.kind(), JsonDecodeErrorKind::InputTooLarge);
}

/// Verifies that decode slice accepts input at exact raw size limit.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_slice_accepts_input_at_exact_raw_size_limit() {
    let input = b"null";
    let decoder = LenientJsonDecoder::new(
        JsonDecodeOptions::strict().with_max_input_bytes(Some(input.len())),
    );

    let value = decoder
        .decode_slice::<serde_json::Value>(input)
        .expect("input at the exact raw byte limit must be accepted");

    assert_eq!(value, serde_json::Value::Null);
}

/// Verifies that decode slice classifies invalid utf8.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_slice_classifies_invalid_utf8() {
    let error = LenientJsonDecoder::default()
        .decode_slice::<serde_json::Value>(&[0xff])
        .expect_err("invalid UTF-8 must fail before normalization");
    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidUtf8);
    assert_eq!(error.stage(), JsonDecodeStage::DecodeText);
    assert_eq!(error.raw_input_bytes(), 1);
    assert_eq!(error.normalized_input_bytes(), None);
}

/// Verifies that decode reports empty input from normalizer.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_reports_empty_input_from_normalizer() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode::<User>("")
        .expect_err("empty input should fail during normalization");
    assert_eq!(error.kind(), JsonDecodeErrorKind::EmptyInput);
}

/// Verifies that decode typed value applies normalization pipeline.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_typed_value_applies_normalization_pipeline() {
    let decoder = LenientJsonDecoder::default();
    let message: Message = decoder
        .decode("```json\n{\"text\":\"a\nb\"}\n```")
        .expect("typed decode should still normalize fenced JSON and repair string control chars");
    assert_eq!(
        message,
        Message {
            text: "a\nb".to_string(),
        }
    );
}

/// Verifies that decode object requires object top level.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_requires_object_top_level() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode_object::<User>("[{\"name\":\"alice\",\"age\":30}]")
        .expect_err("top-level array should be rejected by decode_object");
    assert_eq!(error.kind(), JsonDecodeErrorKind::UnexpectedTopLevel);
    assert_eq!(error.stage(), JsonDecodeStage::TopLevelCheck);
    assert_eq!(error.expected_top_level(), Some(JsonTopLevelKind::Object));
    assert_eq!(error.actual_top_level(), Some(JsonTopLevelKind::Array));
}

/// Verifies that decode object reports empty input from normalizer.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_reports_empty_input_from_normalizer() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode_object::<User>("")
        .expect_err("empty input should fail during normalization");
    assert_eq!(error.kind(), JsonDecodeErrorKind::EmptyInput);
}

/// Verifies that decode object reports invalid json for malformed array.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_reports_invalid_json_for_malformed_array() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder.decode_object::<User>("[").expect_err(
        "malformed JSON should be reported before top-level checking",
    );
    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
}

/// Verifies that decode object reports invalid json for malformed scalar.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_reports_invalid_json_for_malformed_scalar() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder.decode_object::<User>("\"unterminated").expect_err(
        "malformed scalar JSON should not be treated as a top-level mismatch",
    );
    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
}

/// Verifies that decode array requires array top level.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_array_requires_array_top_level() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode_array::<User>("{\"name\":\"alice\",\"age\":30}")
        .expect_err("top-level object should be rejected by decode_array");
    assert_eq!(error.kind(), JsonDecodeErrorKind::UnexpectedTopLevel);
    assert_eq!(error.expected_top_level(), Some(JsonTopLevelKind::Array));
    assert_eq!(error.actual_top_level(), Some(JsonTopLevelKind::Object));
}

/// Verifies that decode array reports empty input from normalizer.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_array_reports_empty_input_from_normalizer() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode_array::<User>("")
        .expect_err("empty input should fail during normalization");
    assert_eq!(error.kind(), JsonDecodeErrorKind::EmptyInput);
}

/// Verifies that decode array reports invalid json for malformed object.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_array_reports_invalid_json_for_malformed_object() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder.decode_array::<User>("{").expect_err(
        "malformed JSON should be reported before top-level checking",
    );
    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
}

/// Verifies that decode object rejects scalar top level.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_rejects_scalar_top_level() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode_object::<User>("42")
        .expect_err("top-level scalar should be rejected by decode_object");
    assert_eq!(error.kind(), JsonDecodeErrorKind::UnexpectedTopLevel);
    assert_eq!(error.expected_top_level(), Some(JsonTopLevelKind::Object));
    assert_eq!(error.actual_top_level(), Some(JsonTopLevelKind::Other));
}

/// Verifies that decode array rejects scalar top level.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_array_rejects_scalar_top_level() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode_array::<User>("42")
        .expect_err("top-level scalar should be rejected by decode_array");
    assert_eq!(error.kind(), JsonDecodeErrorKind::UnexpectedTopLevel);
    assert_eq!(error.expected_top_level(), Some(JsonTopLevelKind::Array));
    assert_eq!(error.actual_top_level(), Some(JsonTopLevelKind::Other));
}

/// Verifies that decode array succeeds.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_array_succeeds() {
    let decoder = LenientJsonDecoder::default();
    let people = decoder
        .decode_array::<User>("[{\"name\":\"alice\",\"age\":30}]")
        .expect("top-level array should deserialize into Vec<User>");
    assert_eq!(
        people,
        vec![User {
            name: "alice".to_string(),
            age: 30,
        }]
    );
}

/// Verifies that decode object reports deserialize error after top level check.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_reports_deserialize_error_after_top_level_check() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode_object::<User>("{\"name\":\"alice\",\"age\":\"old\"}")
        .expect_err(
            "valid object with wrong field type should return Deserialize",
        );
    assert_eq!(error.kind(), JsonDecodeErrorKind::Deserialize);
}

/// Verifies that decode array reports deserialize error after top level check.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_array_reports_deserialize_error_after_top_level_check() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode_array::<User>("[{\"name\":\"alice\",\"age\":\"old\"}]")
        .expect_err(
            "valid array with wrong element type should return Deserialize",
        );
    assert_eq!(error.kind(), JsonDecodeErrorKind::Deserialize);
}

/// Verifies that decode allows generic scalar targets.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_allows_generic_scalar_targets() {
    let decoder = LenientJsonDecoder::default();
    let value: i64 = decoder
        .decode("42")
        .expect("scalar JSON should deserialize into i64");
    assert_eq!(value, 42);
}

/// Verifies that decode reports invalid json.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_reports_invalid_json() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode::<User>("{")
        .expect_err("broken JSON should return InvalidJson");
    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
}

/// Verifies that decode reports deserialize error.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_reports_deserialize_error() {
    let decoder = LenientJsonDecoder::default();
    let error = decoder
        .decode::<User>("{\"name\":\"alice\",\"age\":\"old\"}")
        .expect_err("JSON with a wrong field type should return Deserialize");
    assert_eq!(error.kind(), JsonDecodeErrorKind::Deserialize);
}

/// Verifies that decode reports invalid json when data error precedes syntax
/// error.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_reports_invalid_json_when_data_error_precedes_syntax_error() {
    let error = LenientJsonDecoder::default()
        .decode::<SingleValue>("{\"value\":\"wrong\",")
        .expect_err(
            "incomplete JSON must take precedence over a field type error",
        );

    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
    assert_eq!(error.stage(), JsonDecodeStage::Parse);
}

/// Verifies that decode object reports invalid json when data error precedes
/// syntax error.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_reports_invalid_json_when_data_error_precedes_syntax_error()
 {
    let error = LenientJsonDecoder::default()
        .decode_object::<SingleValue>("{\"value\":\"wrong\",")
        .expect_err("incomplete object JSON must take precedence over a field type error");

    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
    assert_eq!(error.stage(), JsonDecodeStage::Parse);
}

/// Verifies that decode array reports invalid json when data error precedes
/// syntax error.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_array_reports_invalid_json_when_data_error_precedes_syntax_error()
 {
    let error = LenientJsonDecoder::default()
        .decode_array::<u8>("[\"wrong\",")
        .expect_err("incomplete array JSON must take precedence over an element type error");

    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
    assert_eq!(error.stage(), JsonDecodeStage::Parse);
}

/// Verifies that decode object reports invalid json for non token start.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_reports_invalid_json_for_non_token_start() {
    let decoder = LenientJsonDecoder::new(
        JsonDecodeOptions::default()
            .with_trim_whitespace(false)
            .with_markdown_fence_policy(MarkdownFencePolicy::Disabled),
    );
    let error = decoder
        .decode_object::<User>(" \n\t ")
        .expect_err("invalid syntax should still be mapped as InvalidJson");
    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
}

/// Verifies that decoder reuses configuration between calls.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decoder_reuses_configuration_between_calls() {
    let decoder = LenientJsonDecoder::new(
        JsonDecodeOptions::default()
            .with_markdown_fence_policy(MarkdownFencePolicy::Disabled),
    );

    let first = decoder
        .decode_value("```json\n{\"a\":1}\n```")
        .expect_err("disabled fence stripping must reject the first input");
    assert_eq!(first.kind(), JsonDecodeErrorKind::InvalidJson);

    let second = decoder
        .decode_value("```json\n{\"a\":2}\n```")
        .expect_err("disabled fence stripping must reject the second input");
    assert_eq!(second.kind(), JsonDecodeErrorKind::InvalidJson);
}

/// Verifies that decoders with different configs do not share state.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decoders_with_different_configs_do_not_share_state() {
    let strict_decoder = LenientJsonDecoder::new(
        JsonDecodeOptions::default()
            .with_markdown_fence_policy(MarkdownFencePolicy::Disabled),
    );
    let permissive_decoder = LenientJsonDecoder::default();

    assert_eq!(
        strict_decoder
            .decode_value("```json\n{\"a\":1}\n```")
            .expect_err("code fence should stay when stripping is disabled")
            .kind(),
        JsonDecodeErrorKind::InvalidJson
    );
    let value = permissive_decoder
        .decode_value("```json\n{\"a\":1}\n```")
        .expect("default normalizer should strip one markdown fence");
    assert_eq!(value, serde_json::json!({"a": 1}));
}

/// Verifies that decoder keeps trim whitespace setting for empty text.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decoder_keeps_trim_whitespace_setting_for_empty_text() {
    let decoder = LenientJsonDecoder::new(
        JsonDecodeOptions::default().with_trim_whitespace(false),
    );
    let error = decoder
        .decode_value(" \n\t")
        .expect_err("trim disabled should leave whitespace for parser");
    assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
}

/// Verifies that decode object preserves u128 without value round trip.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_preserves_u128_without_value_round_trip() {
    let decoded: ExactInteger = LenientJsonDecoder::default()
        .decode_object(r#"{"value":340282366920938463463374607431768211455}"#)
        .expect("direct object decoding should preserve u128::MAX");

    assert_eq!(decoded.value, u128::MAX);
}

/// Verifies that decode array preserves u128 without value round trip.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_array_preserves_u128_without_value_round_trip() {
    let decoded: Vec<ExactInteger> = LenientJsonDecoder::default()
        .decode_array(r#"[{"value":340282366920938463463374607431768211455}]"#)
        .expect("direct array decoding should preserve u128::MAX");

    assert_eq!(decoded, vec![ExactInteger { value: u128::MAX }]);
}

/// Verifies that decode object preserves duplicate field rejection.
///
/// # Panics
///
/// Panics when the expected behavior is not observed.
#[test]
fn test_decode_object_preserves_duplicate_field_rejection() {
    let error = LenientJsonDecoder::default()
        .decode_object::<SingleValue>(r#"{"value":1,"value":2}"#)
        .expect_err("direct object decoding should reject duplicate fields");

    assert_eq!(error.kind(), JsonDecodeErrorKind::Deserialize);
}