-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathJsonValue.fs
More file actions
861 lines (750 loc) · 40.8 KB
/
JsonValue.fs
File metadata and controls
861 lines (750 loc) · 40.8 KB
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
module FSharp.Data.Tests.JsonValue
open NUnit.Framework
open System
open System.Globalization
open System.Threading
open FSharp.Data
open FSharp.Data.JsonExtensions
open FsUnit
[<Test>]
let ``Can parse empty document``() =
let j = JsonValue.Parse "{}"
j |> should equal (JsonValue.Record [| |])
[<Test>]
let ``Can parse document with single property``() =
let j = JsonValue.Parse "{\"firstName\": \"John\"}"
j?firstName.AsString() |> should equal "John"
[<Test>]
let ``Can parse document with text and integer``() =
let j = JsonValue.Parse "{\"firstName\": \"John\", \"lastName\": \"Smith\", \"age\": 25}"
j?firstName.AsString() |> should equal "John"
j?lastName.AsString() |> should equal "Smith"
j?age.AsInteger() |> should equal 25
[<Test>]
let ``Can parse document with text and float``() =
let j = JsonValue.Parse "{\"firstName\": \"John\", \"lastName\": \"Smith\", \"age\": 25.25}"
j?age.AsFloat() |> should equal 25.25
[<Test>]
let ``Can parse document with date``() =
let j = JsonValue.Parse "{\"anniversary\": \"\\/Date(869080830450)\\/\"}"
j?anniversary.AsDateTime() |> should equal (new DateTime(1997, 07, 16, 19, 20, 30, 450, DateTimeKind.Utc))
j?anniversary.AsDateTime().Kind |> should equal DateTimeKind.Utc
[<Test>]
let ``Can parse document with iso date``() =
let j = JsonValue.Parse "{\"anniversary\": \"2009-05-19 14:39:22.500\"}"
j?anniversary.AsDateTime() |> should equal (new DateTime(2009, 05, 19, 14, 39, 22, 500, DateTimeKind.Local))
j?anniversary.AsDateTime().Kind |> should equal DateTimeKind.Local
let withCulture (cultureName: string) =
let originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture <- CultureInfo cultureName
{ new IDisposable with
member _.Dispose() =
CultureInfo.CurrentCulture <- originalCulture }
[<Test>]
let ``Can parse document with iso date in local culture``() =
use _holder = withCulture "zh-CN"
let j = JsonValue.Parse "{\"anniversary\": \"2009-05-19 14:39:22.500\"}"
j?anniversary.AsDateTime() |> should equal (new DateTime(2009, 05, 19, 14, 39, 22, 500, DateTimeKind.Local))
j?anniversary.AsDateTime().Kind |> should equal DateTimeKind.Local
[<Test>]
let ``Can parse document with partial iso date``() =
let j = JsonValue.Parse "{\"anniversary\": \"2009-05-19\"}"
j?anniversary.AsDateTime() |> should equal (new DateTime(2009, 05, 19, 0, 0, 0, DateTimeKind.Local))
j?anniversary.AsDateTime().Kind |> should equal DateTimeKind.Local
[<Test>]
let ``Can parse document with timezone iso date``() =
let j = JsonValue.Parse "{\"anniversary\": \"2009-05-19 14:39:22+0600\"}"
j?anniversary.AsDateTime().ToUniversalTime() |> should equal (new DateTime(2009, 05, 19, 8, 39, 22, DateTimeKind.Utc))
[<Test>]
let ``Can parse document with UTC iso date``() =
let j = JsonValue.Parse "{\"anniversary\": \"2009-05-19 14:39:22Z\"}"
j?anniversary.AsDateTime().ToUniversalTime() |> should equal (new DateTime(2009, 05, 19, 14, 39, 22, DateTimeKind.Utc))
j?anniversary.AsDateTime().Kind |> should equal DateTimeKind.Utc
[<Test>]
let ``Can parse document with timezone and fraction iso date``() =
let j = JsonValue.Parse "{\"anniversary\": \"1997-07-16T19:20:30.45+01:00\"}"
j?anniversary.AsDateTime().ToUniversalTime() |> should equal (new DateTime(1997, 07, 16, 18, 20, 30, 450, DateTimeKind.Utc))
[<Test>]
let ``Can parse document with datetime offset as ticks and timezones`` () =
let j = JsonValue.Parse "{\"anniversary\": \"\\/Date(869080830450+1000)\\/\"}"
j?anniversary.AsDateTimeOffset() |> should equal <| DateTimeOffset (1997, 07, 16, 19, 20, 30, 450, TimeSpan.FromHours 10.)
[<Test>]
let ``Can parse document with datetime offset from iso date format``() =
let j = JsonValue.Parse "{\"anniversary\": \"2009-05-19 14:39:22+0600\"}"
j?anniversary.AsDateTimeOffset() |> should equal <| DateTimeOffset(2009, 05, 19, 14, 39, 22, TimeSpan.FromHours 6.)
[<Test; Explicit>]
let ``Can parse deep arrays``() =
String.replicate 50000 "[" + String.replicate 50000 "]"
|> FSharp.Data.JsonValue.Parse
|> ignore
[<Test; Explicit>]
let ``Can parse deep objects``() =
(String.replicate 50000 "{\"a\":") + "\"\"" + (String.replicate 50000 "}")
|> FSharp.Data.JsonValue.Parse
|> ignore
// TODO: Due to limitations in the current ISO 8601 datetime parsing these fail, and should be made to pass
//[<Test>]
//let ``Cant Yet parse document with basic iso date``() =
// let j = JsonValue.Parse "{\"anniversary\": \"19810405\"}"
// j?anniversary.AsDateTime |> should equal (new DateTime(1981, 04, 05))
//
//[<Test>]
//let ``Cant Yet parse weird iso date``() =
// let j = JsonValue.Parse "{\"anniversary\": \"2010-02-18T16.5\"}"
// j?anniversary.AsDateTime |> should equal (new DateTime(2010, 02, 18, 16, 30, 00))
[<Test>]
let ``Can parse completely invalid, but close, date as string``() =
let j = JsonValue.Parse "{\"anniversary\": \"2010-02-18T16.5:23.35:4\"}"
(fun () -> j?anniversary.AsDateTime() |> ignore) |> should throw typeof<Exception>
j?anniversary.AsString() |> should equal "2010-02-18T16.5:23.35:4"
[<Test>]
let ``Can parse positive time span with days and fraction``() =
let j = JsonValue.Parse "{\"duration\": \"1:3:16:50.5\"}"
j?duration.AsTimeSpan() |> should equal (TimeSpan(1, 3, 16, 50, 500))
[<Test>]
let ``Can parse positive time span without days and without fraction``() =
let j = JsonValue.Parse "{\"duration\": \"00:30:00\"}"
j?duration.AsTimeSpan() |> should equal (TimeSpan(0, 30, 0))
[<Test>]
let ``Can parse negative time span with days and fraction``() =
let j = JsonValue.Parse "{\"duration\": \"-1:3:16:50.5\"}"
j?duration.AsTimeSpan() |> should equal (TimeSpan(-1, -3, -16, -50, -500))
[<Test>]
let ``Can parse time span in different culture``() =
use _holder = withCulture "fr"
let j = JsonValue.Parse("{\"duration\": \"1:3:16:50,5\"}")
j?duration.AsTimeSpan CultureInfo.CurrentCulture |> should equal (TimeSpan(1, 3, 16, 50, 500))
[<Test>]
let ``Can parse UTF-32 unicode characters`` () =
let j = JsonValue.Parse """{ "value": "\U00010343\U00010330\U0001033F\U00010339\U0001033B" }"""
j?value.AsString() |> should equal "\U00010343\U00010330\U0001033F\U00010339\U0001033B"
[<Test>]
let ``Can parse floats in different cultures``() =
use _holder = withCulture "pt-PT"
let j = JsonValue.Parse "{ \"age\": 25.5}"
j?age.AsFloat() |> should equal 25.5
let j = JsonValue.Parse "{ \"age\": \"25.5\"}"
j?age.AsFloat() |> should equal 25.5
let j = JsonValue.TryParse("{ \"age\": 25,5}")
j |> should equal None
let j = JsonValue.Parse("{ \"age\": \"25,5\"}")
j?age.AsFloat(CultureInfo.CurrentCulture) |> should equal 25.5
[<Test>]
let ``Can parse decimals in different cultures``() =
use _holder = withCulture "pt-PT"
let j = JsonValue.Parse "{ \"age\": 25.5}"
j?age.AsDecimal() |> should equal 25.5m
let j = JsonValue.Parse "{ \"age\": \"25.5\"}"
j?age.AsDecimal() |> should equal 25.5m
let j = JsonValue.TryParse("{ \"age\": 25,5}")
j |> should equal None
let j = JsonValue.Parse("{ \"age\": \"25,5\"}")
j?age.AsDecimal(CultureInfo.CurrentCulture) |> should equal 25.5m
[<Test>]
let ``Can parse dates in different cultures``() =
use _holder = withCulture "pt-PT"
let j = JsonValue.Parse "{ \"birthdate\": \"01/02/2000\"}"
j?birthdate.AsDateTime().Month |> should equal 1
let j = JsonValue.Parse("{ \"birthdate\": \"01/02/2000\"}")
j?birthdate.AsDateTime(CultureInfo.CurrentCulture).Month |> should equal 2
[<Test>]
let ``Can parse nested document`` () =
let j = JsonValue.Parse "{ \"main\": { \"title\": \"example\", \"nested\": { \"nestedTitle\": \"sub\" } } }"
let main = j?main
main?title.AsString() |> should equal "example"
let nested = main?nested
nested?nestedTitle.AsString() |> should equal "sub"
[<Test>]
let ``Can parse document with booleans``() =
let j = JsonValue.Parse "{ \"hasTrue\": true, \"hasFalse\": false }"
j?hasTrue.AsBoolean() |> should equal true
j?hasFalse.AsBoolean() |> should equal false
[<Test>]
let ``Can parse document with guids``() =
let j = JsonValue.Parse "{ \"id\": \"{F842213A-82FB-4EEB-AB75-7CCD18676FD5}\" }"
j?id.AsGuid() |> should equal (Guid.Parse "F842213A-82FB-4EEB-AB75-7CCD18676FD5")
[<Test>]
let ``Can parse document with null``() =
let j = JsonValue.Parse "{ \"items\": [{\"id\": \"Open\"}, null, {\"id\": \"Pause\"}] }"
let jArray = j?items
jArray.[0]?id.AsString() |> should equal "Open"
jArray.[1] |> should equal JsonValue.Null
jArray.[2]?id.AsString() |> should equal "Pause"
[<Test>]
let ``Can parse array in outermost scope``() =
let jArray = JsonValue.Parse "[{\"id\": \"Open\"}, null, {\"id\": \"Pause\"}]"
jArray.[0]?id.AsString() |> should equal "Open"
jArray.[1] |> should equal JsonValue.Null
jArray.[2]?id.AsString() |> should equal "Pause"
[<Test>]
let ``Can parse a string from twitter api without throwing an error``() =
let text =
"[{\"in_reply_to_status_id_str\":\"115445959386861568\",\"truncated\":false,\"in_reply_to_user_id_str\":\"40453522\",\"geo\":null,\"retweet_count\":0,\"contributors\":null,\"coordinates\":null,\"user\":{\"default_profile\":false,\"statuses_count\":3638,\"favourites_count\":28,\"protected\":false,\"profile_text_color\":\"634047\",\"profile_image_url\":\"http:\\/\\/a3.twimg.com\\/profile_images\\/1280550984\\/buddy_lueneburg_normal.jpg\",\"name\":\"Steffen Forkmann\",\"profile_sidebar_fill_color\":\"E3E2DE\",\"listed_count\":46,\"following\":true,\"profile_background_tile\":false,\"utc_offset\":3600,\"description\":\"C#, F# and Dynamics NAV developer, blogger and sometimes speaker. Creator of FAKE - F# Make and NaturalSpec.\",\"location\":\"Hamburg \\/ Germany\",\"contributors_enabled\":false,\"verified\":false,\"profile_link_color\":\"088253\",\"followers_count\":471,\"url\":\"http:\\/\\/www.navision-blog.de\\/blog-mitglieder\\/steffen-forkmann-ueber-mich\\/\",\"profile_sidebar_border_color\":\"D3D2CF\",\"screen_name\":\"sforkmann\",\"default_profile_image\":false,\"notifications\":false,\"show_all_inline_media\":false,\"geo_enabled\":true,\"profile_use_background_image\":true,\"friends_count\":373,\"id_str\":\"22477880\",\"is_translator\":false,\"lang\":\"en\",\"time_zone\":\"Berlin\",\"created_at\":\"Mon Mar 02 12:04:39 +0000 2009\",\"profile_background_color\":\"EDECE9\",\"id\":22477880,\"follow_request_sent\":false,\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/images\\/themes\\/theme3\\/bg.gif\",\"profile_background_image_url\":\"http:\\/\\/a1.twimg.com\\/images\\/themes\\/theme3\\/bg.gif\",\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1280550984\\/buddy_lueneburg_normal.jpg\"},\"favorited\":false,\"in_reply_to_screen_name\":\"ovatsus\",\"source\":\"\\u003Ca href=\\\"http:\\/\\/www.tweetdeck.com\\\" rel=\\\"nofollow\\\"\\u003ETweetDeck\\u003C\\/a\\u003E\",\"id_str\":\"115447331628916736\",\"in_reply_to_status_id\":115445959386861568,\"id\":115447331628916736,\"created_at\":\"Sun Sep 18 15:29:23 +0000 2011\",\"place\":null,\"retweeted\":false,\"in_reply_to_user_id\":40453522,\"text\":\"@ovatsus I know it's not complete. But I don't want to add a dependency on FParsec in #FSharp.Data. Can you send me samples where it fails?\"},{\"in_reply_to_status_id_str\":null,\"truncated\":false,\"in_reply_to_user_id_str\":null,\"geo\":null,\"retweet_count\":0,\"contributors\":null,\"coordinates\":null,\"user\":{\"statuses_count\":3637,\"favourites_count\":28,\"protected\":false,\"profile_text_color\":\"634047\",\"profile_image_url\":\"http:\\/\\/a3.twimg.com\\/profile_images\\/1280550984\\/buddy_lueneburg_normal.jpg\",\"name\":\"Steffen Forkmann\",\"profile_sidebar_fill_color\":\"E3E2DE\",\"listed_count\":46,\"following\":true,\"profile_background_tile\":false,\"utc_offset\":3600,\"description\":\"C#, F# and Dynamics NAV developer, blogger and sometimes speaker. Creator of FAKE - F# Make and NaturalSpec.\",\"location\":\"Hamburg \\/ Germany\",\"contributors_enabled\":false,\"verified\":false,\"profile_link_color\":\"088253\",\"followers_count\":471,\"url\":\"http:\\/\\/www.navision-blog.de\\/blog-mitglieder\\/steffen-forkmann-ueber-mich\\/\",\"profile_sidebar_border_color\":\"D3D2CF\",\"screen_name\":\"sforkmann\",\"default_profile_image\":false,\"notifications\":false,\"show_all_inline_media\":false,\"geo_enabled\":true,\"profile_use_background_image\":true,\"friends_count\":372,\"id_str\":\"22477880\",\"is_translator\":false,\"lang\":\"en\",\"time_zone\":\"Berlin\",\"created_at\":\"Mon Mar 02 12:04:39 +0000 2009\",\"profile_background_color\":\"EDECE9\",\"id\":22477880,\"default_profile\":false,\"follow_request_sent\":false,\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/images\\/themes\\/theme3\\/bg.gif\",\"profile_background_image_url\":\"http:\\/\\/a1.twimg.com\\/images\\/themes\\/theme3\\/bg.gif\",\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1280550984\\/buddy_lueneburg_normal.jpg\"},\"favorited\":false,\"in_reply_to_screen_name\":null,\"source\":\"\\u003Ca href=\\\"http:\\/\\/www.tweetdeck.com\\\" rel=\\\"nofollow\\\"\\u003ETweetDeck\\u003C\\/a\\u003E\",\"id_str\":\"115444490331889664\",\"in_reply_to_status_id\":null,\"id\":115444490331889664,\"created_at\":\"Sun Sep 18 15:18:06 +0000 2011\",\"possibly_sensitive\":false,\"place\":null,\"retweeted\":false,\"in_reply_to_user_id\":null,\"text\":\"Added a simple Json parser to #FSharp.Data http:\\/\\/t.co\\/3JGI56SM - #fsharp\"}]"
JsonValue.Parse text |> ignore
[<Test>]
let ``Can parse array of numbers``() =
let j = JsonValue.Parse "[1, 2, 3]"
j.[0] |> should equal (JsonValue.Number 1m)
j.[1] |> should equal (JsonValue.Number 2m)
j.[2] |> should equal (JsonValue.Number 3m)
[<Test>]
let ``Can parse array of numbers when culture is using comma as decimal separator``() =
use _holder = withCulture "de-DE"
CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator |> should equal ","
let j = JsonValue.Parse("[25,5,5,25]")
j.[0] |> should equal (JsonValue.Number 25m)
j.[1] |> should equal (JsonValue.Number 5m)
j.[2] |> should equal (JsonValue.Number 5m)
j.[3] |> should equal (JsonValue.Number 25m)
[<Test>]
let ``Quotes in strings are properly escaped``() =
let jsonStr = "{\"short_description\":\"This a string with \\\"quotes\\\"\"}"
let j = JsonValue.Parse jsonStr
j.ToString(JsonSaveOptions.DisableFormatting) |> should equal jsonStr
[<Test>]
let ``Can parse simple array``() =
let j = JsonValue.Parse "[\"Adam\",\"Eve\",\"Bonnie\",\"Clyde\",\"Donald\",\"Daisy\",\"Han\",\"Leia\"]"
j.[0] |> should equal (JsonValue.String "Adam")
j.[1] |> should equal (JsonValue.String "Eve")
j.[2] |> should equal (JsonValue.String "Bonnie")
j.[3] |> should equal (JsonValue.String "Clyde")
[<Test>]
let ``Can parse nested array``() =
let j = JsonValue.Parse "[ [\"Adam\", \"Eve\"], [\"Bonnie\", \"Clyde\"], [\"Donald\", \"Daisy\"], [\"Han\", \"Leia\"] ]"
j.[0].[0] |> should equal (JsonValue.String "Adam")
j.[0].[1] |> should equal (JsonValue.String "Eve")
j.[1].[0] |> should equal (JsonValue.String "Bonnie")
j.[1].[1] |> should equal (JsonValue.String "Clyde")
[<Test>]
let ``TryParse is None when a bad JSON value is given``() =
let j = JsonValue.TryParse "}{"
j |> should equal None
[<Test>]
let ``TryParse is Some of the correct JSON value when a good structure is given``() =
let j = JsonValue.TryParse """{ "foo": "bar" }"""
j |> should equal (Some (JsonValue.Record [| "foo", JsonValue.String "bar" |]))
[<Test>]
let ``Can serialize empty document``() =
(JsonValue.Record [| |]).ToString(JsonSaveOptions.DisableFormatting)
|> should equal "{}"
[<Test>]
let ``Can serialize document with single property``() =
(JsonValue.Record [| "firstName", JsonValue.String "John" |]).ToString(JsonSaveOptions.DisableFormatting)
|> should equal "{\"firstName\":\"John\"}"
[<Test>]
let ``Can serialize document with booleans``() =
( JsonValue.Record [| "aa", JsonValue.Boolean true
"bb", JsonValue.Boolean false |] ).ToString(JsonSaveOptions.DisableFormatting)
|> should equal "{\"aa\":true,\"bb\":false}"
[<Test>]
let ``Can serialize document with array, null and number``() =
let text = "{\"items\":[{\"id\":\"Open\"},null,{\"id\":25}]}"
let json = JsonValue.Parse text
json.ToString(JsonSaveOptions.DisableFormatting) |> should equal text
[<TestCase(Double.NaN)>]
[<TestCase(Double.PositiveInfinity)>]
[<TestCase(Double.NegativeInfinity)>]
let ``Serializes special float value as null`` v =
let json = JsonValue.Float v
json.ToString(JsonSaveOptions.DisableFormatting) |> should equal "null"
[<Test>]
let ``Float value 100.0 serializes with decimal point`` () =
// Regression test for https://github.com/fsprojects/FSharp.Data/issues/1356
// JsonValue.Float(100.0) should serialize as "100.0", not "100"
JsonValue.Float(100.0).ToString(JsonSaveOptions.DisableFormatting) |> should equal "100.0"
[<Test>]
let ``Float value with fractional part serializes correctly`` () =
JsonValue.Float(100.5).ToString(JsonSaveOptions.DisableFormatting) |> should equal "100.5"
[<Test>]
let ``Float value in scientific notation serializes correctly`` () =
JsonValue.Float(1e20).ToString(JsonSaveOptions.DisableFormatting) |> should equal "1E+20"
let normalize (str:string) =
str.Replace("\r\n", "\n")
.Replace("\r", "\n")
[<Test>]
let ``Pretty printing works``() =
let text = """{"items":[{"id":"Open"},null,{"id":25}],"x":{"y":2},"z":[]}"""
let json = JsonValue.Parse text
json.ToString() |> normalize |> should equal (normalize """{
"items": [
{
"id": "Open"
},
null,
{
"id": 25
}
],
"x": {
"y": 2
},
"z": []
}""")
[<Test>]
let ``Pretty printing with specified indentation works``() =
let text = """{"items":[{"id":"Open"},null,{"id":25}],"x":{"y":2},"z":[]}"""
let json = JsonValue.Parse text
json.ToString(indentationSpaces = 3) |> normalize |> should equal (normalize """{
"items": [
{
"id": "Open"
},
null,
{
"id": 25
}
],
"x": {
"y": 2
},
"z": []
}""")
[<Test>]
let ``Can parse various JSON documents``() =
let IsFloatNear (l : float) (r : float) =
if r < 16. * Double.Epsilon then
l < 16. * Double.Epsilon
else
let d = 1E-10
let ratio = l / r
ratio < (1. + d) && ratio > (1. - d)
let rec IsJsonEqual (l : JsonValue) (r : JsonValue) =
match l,r with
| JsonValue.Null , JsonValue.Null -> true
| JsonValue.Boolean lb , JsonValue.Boolean rb when lb = rb -> true
| JsonValue.String ls , JsonValue.String rs when ls = rs -> true
| JsonValue.Float lf , JsonValue.Float rf when IsFloatNear lf rf -> true // The reasons we do a custom isEqual
| JsonValue.Float lf , JsonValue.Number rd when IsFloatNear lf (float rd) -> true // The reasons we do a custom isEqual
| JsonValue.Number ld , JsonValue.Float rf when IsFloatNear (float ld) rf -> true // The reasons we do a custom isEqual
| JsonValue.Number ld , JsonValue.Number rd when ld = rd -> true // The reasons we do a custom isEqual
| JsonValue.Array lvs , JsonValue.Array rvs ->
if lvs.Length <> rvs.Length then false
else
let mutable res = true
let mutable i = 0
let length = lvs.Length
while res && i < length do
let li = lvs.[i]
let ri = rvs.[i]
res <- IsJsonEqual li ri
i <- i + 1
res
| JsonValue.Record lms , JsonValue.Record rms ->
if lms.Length <> rms.Length then false
else
let mutable res = true
let mutable i = 0
let length = lms.Length
while res && i < length do
let (ln,li) = lms.[i]
let (rn,ri) = rms.[i]
res <- (ln = rn) && IsJsonEqual li ri
i <- i + 1
res
| _ -> false
let Array = JsonValue.Array
let Null = JsonValue.Null
let Boolean = JsonValue.Boolean
let String = JsonValue.String
let Float = JsonValue.Float
let Record = JsonValue.Record
let manualTestCases =
[
// Positive tests
"""[]""" , Some <| Array [||]
"""[null]""" , Some <| Array [|Null|]
"""[true]""" , Some <| Array [|Boolean true|]
"""[false]""" , Some <| Array [|Boolean false|]
"""[""]""" , Some <| Array [|String ""|]
"""["Test"]""" , Some <| Array [|String "Test"|]
"""["Test\t"]""" , Some <| Array [|String "Test\t"|]
"""["\""]""" , Some <| Array [|String "\""|]
"""["\"\\\//\b\f\n\r\t\u2665"]""" , Some <| Array [|String "\"\\//\b\f\n\r\t\u2665"|]
"""["\ud83d\udc36"]""" , Some <| Array [|String "\ud83d\udc36"|]
"""[0]""" , Some <| Array [|Float 0.|]
"""[0.5]""" , Some <| Array [|Float 0.5|]
"""[1234]""" , Some <| Array [|Float 1234.|]
"""[-1234]""" , Some <| Array [|Float -1234.|]
"""[1234.25]""" , Some <| Array [|Float 1234.25|]
"""[-1234.25]""" , Some <| Array [|Float -1234.25|]
"""[1234.50E2]""" , Some <| Array [|Float 123450.|]
"""[-1234.5E+2]""" , Some <| Array [|Float -123450.|]
"""[123450E-2]""" , Some <| Array [|Float 1234.50|]
"""[-123450e-2]""" , Some <| Array [|Float -1234.50|]
"""[4.26353146520608E+18]""" , Some <| Array [|Float 4.26353146520608E+18|]
"""[1.2345]""" , Some <| Array [|Float 1.2345|]
"""[null,false]""" , Some <| Array [|Null;Boolean false|]
"""[{}]""" , Some <| Array [|Record [||]|]
"""{}""" , Some <| Record [||]
"""{"a":null}""" , Some <| Record [|"a",Null|]
"""{"a":[]}""" , Some <| Record [|"a",Array [||]|]
"""{"a":[],"b":{}}""" , Some <| Record [|"a",Array [||];"b",Record [||]|]
"""{"\"":null}""" , Some <| Record [|"\"",Null|]
"""{"\"\\\//\b\f\n\r\t\u2665":null}""" , Some <| Record [|"\"\\//\b\f\n\r\t\u2665",Null|]
// Whitespace cases
""" []""" , Some <| Array [||]
"""[] """ , Some <| Array [||]
""" [] """ , Some <| Array [||]
"""[ true]""" , Some <| Array [|Boolean true|]
"""[true ]""" , Some <| Array [|Boolean true|]
"""[ true ]""" , Some <| Array [|Boolean true|]
"""[null, true]""" , Some <| Array [|Null;Boolean true|]
"""[null ,true]""" , Some <| Array [|Null;Boolean true|]
"""[null , true]""" , Some <| Array [|Null;Boolean true|]
""" {}""" , Some <| Record [||]
"""{} """ , Some <| Record [||]
""" {} """ , Some <| Record [||]
"""{ "a":true}""" , Some <| Record [|"a",Boolean true|]
"""{"a":true }""" , Some <| Record [|"a",Boolean true|]
"""{ "a":true }""" , Some <| Record [|"a",Boolean true|]
"""{"a" :true}""" , Some <| Record [|"a",Boolean true|]
"""{"a": true}""" , Some <| Record [|"a",Boolean true|]
"""{"a" : true}""" , Some <| Record [|"a",Boolean true|]
"""{"a":[] ,"b":{}}""" , Some <| Record [|"a",Array [||];"b",Record [||]|]
"""{"a":[], "b":{}}""" , Some <| Record [|"a",Array [||];"b",Record [||]|]
"""{"a":[] , "b":{}}""" , Some <| Record [|"a",Array [||];"b",Record [||]|]
"""0""" , Some <| Float 0.
"""1234""" , Some <| Float 1234.
"""true""" , Some <| Boolean true
"""false""" , Some <| Boolean false
"""null""" , Some <| Null
""" "Test" """ , Some <| String "Test"
// Negative tests
"""[NaN]""" , None
"""[,]""" , None
"""[true,]""" , None
"""{,}""" , None
"""{{}}""" , None
"""{a:[]}""" , None
"""{"a":[],}""" , None
"""[+123]""" , None
// According to json.org this should fail but currently don't
// """[0123]""" , None
]
let testCases = manualTestCases
let failures = System.Text.StringBuilder ()
let failure (m : string) =
ignore <| failures.AppendLine m
for json,expected in testCases do
try
let result = JsonValue.Parse json
match expected with
| Some exp when IsJsonEqual exp result -> ()
| Some exp -> failure (sprintf "Parse succeeded but didn't produce expected value\nJSON:\n%s\nExpected:\n%A\nActual:\n%A" json exp result)
| None -> failure (sprintf "Parse succeeded but expected to fail\nJSON:\n%s\nActual:\n%A" json result)
with
| e ->
match expected with
| None -> ()
| Some exp -> failure <| sprintf "Parse failed but expected to succeed\nJSON:\n%s\nExpected:\n%A\nException:\n%A" json exp e
if failures.Length > 0 then
Assert.Fail <| failures.ToString ()
[<Test>]
let ``Basic special characters encoded correctly`` () =
let input = " \"quoted\" and \'quoted\' and \r\n and \uABCD "
let w = new IO.StringWriter()
JsonValue.JsonStringEncodeTo w input
let expected = " \\\"quoted\\\" and 'quoted' and \\r\\n and \uABCD "
(w.GetStringBuilder().ToString()) |> should equal expected
[<Test>]
let ``Encoding of simple string is valid JSON`` () =
let input = "sample \"json\" with \t\r\n \' quotes etc."
let w = new IO.StringWriter()
JsonValue.JsonStringEncodeTo w input
let expected = "sample \\\"json\\\" with \\t\\r\\n ' quotes etc."
(w.GetStringBuilder().ToString()) |> should equal expected
[<Test>]
let ``Encoding of markup is not overzealous`` () =
let input = "<SecurityLabel><MOD>ModelAdministrators</MOD></SecurityLabel>"
let w = new IO.StringWriter()
JsonValue.JsonStringEncodeTo w input
let expected = input // Should not escape </>
(w.GetStringBuilder().ToString()) |> should equal expected
// --------------------------------------------------------------------------------------
// Additional tests for improved coverage
// --------------------------------------------------------------------------------------
[<Test>]
let ``JsonSaveOptions DisableFormatting produces compact JSON`` () =
let json = JsonValue.Record [| ("name", JsonValue.String "test"); ("values", JsonValue.Array [| JsonValue.Number 1M; JsonValue.Number 2M |]) |]
let result = json.ToString(JsonSaveOptions.DisableFormatting)
result |> should equal """{"name":"test","values":[1,2]}"""
[<Test>]
let ``JsonSaveOptions CompactSpaceAfterComma adds spaces after commas`` () =
let json = JsonValue.Record [| ("a", JsonValue.Number 1M); ("b", JsonValue.Number 2M) |]
let result = json.ToString(JsonSaveOptions.CompactSpaceAfterComma)
result |> should equal """{"a":1, "b":2}"""
[<Test>]
let ``JsonSaveOptions None produces formatted JSON`` () =
let json = JsonValue.Record [| ("name", JsonValue.String "test") |]
let result = json.ToString(JsonSaveOptions.None)
result.Contains("\n") |> should equal true
result.Contains(" ") |> should equal true
[<Test>]
let ``JsonValue handles Float infinity as null during serialization`` () =
let json = JsonValue.Float System.Double.PositiveInfinity
let result = json.ToString()
result |> should equal "null"
[<Test>]
let ``JsonValue handles Float negative infinity as null during serialization`` () =
let json = JsonValue.Float System.Double.NegativeInfinity
let result = json.ToString()
result |> should equal "null"
[<Test>]
let ``JsonValue handles Float NaN as null during serialization`` () =
let json = JsonValue.Float System.Double.NaN
let result = json.ToString()
result |> should equal "null"
[<Test>]
let ``JsonValue ToString handles long content appropriately`` () =
let longString = String.replicate 600 "a"
let json = JsonValue.String longString
let result = json.ToString()
result.Contains(longString) |> should equal true
result.StartsWith("\"") |> should equal true
result.EndsWith("\"") |> should equal true
[<Test>]
let ``JsonValue ToString preserves short strings`` () =
let shortString = "short"
let json = JsonValue.String shortString
let result = json.ToString()
result |> should equal "\"short\""
[<Test>]
let ``JsonValue parsing with single-line comments`` () =
let jsonWithComments = """{
// This is a comment
"name": "test",
"value": 42 // Another comment
}"""
let result = JsonValue.Parse jsonWithComments
result?name.AsString() |> should equal "test"
result?value.AsInteger() |> should equal 42
[<Test>]
let ``JsonValue parsing with multi-line comments`` () =
let jsonWithComments = """{
/* This is a
multi-line comment */
"data": "valid"
}"""
let result = JsonValue.Parse jsonWithComments
result?data.AsString() |> should equal "valid"
[<Test>]
let ``JsonValue parsing with multi-line comment containing asterisk`` () =
// Bug: old code used '&&' so a '*' anywhere inside stopped scanning too early
let json = """{ /* a * b */ "x": 1 }"""
let result = JsonValue.Parse json
result?x.AsInteger() |> should equal 1
[<Test>]
let ``JsonValue parsing with multi-line comment containing slash`` () =
// Bug: old code used '&&' so a '/' anywhere inside stopped scanning too early
let json = """{ /* path/to/file */ "x": 2 }"""
let result = JsonValue.Parse json
result?x.AsInteger() |> should equal 2
[<Test>]
let ``JsonValue parsing with multi-line comment containing both asterisk and slash`` () =
let json = "{ /* a/b * c */ \"x\": 3 }"
let result = JsonValue.Parse json
result?x.AsInteger() |> should equal 3
[<Test>]
let ``JsonValue parsing with mixed whitespace and comments`` () =
let jsonWithCommentsAndWhitespace = """
{
// Comment 1
"a": 1,
/* Comment 2 */ "b": 2
// Final comment
}
"""
let result = JsonValue.Parse jsonWithCommentsAndWhitespace
result?a.AsInteger() |> should equal 1
result?b.AsInteger() |> should equal 2
[<Test>]
let ``JsonValue parsing handles unicode escape sequences`` () =
let jsonWithUnicode = """{"unicode": "Hello \u0041\u0042\u0043"}"""
let result = JsonValue.Parse jsonWithUnicode
result?unicode.AsString() |> should equal "Hello ABC"
[<Test>]
let ``JsonValue parsing handles low-value unicode escapes`` () =
let jsonWithUnicode = """{"control": "\u0001\u0002\u0003"}"""
let result = JsonValue.Parse jsonWithUnicode
result?control.AsString().Length |> should equal 3
[<Test>]
let ``JsonValue parsing throws on malformed JSON`` () =
let malformedJson = """{"incomplete": """
(fun () -> JsonValue.Parse malformedJson |> ignore) |> should throw typeof<System.Exception>
[<Test>]
let ``JsonValue parsing throws on invalid unicode escape`` () =
let invalidUnicode = """{"bad": "\uXYZ"}"""
(fun () -> JsonValue.Parse invalidUnicode |> ignore) |> should throw typeof<System.Exception>
[<Test>]
let ``JsonValue parsing throws on incomplete unicode escape`` () =
let incompleteUnicode = """{"bad": "\u12"}"""
(fun () -> JsonValue.Parse incompleteUnicode |> ignore) |> should throw typeof<System.Exception>
[<Test>]
let ``JsonValue parsing handles empty arrays`` () =
let json = JsonValue.Parse "[]"
match json with
| JsonValue.Array elements -> elements.Length |> should equal 0
| _ -> failwith "Expected array"
[<Test>]
let ``JsonValue parsing handles empty objects`` () =
let json = JsonValue.Parse "{}"
match json with
| JsonValue.Record properties -> properties.Length |> should equal 0
| _ -> failwith "Expected record"
[<Test>]
let ``JsonValue parsing handles nested structures`` () =
let nestedJson = """{"level1": {"level2": {"level3": "deep"}}}"""
let result = JsonValue.Parse nestedJson
result?level1?level2?level3.AsString() |> should equal "deep"
[<Test>]
let ``JsonValue parsing handles arrays with mixed types`` () =
let mixedArray = """[1, "text", true, null, 3.14]"""
let result = JsonValue.Parse mixedArray
match result with
| JsonValue.Array elements ->
elements.[0] |> should equal (JsonValue.Number 1M)
elements.[1] |> should equal (JsonValue.String "text")
elements.[2] |> should equal (JsonValue.Boolean true)
elements.[3] |> should equal JsonValue.Null
elements.[4].AsFloat() |> should equal 3.14 // Use AsFloat for comparison
| _ -> failwith "Expected array"
[<Test>]
let ``JsonValue handles number parsing with exponential notation`` () =
let json = JsonValue.Parse """{"exp": 1.23e10}"""
json?exp.AsFloat() |> should equal 1.23e10
[<Test>]
let ``JsonValue handles number parsing with negative exponential`` () =
let json = JsonValue.Parse """{"exp": 1.23e-5}"""
json?exp.AsFloat() |> should equal 1.23e-5
[<Test>]
let ``JsonValue handles number parsing with positive exponential`` () =
let json = JsonValue.Parse """{"exp": 1.23e+3}"""
json?exp.AsFloat() |> should equal 1.23e3
[<Test>]
let ``JsonStringEncodeTo handles null or empty string`` () =
let w = new System.IO.StringWriter()
JsonValue.JsonStringEncodeTo w null
w.ToString() |> should equal ""
w.GetStringBuilder().Clear() |> ignore
JsonValue.JsonStringEncodeTo w ""
w.ToString() |> should equal ""
[<Test>]
let ``JsonStringEncodeTo handles control characters`` () =
let controlChars = "\u0001\u0002\u0003\u0004\u0005\u0006\u0007"
let w = new System.IO.StringWriter()
JsonValue.JsonStringEncodeTo w controlChars
let result = w.ToString()
result |> should equal "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007"
[<Test>]
let ``JsonStringEncodeTo handles high-value control characters`` () =
let controlChars = "\u000e\u000f\u0010\u001f"
let w = new System.IO.StringWriter()
JsonValue.JsonStringEncodeTo w controlChars
let result = w.ToString()
result |> should equal "\\u000e\\u000f\\u0010\\u001f"
[<Test>]
let ``JsonStringEncodeTo preserves normal characters`` () =
let normalText = "Hello, World! 123 ABC xyz"
let w = new System.IO.StringWriter()
JsonValue.JsonStringEncodeTo w normalText
let result = w.ToString()
result |> should equal normalText
[<Test>]
let ``JsonValue ToString with custom indentation`` () =
let json = JsonValue.Record [| ("nested", JsonValue.Record [| ("value", JsonValue.Number 42M) |]) |]
let result = json.ToString(indentationSpaces = 4)
result.Contains(" ") |> should equal true
[<Test>]
let ``JsonValue obsolete Object pattern still works`` () =
let json = JsonValue.Record [| ("a", JsonValue.Number 1M); ("b", JsonValue.Number 2M) |]
match json with
| JsonValue.Object map ->
map.["a"] |> should equal (JsonValue.Number 1M)
map.["b"] |> should equal (JsonValue.Number 2M)
| _ -> failwith "Pattern matching failed"
[<Test>]
let ``JsonValue obsolete Object constructor still works`` () =
let map = Map.ofList [("key", JsonValue.String "value"); ("num", JsonValue.Number 123M)]
let json = JsonValue.Object map
match json with
| JsonValue.Record properties ->
properties |> Array.exists (fun (k, v) -> k = "key" && v = JsonValue.String "value") |> should equal true
properties |> Array.exists (fun (k, v) -> k = "num" && v = JsonValue.Number 123M) |> should equal true
| _ -> failwith "Expected record"
// ---- ParseMultiple (NDJSON / concatenated JSON) tests ----
[<Test>]
let ``ParseMultiple returns empty sequence for empty string`` () =
JsonValue.ParseMultiple "" |> Seq.length |> should equal 0
[<Test>]
let ``ParseMultiple parses single value`` () =
let values = JsonValue.ParseMultiple """{"a":1}""" |> Array.ofSeq
values.Length |> should equal 1
values.[0]?a.AsInteger() |> should equal 1
[<Test>]
let ``ParseMultiple parses newline-delimited JSON (NDJSON)`` () =
let ndjson = "{\"id\":1}\n{\"id\":2}\n{\"id\":3}"
let values = JsonValue.ParseMultiple ndjson |> Array.ofSeq
values.Length |> should equal 3
values.[0]?id.AsInteger() |> should equal 1
values.[1]?id.AsInteger() |> should equal 2
values.[2]?id.AsInteger() |> should equal 3
[<Test>]
let ``ParseMultiple handles whitespace between values`` () =
let json = "1 2 3"
let values = JsonValue.ParseMultiple json |> Array.ofSeq
values.Length |> should equal 3
values.[0] |> should equal (JsonValue.Number 1M)
values.[1] |> should equal (JsonValue.Number 2M)
values.[2] |> should equal (JsonValue.Number 3M)
[<Test>]
let ``ParseMultiple handles mixed types in sequence`` () =
let json = """{"x":1} [1,2,3] "hello" true null"""
let values = JsonValue.ParseMultiple json |> Array.ofSeq
values.Length |> should equal 5
values.[1] |> should equal (JsonValue.Array [| JsonValue.Number 1M; JsonValue.Number 2M; JsonValue.Number 3M |])
values.[2] |> should equal (JsonValue.String "hello")
values.[3] |> should equal (JsonValue.Boolean true)
values.[4] |> should equal JsonValue.Null
[<Test>]
let ``ParseMultiple returns lazy sequence`` () =
// Verify only the first value is evaluated when taking only one element
let ndjson = "{\"a\":1}\n{\"b\":2}\n{\"c\":3}"
let firstOnly = JsonValue.ParseMultiple ndjson |> Seq.head
firstOnly?a.AsInteger() |> should equal 1
// ---- JsonValue.Load(Stream) and Load(TextReader) tests ----
[<Test>]
let ``JsonValue Load from stream parses correctly`` () =
let json = """{"name":"Alice","age":30}"""
let bytes = System.Text.Encoding.UTF8.GetBytes(json)
use stream = new System.IO.MemoryStream(bytes)
let result = JsonValue.Load(stream)
result?name.AsString() |> should equal "Alice"
result?age.AsInteger() |> should equal 30
[<Test>]
let ``JsonValue Load from TextReader parses correctly`` () =
let json = """{"items":[1,2,3]}"""
use reader = new System.IO.StringReader(json)
let result = JsonValue.Load(reader :> System.IO.TextReader)
result?items.AsArray() |> Array.map (fun v -> v.AsInteger()) |> should equal [| 1; 2; 3 |]
[<Test>]
let ``JsonValue Load from stream handles nested structures`` () =
let json = """{"outer":{"inner":"value"}}"""
let bytes = System.Text.Encoding.UTF8.GetBytes(json)
use stream = new System.IO.MemoryStream(bytes)
let result = JsonValue.Load(stream)
result?outer?inner.AsString() |> should equal "value"
[<Test>]
let ``JsonValue WriteTo with DisableFormatting produces compact output`` () =
let json = JsonValue.Record [| "a", JsonValue.Number 1M; "b", JsonValue.String "x" |]
use writer = new System.IO.StringWriter()
json.WriteTo(writer, JsonSaveOptions.DisableFormatting)
let result = writer.ToString()
result |> should equal """{"a":1,"b":"x"}"""
[<Test>]
let ``JsonValue WriteTo with None (default) produces indented output`` () =
let json = JsonValue.Record [| "a", JsonValue.Number 1M |]
use writer = new System.IO.StringWriter()
json.WriteTo(writer, JsonSaveOptions.None)
let result = writer.ToString()
result.Contains("\n") |> should equal true
result.Contains(" ") |> should equal true