summary refs log tree commit diff stats
path: root/tests/stdlib/tjsonutils.nim
Commit message (Expand)AuthorAgeFilesLines
* jsonutils: support cstring (including as Table key); improve docs (#16062)Timothee Cour2021-03-311-5/+8
* fix #17325 linux 32bit CI; fix #17085 flaky test (#17469)Timothee Cour2021-03-241-1/+1
* fix #17383: json.%,to and jsonutils.formJson,toJson now works with uint|uint6...Timothee Cour2021-03-161-0/+15
* fix #16822 (#16884)flywind2021-02-011-2/+1
* jsonutils.jsonTo: support opt (#16739)Timothee Cour2021-01-171-0/+3
* Jsonutils: pass opt for object in object (#16615)inv20042021-01-071-0/+6
* jsonutils: fromJson forward opt param fix (#16612)inv20042021-01-061-0/+14
* Correct all eggs (#15906)Miran2020-11-101-1/+1
* Add some enhancements to `jsonutils.nim` (#15133)Ivan Bobev2020-09-091-2/+183
* fromJson: support object variants (#14694)Timothee Cour2020-06-241-0/+53
* make `fromJson/toJson` work with `array[range, typ]`, + 1 bugfix (#14669)Timothee Cour2020-06-151-0/+26
* `toJson`, `jsonTo`, json (de)serialization for custom types; remove dependenc...Timothee Cour2020-06-081-0/+44
ous revision' href='/ahoang/Nim/blame/tests/stdlib/tjsonmacro.nim?h=devel&id=12aafb25cc51488a99d6d73a7fd3965eb73b0bf5'>^
658467a31 ^



5ed99f8d3 ^
658467a31 ^

5ed99f8d3 ^
658467a31 ^



21cbfd72e ^




658467a31 ^


5ed99f8d3 ^
658467a31 ^





cc223ff7d ^




21cbfd72e ^


cc223ff7d ^




7f49364fd ^
cc223ff7d ^











7f49364fd ^
658467a31 ^
a883424d0 ^































e29e6ec76 ^

a883424d0 ^

39290cf88 ^
cc223ff7d ^

658467a31 ^















































eedc6fecd ^
















2108dcf0d ^
eedc6fecd ^


2108dcf0d ^
eedc6fecd ^

88cb40cd3 ^















2108dcf0d ^



793d6efff ^
2108dcf0d ^




















793d6efff ^








03ddfddca ^








d1daccac2 ^



















11fcae570 ^












e0681715d ^















d3c9b58c0 ^


5ed99f8d3 ^





d3c9b58c0 ^
5ed99f8d3 ^


d3c9b58c0 ^
5ed99f8d3 ^



d3c9b58c0 ^
5ed99f8d3 ^

d3c9b58c0 ^
5ed99f8d3 ^
8ca41ce63 ^


















9c2cdc94a ^














c7cba5e6f ^






f92d61b1f ^
c7cba5e6f ^






8187e8364 ^























8d6126237 ^














578ab935c ^

























824732386 ^
f92d61b1f ^
824732386 ^



5ed99f8d3 ^




f92d61b1f ^






















































5ed99f8d3 ^
f92d61b1f ^












































b8454327c ^
5ed99f8d3 ^





eff0837ff ^
5ed99f8d3 ^



eff0837ff ^
5ed99f8d3 ^









1102b8ac6 ^
5ed99f8d3 ^



21cbfd72e ^
21cbfd72e ^
5ed99f8d3 ^




21cbfd72e ^
5ed99f8d3 ^


21cbfd72e ^
5ed99f8d3 ^





21cbfd72e ^
5ed99f8d3 ^















21cbfd72e ^
5ed99f8d3 ^



























ecc9704bf ^











5ed99f8d3 ^




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
           
            
                 
   
 
                                      
 









                                                                              

                                                                



                     
                          

                                               
                        



                                 




                           


                          
                       





                               




                        


                                              




                         
                  











                                        
                             
 































                                                                              

                                                          

                                                    
                                                 

                                                          















































                                  
















                                    
                                                                  


                      
                                  

                                         















                                       



                                
                                




















                                         








                                       








                                          



















                                                                           












                                                    















                                                    


                                





                                                      
 


                                                                       
 



                                
 

                                         
 
                     


















                                                                     














                                                       






                          
 






                                                                           























                                                                 














                                              

























                                                              
                                   
 



                                               




                                                                     






















































                                                    
 












































                                                       
 





                                                       
 



                                                  
 









                                        
 



                                                                      
 
 




                              
 


                           
 





                                                
 















                                      
 



























                                                                        











                                     




            
discard """
  output: ""
  targets: "c js"
"""

import json, strutils, options, tables

# The definition of the `%` proc needs to be here, since the `% c` calls below
# can only find our custom `%` proc for `Pix` if defined in global scope.
type
  Pix = tuple[x, y: uint8, ch: uint16]
proc `%`(p: Pix): JsonNode =
  result = %* { "x" : % p.x,
                "y" : % p.y,
                "ch" : % p.ch }

proc testJson() =
  # Tests inspired by own use case (with some additional tests).
  # This should succeed.
  type
    Point[T] = object
      x, y: T

    ReplayEventKind = enum
      FoodAppeared, FoodEaten, DirectionChanged

    ReplayEvent = object
      time*: float
      case kind*: ReplayEventKind
      of FoodAppeared, FoodEaten:
        foodPos*: Point[float]
        case subKind*: bool
        of true:
          it: int
        of false:
          ot: float
      of DirectionChanged:
        playerPos*: float

    Replay = ref object
      events*: seq[ReplayEvent]
      test: int
      test2: string
      test3: bool
      testNil: string

  var x = Replay(
    events: @[
      ReplayEvent(
        time: 1.2345,
        kind: FoodEaten,
        foodPos: Point[float](x: 5.0, y: 1.0),
        subKind: true,
        it: 7
      )
    ],
    test: 18827361,
    test2: "hello world",
    test3: true,
    testNil: "nil"
  )

  let node = %x

  let y = to(node, Replay)
  doAssert y.events[0].time == 1.2345
  doAssert y.events[0].kind == FoodEaten
  doAssert y.events[0].foodPos.x == 5.0
  doAssert y.events[0].foodPos.y == 1.0
  doAssert y.test == 18827361
  doAssert y.test2 == "hello world"
  doAssert y.test3
  doAssert y.testNil == "nil"

  # Test for custom object variants (without an enum) and with an else branch.
  block:
    type
      TestVariant = object
        name: string
        case age: uint8
        of 2:
          preSchool: string
        of 8:
          primarySchool: string
        else:
          other: int

    var node = %{
      "name": %"Nim",
      "age": %8,
      "primarySchool": %"Sandtown"
    }

    var result = to(node, TestVariant)
    doAssert result.age == 8
    doAssert result.name == "Nim"
    doAssert result.primarySchool == "Sandtown"

    node = %{
      "name": %"⚔️Foo☢️",
      "age": %25,
      "other": %98
    }

    result = to(node, TestVariant)
    doAssert result.name == node["name"].getStr()
    doAssert result.age == node["age"].getInt().uint8
    doAssert result.other == node["other"].getBiggestInt()

  # TODO: Test object variant with set in of branch.
  # TODO: Should we support heterogeneous arrays?

  # Tests that verify the error messages for invalid data.
  block:
    type
      Person = object
        name: string
        age: int

    var node = %{
      "name": %"Dominik"
    }

    try:
      discard to(node, Person)
      doAssert false
    except KeyError as exc:
      doAssert("age" in exc.msg)
    except:
      doAssert false

    node["age"] = %false

    try:
      discard to(node, Person)
      doAssert false
    except JsonKindError as exc:
      doAssert("age" in exc.msg)
    except:
      doAssert false

    type
      PersonAge = enum
        Fifteen, Sixteen

      PersonCase = object
        name: string
        case age: PersonAge
        of Fifteen:
          discard
        of Sixteen:
          id: string

    try:
      discard to(node, PersonCase)
      doAssert false
    except JsonKindError as exc:
      doAssert("age" in exc.msg)
    except:
      doAssert false

  # Test the example in json module.
  block:
    let jsonNode = parseJson("""
      {
        "person": {
          "name": "Nimmer",
          "age": 21
        },
        "list": [1, 2, 3, 4]
      }
    """)

    type
      Person = object
        name: string
        age: int

      Data1 = object # TODO: Codegen bug when changed to ``Data``.
        person: Person
        list: seq[int]

    var data = to(jsonNode, Data1)
    doAssert data.person.name == "Nimmer"
    doAssert data.person.age == 21
    doAssert data.list == @[1, 2, 3, 4]

  # Test non-variant enum fields.
  block:
    type
      EnumType = enum
        Foo, Bar

      TestEnum = object
        field: EnumType

    var node = %{
      "field": %"Bar"
    }

    var result = to(node, TestEnum)
    doAssert result.field == Bar

  # Test ref type in field.
  block:
    var jsonNode = parseJson("""
      {
        "person": {
          "name": "Nimmer",
          "age": 21
        },
        "list": [1, 2, 3, 4]
      }
    """)

    type
      Person = ref object
        name: string
        age: int

      Data = object
        person: Person
        list: seq[int]

    var data = to(jsonNode, Data)
    doAssert data.person.name == "Nimmer"
    doAssert data.person.age == 21
    doAssert data.list == @[1, 2, 3, 4]

    jsonNode = parseJson("""
      {
        "person": null,
        "list": [1, 2, 3, 4]
      }
    """)
    data = to(jsonNode, Data)
    doAssert data.person.isNil

  block:
    type
      FooBar = object
        field: float

    let x = parseJson("""{ "field": 5}""")
    let data = to(x, FooBar)
    doAssert data.field == 5.0

  block:
    type
      BirdColor = object
        name: string
        rgb: array[3, float]

    type
      Bird = object
        age: int
        height: float
        name: string
        colors: array[2, BirdColor]

    var red = BirdColor(name: "red", rgb: [1.0, 0.0, 0.0])
    var blue = Birdcolor(name: "blue", rgb: [0.0, 0.0, 1.0])
    var b = Bird(age: 3, height: 1.734, name: "bardo", colors: [red, blue])
    let jnode = %b
    let data = jnode.to(Bird)
    doAssert data == b

  block:
    type
      MsgBase = ref object of RootObj
        name*: string

      MsgChallenge = ref object of MsgBase
        challenge*: string

    let data = %*{"name": "foo", "challenge": "bar"}
    let msg = data.to(MsgChallenge)
    doAssert msg.name == "foo"
    doAssert msg.challenge == "bar"

  block:
    type
      Color = enum Red, Brown
      Thing = object
        animal: tuple[fur: bool, legs: int]
        color: Color

    var j = parseJson("""
      {"animal":{"fur":true,"legs":6},"color":"Red"}
    """)

    let parsed = to(j, Thing)
    doAssert parsed.animal.fur
    doAssert parsed.animal.legs == 6
    doAssert parsed.color == Red

  block:
    when not defined(js):
      # disable on js because of #12492
      type
        Car = object
          engine: tuple[name: string, capacity: float]
          model: string

      let j = """
        {"engine": {"name": "V8", "capacity": 5.5}, "model": "Skyline"}
      """

      var i = 0
      proc mulTest(): JsonNode =
        inc i
        return parseJson(j)

      let parsed = mulTest().to(Car)
      doAssert parsed.engine.name == "V8"

      doAssert i == 1

  block:
    # Option[T] support!
    type
      Car1 = object # TODO: Codegen bug when `Car`
        engine: tuple[name: string, capacity: Option[float]]
        model: string
        year: Option[int]

    let noYear = """
      {"engine": {"name": "V8", "capacity": 5.5}, "model": "Skyline"}
    """

    let noYearParsed = parseJson(noYear)
    let noYearDeser = to(noYearParsed, Car1)
    doAssert noYearDeser.engine.capacity == some(5.5)
    doAssert noYearDeser.year.isNone
    doAssert noYearDeser.engine.name == "V8"

    # Issue #7433
    type
      Obj2 = object
        n1: int
        n2: Option[string]
        n3: bool

    var j = %*[ { "n1": 4, "n2": "ABC", "n3": true },
                { "n1": 1, "n3": false },
                { "n1": 1, "n2": "XYZ", "n3": false } ]

    let jDeser = j.to(seq[Obj2])
    doAssert jDeser[0].n2.get() == "ABC"
    doAssert jDeser[1].n2.isNone()

    # Issue #6902
    type
      Obj = object
        n1: int
        n2: Option[int]
        n3: Option[string]
        n4: Option[bool]

    var j0 = parseJson("""{"n1": 1, "n2": null, "n3": null, "n4": null}""")
    let j0Deser = j0.to(Obj)
    doAssert j0Deser.n1 == 1
    doAssert j0Deser.n2.isNone()
    doAssert j0Deser.n3.isNone()
    doAssert j0Deser.n4.isNone()

  # Table[T, Y] support.
  block:
    type
      Friend = object
        name: string
        age: int

      Dynamic = object
        name: string
        friends: Table[string, Friend]

    let data = """
      {"friends": {
                    "John": {"name": "John", "age": 35},
                    "Elizabeth": {"name": "Elizabeth", "age": 23}
                  }, "name": "Dominik"}
    """

    let dataParsed = parseJson(data)
    let dataDeser = to(dataParsed, Dynamic)
    doAssert dataDeser.name == "Dominik"
    doAssert dataDeser.friends["John"].age == 35
    doAssert dataDeser.friends["Elizabeth"].age == 23

  # JsonNode support
  block:
    type
      Test = object
        name: string
        fallback: JsonNode

    let data = """
      {"name": "FooBar", "fallback": 56.42}
    """

    let dataParsed = parseJson(data)
    let dataDeser = to(dataParsed, Test)
    doAssert dataDeser.name == "FooBar"
    doAssert dataDeser.fallback.kind == JFloat
    doAssert dataDeser.fallback.getFloat() == 56.42

  # int64, float64 etc support.
  block:
    type
      Test1 = object
        a: int8
        b: int16
        c: int32
        d: int64
        e: uint8
        f: uint16
        g: uint32
        h: uint64
        i: float32
        j: float64

    let data = """
      {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7,
       "h": 8, "i": 9.9, "j": 10.10}
    """

    let dataParsed = parseJson(data)
    let dataDeser = to(dataParsed, Test1)
    doAssert dataDeser.a == 1
    doAssert dataDeser.f == 6
    doAssert dataDeser.i == 9.9'f32

  # deserialize directly into a table
  block:
    let s = """{"a": 1, "b": 2}"""
    let t = parseJson(s).to(Table[string, int])
    when not defined(js):
      # For some reason on the JS backend `{"b": 2, "a": 0}` is
      # sometimes the value of `t`. This needs investigation. I can't
      # reproduce it right now in an isolated test.
      doAssert t["a"] == 1
    doAssert t["b"] == 2

  block:
    # bug #8037
    type
      Apple = distinct string
      String = distinct Apple
      Email = distinct string
      MyList = distinct seq[int]
      MyYear = distinct Option[int]
      MyTable = distinct Table[string, int]
      MyArr = distinct array[3, float]
      MyRef = ref object
        name: string
      MyObj = object
        color: int
      MyDistRef = distinct MyRef
      MyDistObj = distinct MyObj
      Toot = object
        name*: String
        email*: Email
        list: MyList
        year: MyYear
        dict: MyTable
        arr: MyArr
        person: MyDistRef
        distfruit: MyDistObj
        dog: MyRef
        fruit: MyObj
        emails: seq[String]

    var tJson = parseJson("""
      {
        "name":"Bongo",
        "email":"bongo@bingo.com",
        "list": [11,7,15],
        "year": 1975,
        "dict": {"a": 1, "b": 2},
        "arr": [1.0, 2.0, 7.0],
        "person": {"name": "boney"},
        "dog": {"name": "honey"},
        "fruit": {"color": 10},
        "distfruit": {"color": 11},
        "emails": ["abc", "123"]
      }
    """)

    var t = to(tJson, Toot)
    doAssert string(t.name) == "Bongo"
    doAssert string(t.email) == "bongo@bingo.com"
    doAssert seq[int](t.list) == @[11,7,15]
    doAssert Option[int](t.year).get() == 1975
    doAssert Table[string,int](t.dict)["a"] == 1
    doAssert Table[string,int](t.dict)["b"] == 2
    doAssert array[3, float](t.arr) == [1.0,2.0,7.0]

    doAssert MyRef(t.person).name == "boney"
    doAssert MyObj(t.distFruit).color == 11
    doAssert t.dog.name == "honey"
    doAssert t.fruit.color == 10
    doAssert seq[string](t.emails) == @["abc", "123"]

    block test_table:
      var y = parseJson("""{"a": 1, "b": 2, "c": 3}""")
      var u = y.to(MyTable)
      var v = y.to(Table[string, int])
      doAssert Table[string, int](u)["a"] == 1
      doAssert Table[string, int](u)["b"] == 2
      doAssert Table[string, int](u)["c"] == 3
      doAssert v["a"] == 1

    block primitive_string:
      const kApple = "apple"
      var u = newJString(kApple)
      var v = u.to(Email)
      var w = u.to(Apple)
      var x = u.to(String)
      doAssert string(v) == kApple
      doAssert string(w) == kApple
      doAssert string(x) == kApple

    block test_option:
      var u = newJInt(1137)
      var v = u.to(MyYear)
      var w = u.to(Option[int])
      doAssert Option[int](v).get() == 1137
      doAssert w.get() == 1137

    block test_object:
      var u = parseJson("""{"color": 987}""")
      var v = u.to(MyObj)
      var w = u.to(MyDistObj)
      doAssert v.color == 987
      doAssert MyObj(w).color == 987

    block test_ref_object:
      var u = parseJson("""{"name": "smith"}""")
      var v = u.to(MyRef)
      var w = u.to(MyDistRef)
      doAssert v.name == "smith"
      doAssert MyRef(w).name == "smith"

  block:
    # bug #12015
    type
      Cluster = object
        works: tuple[x, y: uint8, ch: uint16] # working
        fails: Pix # previously broken

    let data = (x: 123'u8, y: 53'u8, ch: 1231'u16)
    let c = Cluster(works: data, fails: data)
    let cFromJson = (% c).to(Cluster)
    doAssert c == cFromJson

  block:
    # bug related to #12015
    type
      PixInt = tuple[x, y, ch: int]
      SomePix = Pix | PixInt
      Cluster[T: SomePix] = seq[T]
      ClusterObject[T: SomePix] = object
        data: Cluster[T]
      RecoEvent[T: SomePix] = object
        cluster: seq[ClusterObject[T]]

    let data = @[(x: 123'u8, y: 53'u8, ch: 1231'u16)]
    var c = RecoEvent[Pix](cluster: @[ClusterObject[Pix](data: data)])
    let cFromJson = (% c).to(RecoEvent[Pix])
    doAssert c == cFromJson


  block:
    # ref objects with cycles.
    type
      Misdirection = object
        cycle: Cycle

      Cycle = ref object
        foo: string
        cycle: Misdirection

    let data = """
      {"cycle": null}
    """

    let dataParsed = parseJson(data)
    let dataDeser = to(dataParsed, Misdirection)

  block:
    # ref object from #12316
    type
      Foo = ref Bar
      Bar = object

    discard "null".parseJson.to Foo

  block:
    # named array #12289
    type Vec = array[2, int]
    let arr = "[1,2]".parseJson.to Vec
    doAssert arr == [1,2]

  block:
    # test error message in exception

    type
      MyType = object
        otherMember: string
        member: MySubType

      MySubType = object
        somethingElse: string
        list: seq[MyData]

      MyData = object
        value: int

    let jsonNode = parseJson("""
      {
        "otherMember": "otherValue",
        "member": {
          "somethingElse": "something",
          "list": [{"value": 1}, {"value": 2}, {}]
        }
      }
    """)

    try:
      let tmp = jsonNode.to(MyType)
      doAssert false, "this should be unreachable"
    except KeyError:
      doAssert getCurrentExceptionMsg().contains ".member.list[2].value"

  block:
    # Enum indexed array test
    type Test = enum
      one, two, three, four, five
    let a = [
      one: 300,
      two: 20,
      three: 10,
      four: 0,
      five: -10
    ]
    doAssert (%* a).to(a.typeof) == a


testJson()
static:
  testJson()