-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
NumberHandlingTests.cs
1954 lines (1634 loc) · 90.8 KB
/
NumberHandlingTests.cs
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json.Tests;
using System.Threading.Tasks;
using Xunit;
namespace System.Text.Json.Serialization.Tests
{
public abstract class NumberHandlingTests : SerializerTests
{
public NumberHandlingTests(JsonSerializerWrapper serializerWrapper) : base(serializerWrapper) { }
private static readonly JsonSerializerOptions s_optionReadFromStr = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
private static readonly JsonSerializerOptions s_optionWriteAsStr = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.WriteAsString
};
private static readonly JsonSerializerOptions s_optionReadAndWriteFromStr = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString
};
private static readonly JsonSerializerOptions s_optionsAllowFloatConstants = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals
};
private static readonly JsonSerializerOptions s_optionReadFromStrAllowFloatConstants = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals
};
private static readonly JsonSerializerOptions s_optionWriteAsStrAllowFloatConstants = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowNamedFloatingPointLiterals
};
[Fact]
public async Task Number_AsRootType_RoundTrip()
{
await RunAsRootTypeTest(JsonNumberTestData.Bytes);
await RunAsRootTypeTest(JsonNumberTestData.SBytes);
await RunAsRootTypeTest(JsonNumberTestData.Shorts);
await RunAsRootTypeTest(JsonNumberTestData.Ints);
await RunAsRootTypeTest(JsonNumberTestData.Longs);
await RunAsRootTypeTest(JsonNumberTestData.UShorts);
await RunAsRootTypeTest(JsonNumberTestData.UInts);
await RunAsRootTypeTest(JsonNumberTestData.ULongs);
await RunAsRootTypeTest(JsonNumberTestData.Floats);
await RunAsRootTypeTest(JsonNumberTestData.Doubles);
await RunAsRootTypeTest(JsonNumberTestData.Decimals);
#if NETCOREAPP
await RunAsRootTypeTest(JsonNumberTestData.Int128s);
await RunAsRootTypeTest(JsonNumberTestData.UInt128s);
await RunAsRootTypeTest(JsonNumberTestData.Halfs);
#endif
await RunAsRootTypeTest(JsonNumberTestData.NullableBytes);
await RunAsRootTypeTest(JsonNumberTestData.NullableSBytes);
await RunAsRootTypeTest(JsonNumberTestData.NullableShorts);
await RunAsRootTypeTest(JsonNumberTestData.NullableInts);
await RunAsRootTypeTest(JsonNumberTestData.NullableLongs);
await RunAsRootTypeTest(JsonNumberTestData.NullableUShorts);
await RunAsRootTypeTest(JsonNumberTestData.NullableUInts);
await RunAsRootTypeTest(JsonNumberTestData.NullableULongs);
await RunAsRootTypeTest(JsonNumberTestData.NullableFloats);
await RunAsRootTypeTest(JsonNumberTestData.NullableDoubles);
await RunAsRootTypeTest(JsonNumberTestData.NullableDecimals);
#if NETCOREAPP
await RunAsRootTypeTest(JsonNumberTestData.NullableInt128s);
await RunAsRootTypeTest(JsonNumberTestData.NullableUInt128s);
await RunAsRootTypeTest(JsonNumberTestData.NullableHalfs);
#endif
}
private async Task RunAsRootTypeTest<T>(List<T> numbers)
{
foreach (T number in numbers)
{
string numberAsString = GetNumberAsString(number);
string json = $"{numberAsString}";
string jsonWithNumberAsString = @$"""{numberAsString}""";
await PerformAsRootTypeSerialization(number, json, jsonWithNumberAsString);
}
}
private static string GetNumberAsString<T>(T number)
{
return number switch
{
double @double => @double.ToString(JsonTestHelper.DoubleFormatString, CultureInfo.InvariantCulture),
float @float => @float.ToString(JsonTestHelper.SingleFormatString, CultureInfo.InvariantCulture),
decimal @decimal => @decimal.ToString(CultureInfo.InvariantCulture),
_ => Convert.ToString(number, CultureInfo.InvariantCulture)
};
}
private async Task PerformAsRootTypeSerialization<T>(T number, string jsonWithNumberAsNumber, string jsonWithNumberAsString)
{
// Option: read from string
// Deserialize
Assert.Equal(number, await Serializer.DeserializeWrapper<T>(jsonWithNumberAsNumber, s_optionReadFromStr));
Assert.Equal(number, await Serializer.DeserializeWrapper<T>(jsonWithNumberAsString, s_optionReadFromStr));
// Serialize
Assert.Equal(jsonWithNumberAsNumber, await Serializer.SerializeWrapper(number, s_optionReadFromStr));
// Option: write as string
// Deserialize
Assert.Equal(number, await Serializer.DeserializeWrapper<T>(jsonWithNumberAsNumber, s_optionWriteAsStr));
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<T>(jsonWithNumberAsString, s_optionWriteAsStr));
// Serialize
Assert.Equal(jsonWithNumberAsString, await Serializer.SerializeWrapper(number, s_optionWriteAsStr));
// Option: read and write from/to string
// Deserialize
Assert.Equal(number, await Serializer.DeserializeWrapper<T>(jsonWithNumberAsNumber, s_optionReadAndWriteFromStr));
Assert.Equal(number, await Serializer.DeserializeWrapper<T>(jsonWithNumberAsString, s_optionReadAndWriteFromStr));
// Serialize
Assert.Equal(jsonWithNumberAsString, await Serializer.SerializeWrapper(number, s_optionReadAndWriteFromStr));
}
[Fact]
public async Task Number_AsBoxed_RootType()
{
string numberAsString = @"""2""";
int @int = 2;
float @float = 2;
int? nullableInt = 2;
float? nullableFloat = 2;
Assert.Equal(numberAsString, await Serializer.SerializeWrapper((object)@int, s_optionReadAndWriteFromStr));
Assert.Equal(numberAsString, await Serializer.SerializeWrapper((object)@float, s_optionReadAndWriteFromStr));
Assert.Equal(numberAsString, await Serializer.SerializeWrapper((object)nullableInt, s_optionReadAndWriteFromStr));
Assert.Equal(numberAsString, await Serializer.SerializeWrapper((object)nullableFloat, s_optionReadAndWriteFromStr));
Assert.Equal(2, (int)await Serializer.DeserializeWrapper(numberAsString, typeof(int), s_optionReadAndWriteFromStr));
Assert.Equal(2, (float)await Serializer.DeserializeWrapper(numberAsString, typeof(float), s_optionReadAndWriteFromStr));
Assert.Equal(2, (int?)await Serializer.DeserializeWrapper(numberAsString, typeof(int?), s_optionReadAndWriteFromStr));
Assert.Equal(2, (float?)await Serializer.DeserializeWrapper(numberAsString, typeof(float?), s_optionReadAndWriteFromStr));
}
[Fact]
public async Task Number_AsBoxed_Property()
{
int @int = 1;
float? nullableFloat = 2;
string expected = @"{""MyInt"":""1"",""MyNullableFloat"":""2""}";
var obj = new Class_With_BoxedNumbers
{
MyInt = @int,
MyNullableFloat = nullableFloat
};
string serialized = await Serializer.SerializeWrapper(obj);
JsonTestHelper.AssertJsonEqual(expected, serialized);
obj = await Serializer.DeserializeWrapper<Class_With_BoxedNumbers>(serialized);
JsonElement el = Assert.IsType<JsonElement>(obj.MyInt);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal("1", el.GetString());
el = Assert.IsType<JsonElement>(obj.MyNullableFloat);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal("2", el.GetString());
}
public class Class_With_BoxedNumbers
{
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public object MyInt { get; set; }
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public object MyNullableFloat { get; set; }
}
[Fact]
public async Task Number_AsBoxed_CollectionRootType_Element()
{
int @int = 1;
float? nullableFloat = 2;
string expected = @"[""1""]";
var obj = new List<object> { @int };
string serialized = await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr);
Assert.Equal(expected, serialized);
obj = await Serializer.DeserializeWrapper<List<object>>(serialized, s_optionReadAndWriteFromStr);
JsonElement el = Assert.IsType<JsonElement>(obj[0]);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal("1", el.GetString());
expected = @"[""2""]";
IList obj2 = new object[] { nullableFloat };
serialized = await Serializer.SerializeWrapper(obj2, s_optionReadAndWriteFromStr);
Assert.Equal(expected, serialized);
obj2 = await Serializer.DeserializeWrapper<IList>(serialized, s_optionReadAndWriteFromStr);
el = Assert.IsType<JsonElement>(obj2[0]);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal("2", el.GetString());
}
[Fact]
public async Task Number_AsBoxed_CollectionProperty_Element()
{
int @int = 2;
float? nullableFloat = 2;
string expected = @"{""MyInts"":[""2""],""MyNullableFloats"":[""2""]}";
var obj = new Class_With_ListsOfBoxedNumbers
{
MyInts = new List<object> { @int },
MyNullableFloats = new object[] { nullableFloat }
};
string serialized = await Serializer.SerializeWrapper(obj);
JsonTestHelper.AssertJsonEqual(expected, serialized);
obj = await Serializer.DeserializeWrapper<Class_With_ListsOfBoxedNumbers>(serialized);
JsonElement el = Assert.IsType<JsonElement>(obj.MyInts[0]);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal("2", el.GetString());
el = Assert.IsType<JsonElement>(obj.MyNullableFloats[0]);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal("2", el.GetString());
}
public class Class_With_ListsOfBoxedNumbers
{
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public List<object> MyInts { get; set; }
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public IList MyNullableFloats { get; set; }
}
[Fact]
public async Task NonNumber_AsBoxed_Property()
{
DateTime dateTime = DateTimeTestHelpers.FixedDateTimeValue;
Guid? nullableGuid = Guid.NewGuid();
string expected = @$"{{""MyDateTime"":{await Serializer.SerializeWrapper(dateTime)},""MyNullableGuid"":{await Serializer.SerializeWrapper(nullableGuid)}}}";
var obj = new Class_With_BoxedNonNumbers
{
MyDateTime = dateTime,
MyNullableGuid = nullableGuid
};
string serialized = await Serializer.SerializeWrapper(obj);
JsonTestHelper.AssertJsonEqual(expected, serialized);
obj = await Serializer.DeserializeWrapper<Class_With_BoxedNonNumbers>(serialized);
JsonElement el = Assert.IsType<JsonElement>(obj.MyDateTime);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal(dateTime, el.GetDateTime());
el = Assert.IsType<JsonElement>(obj.MyNullableGuid);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal(nullableGuid.Value, el.GetGuid());
}
public class Class_With_BoxedNonNumbers
{
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public object MyDateTime { get; set; }
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public object MyNullableGuid { get; set; }
}
[Fact]
public async Task NonNumber_AsBoxed_CollectionRootType_Element()
{
DateTime dateTime = DateTimeTestHelpers.FixedDateTimeValue;
Guid? nullableGuid = Guid.NewGuid();
string expected = @$"[{await Serializer.SerializeWrapper(dateTime)}]";
var obj = new List<object> { dateTime };
string serialized = await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr);
Assert.Equal(expected, serialized);
obj = await Serializer.DeserializeWrapper<List<object>>(serialized, s_optionReadAndWriteFromStr);
JsonElement el = Assert.IsType<JsonElement>(obj[0]);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal(dateTime, el.GetDateTime());
expected = @$"[{await Serializer.SerializeWrapper(nullableGuid)}]";
IList obj2 = new object[] { nullableGuid };
serialized = await Serializer.SerializeWrapper(obj2, s_optionReadAndWriteFromStr);
Assert.Equal(expected, serialized);
obj2 = await Serializer.DeserializeWrapper<IList>(serialized, s_optionReadAndWriteFromStr);
el = Assert.IsType<JsonElement>(obj2[0]);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal(nullableGuid.Value, el.GetGuid());
}
[Fact]
public async Task NonNumber_AsBoxed_CollectionProperty_Element()
{
DateTime dateTime = DateTimeTestHelpers.FixedDateTimeValue;
Guid? nullableGuid = Guid.NewGuid();
string expected = @$"{{""MyDateTimes"":[{await Serializer.SerializeWrapper(dateTime)}],""MyNullableGuids"":[{await Serializer.SerializeWrapper(nullableGuid)}]}}";
var obj = new Class_With_ListsOfBoxedNonNumbers
{
MyDateTimes = new List<object> { dateTime },
MyNullableGuids = new object[] { nullableGuid }
};
string serialized = await Serializer.SerializeWrapper(obj);
JsonTestHelper.AssertJsonEqual(expected, serialized);
obj = await Serializer.DeserializeWrapper<Class_With_ListsOfBoxedNonNumbers>(serialized);
JsonElement el = Assert.IsType<JsonElement>(obj.MyDateTimes[0]);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal(dateTime, el.GetDateTime());
el = Assert.IsType<JsonElement>(obj.MyNullableGuids[0]);
Assert.Equal(JsonValueKind.String, el.ValueKind);
Assert.Equal(nullableGuid, el.GetGuid());
}
public class Class_With_ListsOfBoxedNonNumbers
{
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public List<object> MyDateTimes { get; set; }
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public IList MyNullableGuids { get; set; }
}
[Fact]
public async Task Number_AsCollectionElement_RoundTrip()
{
await RunAsCollectionElementTest(JsonNumberTestData.Bytes);
await RunAsCollectionElementTest(JsonNumberTestData.SBytes);
await RunAsCollectionElementTest(JsonNumberTestData.Shorts);
await RunAsCollectionElementTest(JsonNumberTestData.Ints);
await RunAsCollectionElementTest(JsonNumberTestData.Longs);
await RunAsCollectionElementTest(JsonNumberTestData.UShorts);
await RunAsCollectionElementTest(JsonNumberTestData.UInts);
await RunAsCollectionElementTest(JsonNumberTestData.ULongs);
await RunAsCollectionElementTest(JsonNumberTestData.Floats);
await RunAsCollectionElementTest(JsonNumberTestData.Doubles);
await RunAsCollectionElementTest(JsonNumberTestData.Decimals);
#if NETCOREAPP
await RunAsCollectionElementTest(JsonNumberTestData.Int128s);
await RunAsCollectionElementTest(JsonNumberTestData.UInt128s);
await RunAsCollectionElementTest(JsonNumberTestData.Halfs);
#endif
// https://github.com/dotnet/runtime/issues/66220
if (!PlatformDetection.IsAppleMobile)
{
await RunAsCollectionElementTest(JsonNumberTestData.NullableBytes);
await RunAsCollectionElementTest(JsonNumberTestData.NullableSBytes);
await RunAsCollectionElementTest(JsonNumberTestData.NullableShorts);
await RunAsCollectionElementTest(JsonNumberTestData.NullableInts);
await RunAsCollectionElementTest(JsonNumberTestData.NullableLongs);
await RunAsCollectionElementTest(JsonNumberTestData.NullableUShorts);
await RunAsCollectionElementTest(JsonNumberTestData.NullableUInts);
await RunAsCollectionElementTest(JsonNumberTestData.NullableULongs);
await RunAsCollectionElementTest(JsonNumberTestData.NullableFloats);
await RunAsCollectionElementTest(JsonNumberTestData.NullableDoubles);
await RunAsCollectionElementTest(JsonNumberTestData.NullableDecimals);
#if NETCOREAPP
await RunAsCollectionElementTest(JsonNumberTestData.NullableInt128s);
await RunAsCollectionElementTest(JsonNumberTestData.NullableUInt128s);
await RunAsCollectionElementTest(JsonNumberTestData.NullableHalfs);
#endif
}
}
private async Task RunAsCollectionElementTest<T>(List<T> numbers)
{
StringBuilder jsonBuilder_NumbersAsNumbers = new StringBuilder();
StringBuilder jsonBuilder_NumbersAsStrings = new StringBuilder();
StringBuilder jsonBuilder_NumbersAsNumbersAndStrings = new StringBuilder();
StringBuilder jsonBuilder_NumbersAsNumbersAndStrings_Alternate = new StringBuilder();
bool asNumber = false;
jsonBuilder_NumbersAsNumbers.Append("[");
jsonBuilder_NumbersAsStrings.Append("[");
jsonBuilder_NumbersAsNumbersAndStrings.Append("[");
jsonBuilder_NumbersAsNumbersAndStrings_Alternate.Append("[");
foreach (T number in numbers)
{
string numberAsString = GetNumberAsString(number);
string jsonWithNumberAsString = @$"""{numberAsString}""";
jsonBuilder_NumbersAsNumbers.Append($"{numberAsString},");
jsonBuilder_NumbersAsStrings.Append($"{jsonWithNumberAsString},");
jsonBuilder_NumbersAsNumbersAndStrings.Append(asNumber
? $"{numberAsString},"
: $"{jsonWithNumberAsString},");
jsonBuilder_NumbersAsNumbersAndStrings_Alternate.Append(!asNumber
? $"{numberAsString},"
: $"{jsonWithNumberAsString},");
asNumber = !asNumber;
}
jsonBuilder_NumbersAsNumbers.Remove(jsonBuilder_NumbersAsNumbers.Length - 1, 1);
jsonBuilder_NumbersAsStrings.Remove(jsonBuilder_NumbersAsStrings.Length - 1, 1);
jsonBuilder_NumbersAsNumbersAndStrings.Remove(jsonBuilder_NumbersAsNumbersAndStrings.Length - 1, 1);
jsonBuilder_NumbersAsNumbersAndStrings_Alternate.Remove(jsonBuilder_NumbersAsNumbersAndStrings_Alternate.Length - 1, 1);
jsonBuilder_NumbersAsNumbers.Append("]");
jsonBuilder_NumbersAsStrings.Append("]");
jsonBuilder_NumbersAsNumbersAndStrings.Append("]");
jsonBuilder_NumbersAsNumbersAndStrings_Alternate.Append("]");
string jsonNumbersAsStrings = jsonBuilder_NumbersAsStrings.ToString();
await PerformAsCollectionElementSerialization(
numbers,
jsonBuilder_NumbersAsNumbers.ToString(),
jsonNumbersAsStrings,
jsonBuilder_NumbersAsNumbersAndStrings.ToString(),
jsonBuilder_NumbersAsNumbersAndStrings_Alternate.ToString());
// Reflection based tests for every collection type.
await RunAllCollectionsRoundTripTest<T>(jsonNumbersAsStrings);
}
private async Task PerformAsCollectionElementSerialization<T>(
List<T> numbers,
string json_NumbersAsNumbers,
string json_NumbersAsStrings,
string json_NumbersAsNumbersAndStrings,
string json_NumbersAsNumbersAndStrings_Alternate)
{
List<T> deserialized;
// Option: read from string
// Deserialize
deserialized = await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsNumbers, s_optionReadFromStr);
AssertIEnumerableEqual(numbers, deserialized);
deserialized = await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsStrings, s_optionReadFromStr);
AssertIEnumerableEqual(numbers, deserialized);
deserialized = await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsNumbersAndStrings, s_optionReadFromStr);
AssertIEnumerableEqual(numbers, deserialized);
deserialized = await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsNumbersAndStrings_Alternate, s_optionReadFromStr);
AssertIEnumerableEqual(numbers, deserialized);
// Serialize
Assert.Equal(json_NumbersAsNumbers, await Serializer.SerializeWrapper(numbers, s_optionReadFromStr));
// Option: write as string
// Deserialize
deserialized = await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsNumbers, s_optionWriteAsStr);
AssertIEnumerableEqual(numbers, deserialized);
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsStrings, s_optionWriteAsStr));
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsNumbersAndStrings, s_optionWriteAsStr));
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsNumbersAndStrings_Alternate, s_optionWriteAsStr));
// Serialize
Assert.Equal(json_NumbersAsStrings, await Serializer.SerializeWrapper(numbers, s_optionWriteAsStr));
// Option: read and write from/to string
// Deserialize
deserialized = await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsNumbers, s_optionReadAndWriteFromStr);
AssertIEnumerableEqual(numbers, deserialized);
deserialized = await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsStrings, s_optionReadAndWriteFromStr);
AssertIEnumerableEqual(numbers, deserialized);
deserialized = await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsNumbersAndStrings, s_optionReadAndWriteFromStr);
AssertIEnumerableEqual(numbers, deserialized);
deserialized = await Serializer.DeserializeWrapper<List<T>>(json_NumbersAsNumbersAndStrings_Alternate, s_optionReadAndWriteFromStr);
AssertIEnumerableEqual(numbers, deserialized);
// Serialize
Assert.Equal(json_NumbersAsStrings, await Serializer.SerializeWrapper(numbers, s_optionReadAndWriteFromStr));
}
private void AssertIEnumerableEqual<T>(IEnumerable<T> list1, IEnumerable<T> list2)
{
IEnumerator<T> enumerator1 = list1.GetEnumerator();
IEnumerator<T> enumerator2 = list2.GetEnumerator();
while (enumerator1.MoveNext())
{
enumerator2.MoveNext();
Assert.Equal(enumerator1.Current, enumerator2.Current);
}
Assert.False(enumerator2.MoveNext());
}
private async Task RunAllCollectionsRoundTripTest<T>(string json)
{
foreach (Type type in CollectionTestTypes.DeserializableGenericEnumerableTypes<T>())
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(HashSet<>))
{
HashSet<T> obj1 = (HashSet<T>)await Serializer.DeserializeWrapper(json, type, s_optionReadAndWriteFromStr);
string serialized = await Serializer.SerializeWrapper(obj1, s_optionReadAndWriteFromStr);
HashSet<T> obj2 = (HashSet<T>)await Serializer.DeserializeWrapper(serialized, type, s_optionReadAndWriteFromStr);
Assert.Equal(obj1.Count, obj2.Count);
foreach (T element in obj1)
{
Assert.True(obj2.Contains(element));
}
}
else if (type != typeof(byte[]))
{
object obj = await Serializer.DeserializeWrapper(json, type, s_optionReadAndWriteFromStr);
string serialized = await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr);
Assert.Equal(json, serialized);
}
}
foreach (Type type in CollectionTestTypes.DeserializableNonGenericEnumerableTypes())
{
// Deserialized as collection of JsonElements.
object obj = await Serializer.DeserializeWrapper(json, type, s_optionReadAndWriteFromStr);
// Serialized as strings with escaping.
string serialized = await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr);
// Ensure escaped values were serialized accurately
List<T> list = await Serializer.DeserializeWrapper<List<T>>(serialized, s_optionReadAndWriteFromStr);
serialized = await Serializer.SerializeWrapper(list, s_optionReadAndWriteFromStr);
Assert.Equal(json, serialized);
// Serialize instance which is a collection of numbers (not JsonElements).
obj = Activator.CreateInstance(type, new[] { list });
serialized = await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr);
Assert.Equal(json, serialized);
}
}
[Fact]
public async Task Number_AsDictionaryElement_RoundTrip()
{
var dict = new Dictionary<int, float>();
for (int i = 0; i < 10; i++)
{
dict[JsonNumberTestData.Ints[i]] = JsonNumberTestData.Floats[i];
}
// Serialize
string serialized = await Serializer.SerializeWrapper(dict, s_optionReadAndWriteFromStr);
AssertDictionaryElements_StringValues(serialized);
// Deserialize
dict = await Serializer.DeserializeWrapper<Dictionary<int, float>>(serialized, s_optionReadAndWriteFromStr);
// Test roundtrip
JsonTestHelper.AssertJsonEqual(serialized, await Serializer.SerializeWrapper(dict, s_optionReadAndWriteFromStr));
}
private void AssertDictionaryElements_StringValues(string serialized)
{
Utf8JsonReader reader = new(Encoding.UTF8.GetBytes(serialized));
reader.Read();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
else if (reader.TokenType == JsonTokenType.String)
{
Assert.True(reader.ValueSpan.IndexOf((byte)'\\') == -1);
}
else
{
Assert.Equal(JsonTokenType.PropertyName, reader.TokenType);
}
}
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/39674", typeof(PlatformDetection), nameof(PlatformDetection.IsMonoInterpreter))]
[SkipOnCoreClr("https://github.com/dotnet/runtime/issues/45464", ~RuntimeConfiguration.Release)]
public async Task DictionariesRoundTrip()
{
await RunAllDictionariessRoundTripTest(JsonNumberTestData.ULongs);
await RunAllDictionariessRoundTripTest(JsonNumberTestData.Floats);
await RunAllDictionariessRoundTripTest(JsonNumberTestData.Doubles);
}
private async Task RunAllDictionariessRoundTripTest<T>(List<T> numbers)
{
StringBuilder jsonBuilder_NumbersAsStrings = new StringBuilder();
jsonBuilder_NumbersAsStrings.Append("{");
foreach (T number in numbers)
{
string numberAsString = GetNumberAsString(number);
string jsonWithNumberAsString = @$"""{numberAsString}""";
jsonBuilder_NumbersAsStrings.Append($"{jsonWithNumberAsString}:");
jsonBuilder_NumbersAsStrings.Append($"{jsonWithNumberAsString},");
}
jsonBuilder_NumbersAsStrings.Remove(jsonBuilder_NumbersAsStrings.Length - 1, 1);
jsonBuilder_NumbersAsStrings.Append("}");
string jsonNumbersAsStrings = jsonBuilder_NumbersAsStrings.ToString();
foreach (Type type in CollectionTestTypes.DeserializableDictionaryTypes<string, T>())
{
object obj = await Serializer.DeserializeWrapper(jsonNumbersAsStrings, type, s_optionReadAndWriteFromStr);
JsonTestHelper.AssertJsonEqual(jsonNumbersAsStrings, await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr));
}
foreach (Type type in CollectionTestTypes.DeserializableNonGenericDictionaryTypes())
{
Dictionary<T, T> dict = await Serializer.DeserializeWrapper<Dictionary<T, T>>(jsonNumbersAsStrings, s_optionReadAndWriteFromStr);
// Serialize instance which is a dictionary of numbers (not JsonElements).
object obj = Activator.CreateInstance(type, new[] { dict });
string serialized = await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr);
JsonTestHelper.AssertJsonEqual(jsonNumbersAsStrings, serialized);
}
}
[Fact]
public async Task Number_AsPropertyValue_RoundTrip()
{
var obj = new Class_With_NullableUInt64_And_Float()
{
NullableUInt64Number = JsonNumberTestData.NullableULongs.LastOrDefault(),
FloatNumbers = JsonNumberTestData.Floats
};
// Serialize
string serialized = await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr);
// Deserialize
obj = await Serializer.DeserializeWrapper<Class_With_NullableUInt64_And_Float>(serialized, s_optionReadAndWriteFromStr);
// Test roundtrip
JsonTestHelper.AssertJsonEqual(serialized, await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr));
}
public class Class_With_NullableUInt64_And_Float
{
public ulong? NullableUInt64Number { get; set; }
[JsonInclude]
public List<float> FloatNumbers;
}
[Fact]
public async Task Number_AsKeyValuePairValue_RoundTrip()
{
var obj = new KeyValuePair<ulong?, List<float>>(JsonNumberTestData.NullableULongs.LastOrDefault(), JsonNumberTestData.Floats);
// Serialize
string serialized = await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr);
// Deserialize
obj = await Serializer.DeserializeWrapper<KeyValuePair<ulong?, List<float>>>(serialized, s_optionReadAndWriteFromStr);
// Test roundtrip
JsonTestHelper.AssertJsonEqual(serialized, await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr));
}
[Fact]
public async Task Number_AsObjectWithParameterizedCtor_RoundTrip()
{
var obj = new MyClassWithNumbers(JsonNumberTestData.NullableULongs.LastOrDefault(), JsonNumberTestData.Floats);
// Serialize
string serialized = await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr);
// Deserialize
obj = await Serializer.DeserializeWrapper<MyClassWithNumbers>(serialized, s_optionReadAndWriteFromStr);
// Test roundtrip
JsonTestHelper.AssertJsonEqual(serialized, await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr));
}
public class MyClassWithNumbers
{
public ulong? Ulong { get; }
public List<float> ListOfFloats { get; }
public MyClassWithNumbers(ulong? @ulong, List<float> listOfFloats)
{
Ulong = @ulong;
ListOfFloats = listOfFloats;
}
}
[Fact]
public async Task Number_AsObjectWithParameterizedCtor_PropHasAttribute()
{
string json = @"{""ListOfFloats"":[""1""]}";
// Strict handling on property overrides loose global policy.
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<MyClassWithNumbers_PropsHasAttribute>(json, s_optionReadFromStr));
// Serialize
json = @"{""ListOfFloats"":[1]}";
MyClassWithNumbers_PropsHasAttribute obj = await Serializer.DeserializeWrapper<MyClassWithNumbers_PropsHasAttribute>(json);
// Number serialized as JSON number due to strict handling on property which overrides loose global policy.
Assert.Equal(json, await Serializer.SerializeWrapper(obj, s_optionReadAndWriteFromStr));
}
public class MyClassWithNumbers_PropsHasAttribute
{
[JsonNumberHandling(JsonNumberHandling.Strict)]
public List<float> ListOfFloats { get; }
public MyClassWithNumbers_PropsHasAttribute(List<float> listOfFloats)
{
ListOfFloats = listOfFloats;
}
}
[Fact]
public async Task FloatingPointConstants_Pass()
{
// Valid values
await PerformFloatingPointSerialization("NaN");
await PerformFloatingPointSerialization("Infinity");
await PerformFloatingPointSerialization("-Infinity");
await PerformFloatingPointSerialization("\u004EaN"); // NaN
await PerformFloatingPointSerialization("Inf\u0069ni\u0074y"); // Infinity
await PerformFloatingPointSerialization("\u002DInf\u0069nity"); // -Infinity
async Task PerformFloatingPointSerialization(string testString)
{
string testStringAsJson = $@"""{testString}""";
#if NETCOREAPP
string testJson = @$"{{""HalfNumber"":{testStringAsJson},""FloatNumber"":{testStringAsJson},""DoubleNumber"":{testStringAsJson}}}";
#else
string testJson = @$"{{""FloatNumber"":{testStringAsJson},""DoubleNumber"":{testStringAsJson}}}";
#endif
StructWithNumbers obj;
switch (testString)
{
case "NaN":
obj = await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionsAllowFloatConstants);
#if NETCOREAPP
Assert.Equal(Half.NaN, obj.HalfNumber);
#endif
Assert.Equal(float.NaN, obj.FloatNumber);
Assert.Equal(double.NaN, obj.DoubleNumber);
obj = await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionReadFromStr);
#if NETCOREAPP
Assert.Equal(Half.NaN, obj.HalfNumber);
#endif
Assert.Equal(float.NaN, obj.FloatNumber);
Assert.Equal(double.NaN, obj.DoubleNumber);
break;
case "Infinity":
obj = await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionsAllowFloatConstants);
#if NETCOREAPP
Assert.Equal(Half.PositiveInfinity, obj.HalfNumber);
#endif
Assert.Equal(float.PositiveInfinity, obj.FloatNumber);
Assert.Equal(double.PositiveInfinity, obj.DoubleNumber);
obj = await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionReadFromStr);
#if NETCOREAPP
Assert.Equal(Half.PositiveInfinity, obj.HalfNumber);
#endif
Assert.Equal(float.PositiveInfinity, obj.FloatNumber);
Assert.Equal(double.PositiveInfinity, obj.DoubleNumber);
break;
case "-Infinity":
obj = await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionsAllowFloatConstants);
#if NETCOREAPP
Assert.Equal(Half.NegativeInfinity, obj.HalfNumber);
#endif
Assert.Equal(float.NegativeInfinity, obj.FloatNumber);
Assert.Equal(double.NegativeInfinity, obj.DoubleNumber);
obj = await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionReadFromStr);
#if NETCOREAPP
Assert.Equal(Half.NegativeInfinity, obj.HalfNumber);
#endif
Assert.Equal(float.NegativeInfinity, obj.FloatNumber);
Assert.Equal(double.NegativeInfinity, obj.DoubleNumber);
break;
default:
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionsAllowFloatConstants));
return;
}
JsonTestHelper.AssertJsonEqual(testJson, await Serializer.SerializeWrapper(obj, s_optionsAllowFloatConstants));
JsonTestHelper.AssertJsonEqual(testJson, await Serializer.SerializeWrapper(obj, s_optionWriteAsStr));
}
}
[Theory]
[InlineData("naN")]
[InlineData("Nan")]
[InlineData("NAN")]
[InlineData("+Infinity")]
[InlineData("+infinity")]
[InlineData("infinity")]
[InlineData("infinitY")]
[InlineData("INFINITY")]
[InlineData("+INFINITY")]
[InlineData("-infinity")]
[InlineData("-infinitY")]
[InlineData("-INFINITY")]
[InlineData(" NaN")]
[InlineData("NaN ")]
[InlineData(" Infinity")]
[InlineData(" -Infinity")]
[InlineData("Infinity ")]
[InlineData("-Infinity ")]
[InlineData("a-Infinity")]
[InlineData("NaNa")]
[InlineData("Infinitya")]
[InlineData("-Infinitya")]
#pragma warning disable xUnit1025 // Theory method 'FloatingPointConstants_Fail' on test class 'NumberHandlingTests' has InlineData duplicate(s)
[InlineData("\u006EaN")] // "naN"
[InlineData("\u0020Inf\u0069ni\u0074y")] // " Infinity"
[InlineData("\u002BInf\u0069nity")] // "+Infinity"
#pragma warning restore xUnit1025
public async Task FloatingPointConstants_Fail(string testString)
{
string testStringAsJson = $@"""{testString}""";
string testJson;
#if NETCOREAPP
testJson = @$"{{""HalfNumber"":{testStringAsJson}}}";
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionsAllowFloatConstants));
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionReadFromStr));
#endif
testJson = @$"{{""FloatNumber"":{testStringAsJson}}}";
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionsAllowFloatConstants));
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionReadFromStr));
testJson = @$"{{""DoubleNumber"":{testStringAsJson}}}";
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionsAllowFloatConstants));
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<StructWithNumbers>(testJson, s_optionReadFromStr));
}
[Fact]
public async Task AllowFloatingPointConstants_WriteAsNumber_IfNotConstant()
{
#if NETCOREAPP
Half half = (Half)1;
// Not written as "1"
Assert.Equal("1", await Serializer.SerializeWrapper(half, s_optionsAllowFloatConstants));
#endif
float @float = 1;
// Not written as "1"
Assert.Equal("1", await Serializer.SerializeWrapper(@float, s_optionsAllowFloatConstants));
double @double = 1;
// Not written as "1"
Assert.Equal("1", await Serializer.SerializeWrapper(@double, s_optionsAllowFloatConstants));
}
[Theory]
[InlineData("NaN")]
[InlineData("Infinity")]
[InlineData("-Infinity")]
public async Task Unquoted_FloatingPointConstants_Read_Fail(string testString)
{
#if NETCOREAPP
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<Half>(testString, s_optionsAllowFloatConstants));
#endif
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<float>(testString, s_optionsAllowFloatConstants));
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<double?>(testString, s_optionReadFromStr));
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<double>(testString, s_optionReadFromStrAllowFloatConstants));
}
public struct StructWithNumbers
{
#if NETCOREAPP
public Half HalfNumber { get; set; }
#endif
public float FloatNumber { get; set; }
public double DoubleNumber { get; set; }
}
[Fact]
public async Task ReadFromString_AllowFloatingPoint()
{
string json = @"{""IntNumber"":""1"",""FloatNumber"":""NaN""}";
ClassWithNumbers obj = await Serializer.DeserializeWrapper<ClassWithNumbers>(json, s_optionReadFromStrAllowFloatConstants);
Assert.Equal(1, obj.IntNumber);
Assert.Equal(float.NaN, obj.FloatNumber);
JsonTestHelper.AssertJsonEqual(@"{""IntNumber"":1,""FloatNumber"":""NaN""}", await Serializer.SerializeWrapper(obj, s_optionReadFromStrAllowFloatConstants));
}
[Fact]
public async Task WriteAsString_AllowFloatingPoint()
{
string json = @"{""IntNumber"":""1"",""FloatNumber"":""NaN""}";
await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<ClassWithNumbers>(json, s_optionWriteAsStrAllowFloatConstants));
var obj = new ClassWithNumbers
{
IntNumber = 1,
FloatNumber = float.NaN
};
JsonTestHelper.AssertJsonEqual(json, await Serializer.SerializeWrapper(obj, s_optionWriteAsStrAllowFloatConstants));
}
public class ClassWithNumbers
{
public int IntNumber { get; set; }
public float FloatNumber { get; set; }
}
[Fact]
public async Task FloatingPointConstants_IncompatibleNumber()
{
await AssertFloatingPointIncompatible_Fails<byte>();
await AssertFloatingPointIncompatible_Fails<sbyte>();
await AssertFloatingPointIncompatible_Fails<short>();
await AssertFloatingPointIncompatible_Fails<int>();
await AssertFloatingPointIncompatible_Fails<long>();
await AssertFloatingPointIncompatible_Fails<ushort>();
await AssertFloatingPointIncompatible_Fails<uint>();
await AssertFloatingPointIncompatible_Fails<ulong>();
await AssertFloatingPointIncompatible_Fails<decimal>();
await AssertFloatingPointIncompatible_Fails<byte?>();
await AssertFloatingPointIncompatible_Fails<sbyte?>();
await AssertFloatingPointIncompatible_Fails<short?>();
await AssertFloatingPointIncompatible_Fails<int?>();
await AssertFloatingPointIncompatible_Fails<long?>();
await AssertFloatingPointIncompatible_Fails<ushort?>();
await AssertFloatingPointIncompatible_Fails<uint?>();
await AssertFloatingPointIncompatible_Fails<ulong?>();
await AssertFloatingPointIncompatible_Fails<decimal?>();
#if NETCOREAPP
await AssertFloatingPointIncompatible_Fails<Int128>();
await AssertFloatingPointIncompatible_Fails<UInt128>();
await AssertFloatingPointIncompatible_Fails<Int128?>();
await AssertFloatingPointIncompatible_Fails<UInt128?>();
#endif
}
private async Task AssertFloatingPointIncompatible_Fails<T>()
{
string[] testCases = new[]
{
@"""NaN""",