jsonx 0.1.0

A serde-enabled implementation of the JSONX extended-JSON format: typed value constructors, unquoted keys, and trailing commas.
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
use std::collections::BTreeMap;
use std::net::{IpAddr, SocketAddr};

use jsonx::{Bytes, DateTime, Datetime, Int, Ip, IpPort, Map, Uint, Value};
use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Basic JSON decoding
// ---------------------------------------------------------------------------

#[test]
fn decode_primitives() {
    assert_eq!(jsonx::from_str::<Value>("null").unwrap(), Value::Null);
    assert_eq!(jsonx::from_str::<Value>("true").unwrap(), Value::Bool(true));
    assert_eq!(jsonx::from_str::<Value>("false").unwrap(), Value::Bool(false));
    // Bare numbers are always f64.
    assert_eq!(jsonx::from_str::<Value>("5").unwrap(), Value::Number(5.0));
    assert_eq!(jsonx::from_str::<Value>("-5").unwrap(), Value::Number(-5.0));
    assert_eq!(jsonx::from_str::<Value>("5.5").unwrap(), Value::Number(5.5));
    assert_eq!(jsonx::from_str::<Value>("1e-3").unwrap(), Value::Number(0.001));
    assert_eq!(
        jsonx::from_str::<Value>(r#""hello""#).unwrap(),
        Value::String("hello".into())
    );
}

#[test]
fn decode_whitespace_around_value() {
    assert_eq!(jsonx::from_str::<Value>("\n true ").unwrap(), Value::Bool(true));
    assert_eq!(jsonx::from_str::<f64>("\t -5 \n").unwrap(), -5.0);
}

#[test]
fn decode_nested_json() {
    let v: Value = jsonx::from_str(r#"{"X": [1], "Y": 4}"#).unwrap();
    let mut expected = Map::new();
    expected.insert("X".into(), Value::Array(vec![Value::Number(1.0)]));
    expected.insert("Y".into(), Value::Number(4.0));
    assert_eq!(v, Value::Object(expected));
}

// ---------------------------------------------------------------------------
// Relaxed syntax: unquoted keys, trailing commas
// ---------------------------------------------------------------------------

#[test]
fn unquoted_keys_and_trailing_commas() {
    let v: Value = jsonx::from_str("{ __: true, _a_b : false, x9: 1, }").unwrap();
    let obj = v.as_object().unwrap();
    assert_eq!(obj.get("__"), Some(&Value::Bool(true)));
    assert_eq!(obj.get("_a_b"), Some(&Value::Bool(false)));
    assert_eq!(obj.get("x9"), Some(&Value::Number(1.0)));

    let arr: Value = jsonx::from_str(r#"["test", int64(-123),]"#).unwrap();
    assert_eq!(
        arr,
        Value::Array(vec![Value::String("test".into()), Value::Int64(-123)])
    );
}

// ---------------------------------------------------------------------------
// String escapes & unicode
// ---------------------------------------------------------------------------

#[test]
fn string_escapes() {
    assert_eq!(jsonx::from_str::<String>(r#""aሴ""#).unwrap(), "a\u{1234}");
    assert_eq!(jsonx::from_str::<String>(r#""http:\/\/""#).unwrap(), "http://");
    assert_eq!(
        jsonx::from_str::<String>(r#""tab\tnewline\n""#).unwrap(),
        "tab\tnewline\n"
    );
    // Surrogate pair for U+1D11E (musical G-clef).
    assert_eq!(
        jsonx::from_str::<String>(r#""g-clef: 𝄞""#).unwrap(),
        "g-clef: \u{1D11E}"
    );
}

#[test]
fn rejects_lone_surrogate() {
    assert!(jsonx::from_str::<String>(r#""\uD834""#).is_err());
    assert!(jsonx::from_str::<String>(r#""\uDD1E""#).is_err());
}

#[test]
fn borrows_strings_when_possible() {
    // Without escapes the deserializer can borrow straight from the input.
    let s: &str = jsonx::from_str(r#""borrowed""#).unwrap();
    assert_eq!(s, "borrowed");
}

// ---------------------------------------------------------------------------
// Extended typed integers
// ---------------------------------------------------------------------------

#[test]
fn typed_integers_to_value() {
    assert_eq!(jsonx::from_str::<Value>("int(64)").unwrap(), Value::Int(64));
    assert_eq!(jsonx::from_str::<Value>("uint(64)").unwrap(), Value::Uint(64));
    assert_eq!(jsonx::from_str::<Value>("int8(-128)").unwrap(), Value::Int8(-128));
    assert_eq!(jsonx::from_str::<Value>("uint8(255)").unwrap(), Value::Uint8(255));
    assert_eq!(jsonx::from_str::<Value>("int16(-4567)").unwrap(), Value::Int16(-4567));
    assert_eq!(jsonx::from_str::<Value>("int32(5364564)").unwrap(), Value::Int32(5364564));
    assert_eq!(
        jsonx::from_str::<Value>(r#"int64("9223372036854775807")"#).unwrap(),
        Value::Int64(i64::MAX)
    );
    assert_eq!(
        jsonx::from_str::<Value>(r#"uint64("18446744073709551615")"#).unwrap(),
        Value::Uint64(u64::MAX)
    );
}

#[test]
fn typed_integers_into_rust_ints() {
    // A typed constructor, a bare number, and a differently-typed constructor
    // all coerce into a Rust integer field when in range.
    assert_eq!(jsonx::from_str::<i32>("int32(5)").unwrap(), 5);
    assert_eq!(jsonx::from_str::<i32>("5").unwrap(), 5);
    assert_eq!(jsonx::from_str::<i32>("int(5)").unwrap(), 5);
    assert_eq!(jsonx::from_str::<u8>("uint8(200)").unwrap(), 200);
    assert_eq!(jsonx::from_str::<i64>(r#"int64("123")"#).unwrap(), 123);
}

#[test]
fn integer_range_errors() {
    assert!(jsonx::from_str::<Value>("int8(-500)").is_err());
    assert!(jsonx::from_str::<Value>("uint8(256)").is_err());
    assert!(jsonx::from_str::<i8>("int32(1000)").is_err());
}

// ---------------------------------------------------------------------------
// Extended types: datetime / ip / ipport / bytes
// ---------------------------------------------------------------------------

#[test]
fn extended_types_to_value() {
    assert_eq!(
        jsonx::from_str::<Value>(r#"datetime("2017-01-01T12:00:00Z")"#).unwrap(),
        Value::DateTime(DateTime::parse_from_rfc3339("2017-01-01T12:00:00Z").unwrap())
    );
    assert_eq!(
        jsonx::from_str::<Value>(r#"ip("192.168.100.19")"#).unwrap(),
        Value::Ip("192.168.100.19".parse().unwrap())
    );
    assert_eq!(
        jsonx::from_str::<Value>(r#"ip("fd00::abc:1")"#).unwrap(),
        Value::Ip("fd00::abc:1".parse().unwrap())
    );
    assert_eq!(
        jsonx::from_str::<Value>(r#"ipport("192.168.1.2:65000")"#).unwrap(),
        Value::IpPort("192.168.1.2:65000".parse().unwrap())
    );
    assert_eq!(
        jsonx::from_str::<Value>(r#"ipport("[fd00::abc:1]:65000")"#).unwrap(),
        Value::IpPort("[fd00::abc:1]:65000".parse().unwrap())
    );
    assert_eq!(
        jsonx::from_str::<Value>(r#"bytes("YWJjZA==")"#).unwrap(),
        Value::Bytes(b"abcd".to_vec())
    );
}

// ---------------------------------------------------------------------------
// Custom constructors: the open, dynamic `Value` path
// ---------------------------------------------------------------------------

fn ctor(name: &str, arg: Value) -> Value {
    Value::Constructor {
        name: name.to_owned(),
        arg: Box::new(arg),
    }
}

#[test]
fn unknown_constructor_to_value() {
    // An unrecognized `name(...)` is captured rather than rejected.
    assert_eq!(
        jsonx::from_str::<Value>(r#"duration("5s")"#).unwrap(),
        ctor("duration", Value::String("5s".into()))
    );
    assert_eq!(
        jsonx::from_str::<Value>(r#"ipnet("10.0.0.0/8")"#).unwrap(),
        ctor("ipnet", Value::String("10.0.0.0/8".into()))
    );
}

#[test]
fn custom_constructor_round_trips() {
    for text in [
        r#"duration("5s")"#,
        r#"ipnet("10.0.0.0/8")"#,
        r#"uuid("550e8400-e29b-41d4-a716-446655440000")"#,
        // A non-string argument: bare numbers stay JSON floats.
        r#"scaled(1.5)"#,
        // A nested built-in constructor as the argument.
        r#"wrapped(int(5))"#,
        // A nested custom constructor.
        r#"outer(inner("x"))"#,
        // Compound arguments round-trip too (bare numbers are f64, so they
        // render in shortest form — integers keep a `.0`).
        r#"point([1.0,2.0])"#,
    ] {
        let value: Value = jsonx::from_str(text).unwrap();
        assert_eq!(jsonx::to_string(&value).unwrap(), text, "round-trip of {text}");
        // ...and re-parsing the output yields the identical value.
        assert_eq!(jsonx::from_str::<Value>(&jsonx::to_string(&value).unwrap()).unwrap(), value);
    }
}

#[test]
fn custom_constructor_nested_in_containers() {
    let value: Value =
        jsonx::from_str(r#"{a: duration("5s"), b: [cidr("0.0.0.0/0")],}"#).unwrap();
    assert_eq!(value.get("a"), Some(&ctor("duration", Value::String("5s".into()))));
    assert_eq!(
        value.get("b").and_then(Value::as_array),
        Some(&[ctor("cidr", Value::String("0.0.0.0/0".into()))][..])
    );
}

#[test]
fn constructor_helper_builds_and_encodes() {
    assert_eq!(
        jsonx::to_string(&Value::constructor("duration", "5s")).unwrap(),
        r#"duration("5s")"#
    );
    assert_eq!(
        jsonx::to_string(&Value::constructor("scaled", 1.5)).unwrap(),
        "scaled(1.5)"
    );
}

#[test]
fn deeply_nested_constructors_are_bounded() {
    // A chain `a(a(a(...)))` recurses through the dynamic path; it must be
    // depth-bounded and error rather than overflow the stack.
    let deep = format!("{}1{}", "a(".repeat(500), ")".repeat(500));
    assert!(jsonx::from_str::<Value>(&deep).is_err());
}

#[test]
fn invalid_constructor_name_fails_to_serialize() {
    // A constructor name that isn't a legal JSONX identifier has no
    // round-trippable form: quoting it would emit `"bad name"("x")`, which
    // doesn't parse back as a constructor. The serializer must reject it rather
    // than silently produce unparseable output. (Built directly as a struct
    // literal so we test the serializer's guarantee, independent of the
    // debug-build assert in `Value::constructor`.)
    for bad in ["bad name", "3lead", "with-dash", "", "a(b", "💥"] {
        let v = ctor(bad, Value::String("x".into()));
        assert!(
            jsonx::to_string(&v).is_err(),
            "expected error serializing constructor name {bad:?}, got {:?}",
            jsonx::to_string(&v)
        );
    }
}

#[test]
fn valid_constructor_names_round_trip() {
    // The full identifier set — leading letter or `_`, then alphanumerics or
    // `_` — serializes to bare JSONX and parses straight back.
    for good in ["good", "_private", "x", "snake_case", "with9digits", "_"] {
        let v = ctor(good, Value::String("x".into()));
        let text = jsonx::to_string(&v).unwrap();
        assert_eq!(text, format!(r#"{good}("x")"#));
        assert_eq!(jsonx::from_str::<Value>(&text).unwrap(), v);
    }
}

// ---------------------------------------------------------------------------
// Canonical argument text, value builders, and key ordering
// ---------------------------------------------------------------------------

#[test]
fn to_jsonx_arg_yields_canonical_text() {
    let dt: Value = jsonx::from_str(r#"datetime("2017-12-25T15:00:00Z")"#).unwrap();
    assert_eq!(dt.to_jsonx_arg().as_deref(), Some("2017-12-25T15:00:00Z"));
    assert_eq!(
        Value::Ip("10.0.0.1".parse().unwrap()).to_jsonx_arg().as_deref(),
        Some("10.0.0.1")
    );
    assert_eq!(Value::bytes(b"abcd".to_vec()).to_jsonx_arg().as_deref(), Some("YWJjZA=="));
    assert_eq!(Value::int(7).to_jsonx_arg().as_deref(), Some("7"));
    assert_eq!(Value::Int8(-128).to_jsonx_arg().as_deref(), Some("-128"));
    // A custom constructor returns its string argument directly...
    assert_eq!(
        Value::constructor("ipnet", "10.0.0.0/8").to_jsonx_arg().as_deref(),
        Some("10.0.0.0/8")
    );
    // ...or the JSONX rendering of a non-string argument. Plain numbers are
    // f64 and render in shortest round-trip form, so integers keep a `.0`.
    assert_eq!(
        Value::constructor("point", Value::Array(vec![Value::Number(1.0), Value::Number(2.0)]))
            .to_jsonx_arg()
            .as_deref(),
        Some("[1.0,2.0]")
    );
    // Plain JSON values are not constructors.
    assert_eq!(Value::Null.to_jsonx_arg(), None);
    assert_eq!(Value::String("x".into()).to_jsonx_arg(), None);
    assert_eq!(Value::Number(1.5).to_jsonx_arg(), None);
}

#[test]
fn datetime_helpers_are_public() {
    let dt = jsonx::datetime::parse("2017-12-25T15:00:00+00:00").unwrap();
    assert_eq!(jsonx::datetime::to_jsonx_string(&dt), "2017-12-25T15:00:00Z");
}

#[test]
fn value_builders_encode() {
    assert_eq!(jsonx::to_string(&Value::int(-5)).unwrap(), "int(-5)");
    assert_eq!(jsonx::to_string(&Value::uint(5)).unwrap(), "uint(5)");
    assert_eq!(jsonx::to_string(&Value::bytes(b"hi".to_vec())).unwrap(), r#"bytes("aGk=")"#);
    assert_eq!(jsonx::to_string(&Value::string("x")).unwrap(), r#""x""#);
}

#[test]
fn object_key_ordering_matches_backend() {
    let v: Value = jsonx::from_str("{b: 1, a: 2, c: 3}").unwrap();
    let text = jsonx::to_string(&v).unwrap();
    // Bare numbers are f64 and render in shortest form (so `1` -> `1.0`); this
    // test only cares about key ordering.
    #[cfg(feature = "preserve_order")]
    assert_eq!(text, "{b:1.0,a:2.0,c:3.0}");
    #[cfg(not(feature = "preserve_order"))]
    assert_eq!(text, "{a:2.0,b:1.0,c:3.0}");
}

// ---------------------------------------------------------------------------
// Encoding (compact + pretty) — fidelity to the reference output
// ---------------------------------------------------------------------------

fn reference_value() -> Value {
    let mut m = Map::new();
    m.insert("k01".into(), Value::Null);
    m.insert("k02".into(), Value::Bool(false));
    m.insert("k03".into(), Value::Bool(true));
    m.insert("k04".into(), Value::String("test".into()));
    m.insert("k05".into(), Value::Number(1.45678e-98));
    m.insert("k06".into(), Value::Int(-454365464));
    m.insert("k07".into(), Value::Uint(455645765));
    m.insert("k08".into(), Value::Int8(-128));
    m.insert("k09".into(), Value::Uint8(255));
    m.insert("k10".into(), Value::Int16(32767));
    m.insert("k11".into(), Value::Uint16(65535));
    m.insert("k12".into(), Value::Int32(i32::MAX));
    m.insert("k13".into(), Value::Uint32(u32::MAX));
    m.insert("k14".into(), Value::Int64(i64::MAX));
    m.insert("k15".into(), Value::Uint64(u64::MAX));
    m.insert(
        "k16".into(),
        Value::DateTime(DateTime::parse_from_rfc3339("2017-12-25T15:00:00Z").unwrap()),
    );
    m.insert("k17".into(), Value::Ip("192.168.1.2".parse().unwrap()));
    m.insert("k18".into(), Value::IpPort("192.168.1.2:65000".parse().unwrap()));
    m.insert("k19".into(), Value::Ip("::1".parse().unwrap()));
    m.insert("k20".into(), Value::IpPort("[::1]:65000".parse().unwrap()));
    m.insert(
        "k21".into(),
        Value::Array(vec![Value::String("test".into()), Value::Int(123)]),
    );
    let mut inner = Map::new();
    inner.insert("test".into(), Value::Bool(true));
    m.insert("k22".into(), Value::Object(inner));
    Value::Object(m)
}

#[test]
fn encode_compact() {
    let expected = r#"{k01:null,k02:false,k03:true,k04:"test",k05:1.45678e-98,k06:int(-454365464),k07:uint(455645765),k08:int8(-128),k09:uint8(255),k10:int16(32767),k11:uint16(65535),k12:int32(2147483647),k13:uint32(4294967295),k14:int64("9223372036854775807"),k15:uint64("18446744073709551615"),k16:datetime("2017-12-25T15:00:00Z"),k17:ip("192.168.1.2"),k18:ipport("192.168.1.2:65000"),k19:ip("::1"),k20:ipport("[::1]:65000"),k21:["test",int(123)],k22:{test:true}}"#;
    assert_eq!(jsonx::to_string(&reference_value()).unwrap(), expected);
}

#[test]
fn encode_pretty() {
    let expected = r#"{
  k01: null,
  k02: false,
  k03: true,
  k04: "test",
  k05: 1.45678e-98,
  k06: int(-454365464),
  k07: uint(455645765),
  k08: int8(-128),
  k09: uint8(255),
  k10: int16(32767),
  k11: uint16(65535),
  k12: int32(2147483647),
  k13: uint32(4294967295),
  k14: int64("9223372036854775807"),
  k15: uint64("18446744073709551615"),
  k16: datetime("2017-12-25T15:00:00Z"),
  k17: ip("192.168.1.2"),
  k18: ipport("192.168.1.2:65000"),
  k19: ip("::1"),
  k20: ipport("[::1]:65000"),
  k21: [
    "test",
    int(123)
  ],
  k22: {
    test: true
  }
}"#;
    assert_eq!(jsonx::to_string_pretty(&reference_value()).unwrap(), expected);
}

#[test]
fn value_round_trips_through_text() {
    let value = reference_value();
    let text = jsonx::to_string(&value).unwrap();
    let back: Value = jsonx::from_str(&text).unwrap();
    assert_eq!(value, back);

    let pretty = jsonx::to_string_pretty(&value).unwrap();
    let back_pretty: Value = jsonx::from_str(&pretty).unwrap();
    assert_eq!(value, back_pretty);
}

#[test]
fn empty_containers_encode_tightly() {
    assert_eq!(jsonx::to_string(&Value::Array(vec![])).unwrap(), "[]");
    assert_eq!(jsonx::to_string(&Value::Object(Map::new())).unwrap(), "{}");
    assert_eq!(jsonx::to_string_pretty(&Value::Array(vec![])).unwrap(), "[]");
}

// ---------------------------------------------------------------------------
// serde derive on user structs
// ---------------------------------------------------------------------------

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Server {
    name: String,
    port: u16,
    weight: i32,
    tags: Vec<String>,
    nick: Option<String>,
}

#[test]
fn derive_struct_round_trip() {
    let server = Server {
        name: "db".into(),
        port: 5432,
        weight: -1,
        tags: vec!["a".into(), "b".into()],
        nick: None,
    };
    let text = jsonx::to_string(&server).unwrap();
    assert_eq!(
        text,
        r#"{name:"db",port:uint16(5432),weight:int32(-1),tags:["a","b"],nick:null}"#
    );
    assert_eq!(jsonx::from_str::<Server>(&text).unwrap(), server);
}

#[test]
fn derive_struct_accepts_quoted_and_unquoted_keys() {
    let from_relaxed: Server =
        jsonx::from_str(r#"{ name: "db", port: 5432, weight: -1, tags: [], nick: "d", }"#).unwrap();
    assert_eq!(from_relaxed.name, "db");
    assert_eq!(from_relaxed.port, 5432);
    assert_eq!(from_relaxed.nick.as_deref(), Some("d"));
}

#[test]
fn derive_with_wrappers() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Record {
        #[serde(with = "jsonx::ip")]
        addr: IpAddr,
        #[serde(with = "jsonx::ipport")]
        listen: SocketAddr,
        blob: Bytes,
        big: Int,
        count: Uint,
        #[serde(with = "jsonx::datetime")]
        ts: DateTime,
    }

    let record = Record {
        addr: "10.0.0.1".parse::<IpAddr>().unwrap(),
        listen: "[::1]:8080".parse::<SocketAddr>().unwrap(),
        blob: Bytes(b"hello".to_vec()),
        big: Int(-9000000000),
        count: Uint(42),
        ts: DateTime::parse_from_rfc3339("2020-06-01T08:30:00Z").unwrap(),
    };

    let text = jsonx::to_string(&record).unwrap();
    assert_eq!(
        text,
        r#"{addr:ip("10.0.0.1"),listen:ipport("[::1]:8080"),blob:bytes("aGVsbG8="),big:int(-9000000000),count:uint(42),ts:datetime("2020-06-01T08:30:00Z")}"#
    );
    assert_eq!(jsonx::from_str::<Record>(&text).unwrap(), record);
}

#[test]
fn derive_with_attribute_free_wrappers() {
    // Same struct, no `#[serde(with = ...)]` anywhere: the wrappers carry the
    // JSONX type selection themselves.
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Record {
        addr: Ip,
        listen: IpPort,
        blob: Bytes,
        big: Int,
        count: Uint,
        ts: Datetime,
    }

    let record = Record {
        addr: Ip("10.0.0.1".parse().unwrap()),
        listen: IpPort("[::1]:8080".parse().unwrap()),
        blob: Bytes(b"hello".to_vec()),
        big: Int(-9000000000),
        count: Uint(42),
        ts: Datetime(DateTime::parse_from_rfc3339("2020-06-01T08:30:00Z").unwrap()),
    };

    let text = jsonx::to_string(&record).unwrap();
    assert_eq!(
        text,
        r#"{addr:ip("10.0.0.1"),listen:ipport("[::1]:8080"),blob:bytes("aGVsbG8="),big:int(-9000000000),count:uint(42),ts:datetime("2020-06-01T08:30:00Z")}"#
    );
    assert_eq!(jsonx::from_str::<Record>(&text).unwrap(), record);
}

#[test]
fn wrappers_are_transparent_to_other_formats() {
    // The same wrapper types degrade to plain strings/ints in a non-JSONX
    // format (serde_json stands in for TOML), and round-trip there too.
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Record {
        addr: Ip,
        ts: Datetime,
        big: Int,
    }

    let record = Record {
        addr: Ip("10.0.0.1".parse().unwrap()),
        ts: Datetime(DateTime::parse_from_rfc3339("2020-06-01T08:30:00Z").unwrap()),
        big: Int(7),
    };

    let json = serde_json::to_string(&record).unwrap();
    assert_eq!(json, r#"{"addr":"10.0.0.1","ts":"2020-06-01T08:30:00Z","big":7}"#);
    assert_eq!(serde_json::from_str::<Record>(&json).unwrap(), record);
}

// ---------------------------------------------------------------------------
// Open extension API: a caller's own type gets its own `type(value)` form
// ---------------------------------------------------------------------------

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct Version {
    major: u16,
    minor: u16,
}

impl jsonx::JsonxConstructor for Version {
    const TOKEN: &'static str = jsonx::ctor!("semver");
    fn to_jsonx_arg(&self) -> String {
        format!("{}.{}", self.major, self.minor)
    }
    fn from_jsonx_arg(arg: &str) -> Result<Self, String> {
        let (major, minor) = arg.split_once('.').ok_or("expected MAJOR.MINOR")?;
        Ok(Version {
            major: major.parse().map_err(|_| "bad major")?,
            minor: minor.parse().map_err(|_| "bad minor")?,
        })
    }
}

impl Serialize for Version {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        jsonx::constructor::serialize(self, s)
    }
}
impl<'de> Deserialize<'de> for Version {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        jsonx::constructor::deserialize(d)
    }
}

#[test]
fn custom_constructor_round_trips_in_jsonx() {
    let v = Version { major: 1, minor: 4 };
    let text = jsonx::to_string(&v).unwrap();
    assert_eq!(text, r#"semver("1.4")"#);
    assert_eq!(jsonx::from_str::<Version>(&text).unwrap(), v);

    // Inside a struct, alongside built-in extended types, with no attributes.
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct App {
        version: Version,
        bind: IpPort,
    }
    let app = App {
        version: Version { major: 2, minor: 0 },
        bind: IpPort("0.0.0.0:80".parse().unwrap()),
    };
    let text = jsonx::to_string(&app).unwrap();
    assert_eq!(text, r#"{version:semver("2.0"),bind:ipport("0.0.0.0:80")}"#);
    assert_eq!(jsonx::from_str::<App>(&text).unwrap(), app);
}

#[test]
fn custom_constructor_is_transparent_to_other_formats() {
    // The very same type is a plain string in a non-JSONX format.
    let v = Version { major: 1, minor: 4 };
    let json = serde_json::to_string(&v).unwrap();
    assert_eq!(json, r#""1.4""#);
    assert_eq!(serde_json::from_str::<Version>(&json).unwrap(), v);
}

// `#[derive(JsonxConstructor)]` — the same thing with no hand-written impls,
// driven by `Display` + `FromStr`. Gated so `--no-default-features` still builds.
#[cfg(feature = "derive")]
mod derive_tests {

#[derive(jsonx::JsonxConstructor, Clone, Copy, PartialEq, Eq, Debug)]
#[jsonx(name = "semver")]
struct DerivedVersion {
    major: u16,
    minor: u16,
}

impl std::fmt::Display for DerivedVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}.{}", self.major, self.minor)
    }
}
impl std::str::FromStr for DerivedVersion {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, String> {
        let (a, b) = s.split_once('.').ok_or("expected MAJOR.MINOR")?;
        Ok(DerivedVersion {
            major: a.parse().map_err(|_| "bad major")?,
            minor: b.parse().map_err(|_| "bad minor")?,
        })
    }
}

// A second derived type with no `#[jsonx(name)]`: the name defaults to the type
// name lowercased (`Mac` -> `mac`).
#[derive(jsonx::JsonxConstructor, Clone, Copy, PartialEq, Eq, Debug)]
struct Mac([u8; 6]);

impl std::fmt::Display for Mac {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let b = self.0;
        write!(
            f,
            "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
            b[0], b[1], b[2], b[3], b[4], b[5]
        )
    }
}
impl std::str::FromStr for Mac {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, String> {
        let mut out = [0u8; 6];
        let mut parts = s.split(':');
        for slot in &mut out {
            let p = parts.next().ok_or("too few octets")?;
            *slot = u8::from_str_radix(p, 16).map_err(|_| "bad octet")?;
        }
        if parts.next().is_some() {
            return Err("too many octets".into());
        }
        Ok(Mac(out))
    }
}

#[test]
fn derived_constructor_round_trips_and_is_transparent() {
    let v = DerivedVersion { major: 1, minor: 4 };
    let text = jsonx::to_string(&v).unwrap();
    assert_eq!(text, r#"semver("1.4")"#);
    assert_eq!(jsonx::from_str::<DerivedVersion>(&text).unwrap(), v);

    // Same derived type, plain string in JSON.
    let json = serde_json::to_string(&v).unwrap();
    assert_eq!(json, r#""1.4""#);
    assert_eq!(serde_json::from_str::<DerivedVersion>(&json).unwrap(), v);
}

#[test]
fn derived_constructor_defaults_name_to_lowercased_type() {
    let mac = Mac([0xde, 0xad, 0xbe, 0xef, 0x00, 0x01]);
    let text = jsonx::to_string(&mac).unwrap();
    assert_eq!(text, r#"mac("de:ad:be:ef:00:01")"#);
    assert_eq!(jsonx::from_str::<Mac>(&text).unwrap(), mac);
}

} // mod derive_tests

#[test]
fn custom_constructor_via_with_attribute() {
    // The same type, applied through serde's `with` attribute instead of its
    // own Serialize/Deserialize impls.
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Release {
        #[serde(with = "jsonx::constructor")]
        at: Version,
    }
    let r = Release { at: Version { major: 3, minor: 7 } };
    let text = jsonx::to_string(&r).unwrap();
    assert_eq!(text, r#"{at:semver("3.7")}"#);
    assert_eq!(jsonx::from_str::<Release>(&text).unwrap(), r);
}

#[test]
fn derive_enums() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum E {
        Unit,
        New(i32),
        Tuple(i32, i32),
        Struct { a: i32 },
    }

    for (value, text) in [
        (E::Unit, r#""Unit""#),
        (E::New(5), r#"{New:int32(5)}"#),
        (E::Tuple(1, 2), r#"{Tuple:[int32(1),int32(2)]}"#),
        (E::Struct { a: 7 }, r#"{Struct:{a:int32(7)}}"#),
    ] {
        assert_eq!(jsonx::to_string(&value).unwrap(), text, "encoding {value:?}");
        assert_eq!(jsonx::from_str::<E>(text).unwrap(), value, "decoding {text}");
    }
}

#[test]
fn map_with_integer_keys() {
    let mut m: BTreeMap<u32, String> = BTreeMap::new();
    m.insert(1, "one".into());
    m.insert(2, "two".into());
    let text = jsonx::to_string(&m).unwrap();
    assert_eq!(text, r#"{"1":"one","2":"two"}"#);
    assert_eq!(jsonx::from_str::<BTreeMap<u32, String>>(&text).unwrap(), m);
}

// ---------------------------------------------------------------------------
// Non-greedy decoding & errors
// ---------------------------------------------------------------------------

#[test]
fn non_greedy_decoding() {
    let (value, offset): (Value, usize) = jsonx::from_str_partial("{test: 1} blah").unwrap();
    let mut expected = Map::new();
    expected.insert("test".into(), Value::Number(1.0));
    assert_eq!(value, Value::Object(expected));
    assert_eq!(&"{test: 1} blah"[offset..], "blah");
}

#[test]
fn trailing_data_is_an_error_for_from_str() {
    let err = jsonx::from_str::<Value>("{a:1} blah").unwrap_err();
    assert!(matches!(err, jsonx::Error::TrailingData { .. }));
}

#[test]
fn syntax_errors() {
    assert!(jsonx::from_str::<Value>("[,]").is_err());
    assert!(jsonx::from_str::<Value>("{").is_err());
    assert!(jsonx::from_str::<Value>(r#"{"X": "foo", "Y"}"#).is_err());
    assert!(jsonx::from_str::<Value>("nul").is_err());
    assert!(jsonx::from_str::<Value>("[1, 2, 3+]").is_err());
}

#[test]
fn non_finite_float_is_rejected_on_encode() {
    assert!(jsonx::to_string(&f64::NAN).is_err());
    assert!(jsonx::to_string(&f64::INFINITY).is_err());
}

#[test]
fn display_matches_compact_encoding() {
    let value = reference_value();
    assert_eq!(value.to_string(), jsonx::to_string(&value).unwrap());
}

// ---------------------------------------------------------------------------
// Robustness: malformed input must error rather than panic
// ---------------------------------------------------------------------------

#[test]
fn malformed_input_never_panics() {
    let inputs = [
        "", " ", "[", "]", "{", "}", "{:}", "[,]", "{,}", ",", ":",
        "int", "int(", "int()", "int8(", "ip(", "ip()", "datetime()",
        "bytes(\"!!!!\")", "\"unterminated", "\"\\", "\"\\u", "\"\\uzzzz\"",
        "{a", "{a:", "{a:1", "[1", "[1,", "tru", "fals", "nul", "-", "0.",
        "1e", "1.e5", "--5", "01", "{\"a\":1}{\"b\":2}", "ip(\"not-an-ip\")",
        "int8(99999999999999999999999999)", "+", "0x10", ".5", "[[[",
    ];
    for input in inputs {
        // Must return a Result (Ok or Err), never panic.
        let _ = jsonx::from_str::<jsonx::Value>(input);
    }
}

#[test]
fn deep_nesting_is_bounded() {
    let deep = "[".repeat(100_000);
    let err = jsonx::from_str::<jsonx::Value>(&deep).unwrap_err();
    assert!(matches!(err, jsonx::Error::Syntax { .. }));
}

#[test]
fn number_overflow_is_rejected() {
    assert!(jsonx::from_str::<Value>("1e400").is_err());
    assert!(jsonx::from_str::<f64>("-1e400").is_err());
}

/// A fractional part combined with an exponent so long that it saturates the
/// i64 accumulator must not overflow the decimal-exponent total. Underflowing
/// values round to zero; overflowing ones are rejected as out of range.
#[test]
fn extreme_exponent_does_not_overflow() {
    // Two fractional digits plus a saturating negative exponent: the slow
    // path rounds to zero.
    assert_eq!(jsonx::from_str::<f64>("0.00e-99999999999999999999").unwrap(), 0.0);
    assert_eq!(
        jsonx::from_str::<f64>("1.21e-8888888888888888888888").unwrap(),
        0.0
    );
    // The one-fractional-digit boundary lands on i64::MIN exactly.
    assert_eq!(jsonx::from_str::<f64>("0.0e-99999999999999999999").unwrap(), 0.0);
    // A saturating positive exponent still overflows f64 and is rejected.
    assert!(jsonx::from_str::<f64>("1.21e99999999999999999999").is_err());
}

// ---------------------------------------------------------------------------
// Number/integer parsing & formatting fast paths
// ---------------------------------------------------------------------------

/// The fast-path float parser must be bit-identical to the standard library on
/// both its exact (≤15-digit) fast path and the fallback for harder inputs.
#[test]
fn float_parsing_is_bit_exact() {
    let cases = [
        "0", "-0", "0.5", "1.5", "123.456", "0.001", "1e-3", "6.022e23",
        "1.7976931348623157e308", "2.2250738585072014e-308", "5e-324",
        "9007199254740993", "0.1", "0.2", "0.3", "1234567.0",
        "12345678901234567890.12345678901234567890", "9.999999999999999e22",
        "3.141592653589793", "2.718281828459045", "-0.0", "100000000000000000000",
    ];
    for s in cases {
        let want: f64 = s.parse().unwrap();
        let got: f64 = jsonx::from_str(s).unwrap();
        assert_eq!(want.to_bits(), got.to_bits(), "parsing {s:?}");
    }
}

/// Bare integers and the typed-integer constructors must parse across the full
/// i64/u64 range, including the values that straddle the i64/i128 fallback.
#[test]
fn integer_boundaries_parse() {
    assert_eq!(jsonx::from_str::<i64>("-9223372036854775808").unwrap(), i64::MIN);
    assert_eq!(jsonx::from_str::<i64>("9223372036854775807").unwrap(), i64::MAX);
    assert_eq!(jsonx::from_str::<u64>("18446744073709551615").unwrap(), u64::MAX);
    assert_eq!(jsonx::from_str::<u64>("9223372036854775808").unwrap(), 1u64 << 63);
    assert_eq!(jsonx::from_str::<i128>("-9223372036854775809").unwrap(), -9223372036854775809i128);
    assert_eq!(jsonx::from_str::<u64>(r#"uint64("18446744073709551615")"#).unwrap(), u64::MAX);
    assert_eq!(jsonx::from_str::<i64>("int64(-9223372036854775808)").unwrap(), i64::MIN);
    // Out-of-range for the target width is still rejected.
    assert!(jsonx::from_str::<u8>("256").is_err());
    assert!(jsonx::from_str::<i32>("2147483648").is_err());
    assert!(jsonx::from_str::<u16>("-1").is_err());
}

/// Floats render in shortest round-trip form (ryu): integers keep a trailing
/// `.0`, `-0.0` keeps its sign, and every value round-trips bit-exactly.
#[test]
fn float_formatting_round_trips() {
    assert_eq!(jsonx::to_string(&5432.0_f64).unwrap(), "5432.0");
    assert_eq!(jsonx::to_string(&-1.0_f64).unwrap(), "-1.0");
    assert_eq!(jsonx::to_string(&0.0_f64).unwrap(), "0.0");
    assert_eq!(jsonx::to_string(&-0.0_f64).unwrap(), "-0.0");
    assert_eq!(jsonx::to_string(&1.5_f64).unwrap(), "1.5");
    assert_eq!(jsonx::to_string(&6.022e23_f64).unwrap(), "6.022e23");
    for v in [0.0, -0.0, 1.5, -2.5e-310, 9007199254740993.0, 42.0, f64::MAX] {
        let s = jsonx::to_string(&v).unwrap();
        let back: f64 = jsonx::from_str(&s).unwrap();
        assert_eq!(v.to_bits(), back.to_bits(), "round-trip {v} via {s}");
    }
}

/// Multibyte UTF-8 in an escape-free string borrows and is returned intact,
/// while invalid UTF-8 bytes inside a string are rejected (guards the ASCII
/// fast path's unchecked conversion).
#[test]
fn utf8_strings_are_validated() {
    assert_eq!(jsonx::from_str::<String>("\"café 日本語 🦀\"").unwrap(), "café 日本語 🦀");
    assert!(jsonx::from_slice::<String>(b"\"a\xffb\"").is_err());
    assert!(jsonx::from_slice::<String>(b"\"\xe2\x28\xa1\"").is_err());
    // Escapes force the owned path; multibyte runs around an escape must still
    // validate and copy correctly, and invalid UTF-8 there must be rejected.
    assert_eq!(jsonx::from_str::<String>("\"café\\tend 🦀\"").unwrap(), "café\tend 🦀");
    assert!(jsonx::from_slice::<String>(b"\"a\\t\xffb\"").is_err());
}