tjsonutils.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. import std/jsonutils
  5. import std/json
  6. proc testRoundtrip[T](t: T, expected: string) =
  7. let j = t.toJson
  8. doAssert $j == expected, $j
  9. doAssert j.jsonTo(T).toJson == j
  10. var t2: T
  11. t2.fromJson(j)
  12. doAssert t2.toJson == j
  13. import tables
  14. import strtabs
  15. type Foo = ref object
  16. id: int
  17. proc `==`(a, b: Foo): bool =
  18. a.id == b.id
  19. template fn() =
  20. block: # toJson, jsonTo
  21. type Foo = distinct float
  22. testRoundtrip('x', """120""")
  23. when not defined(js):
  24. testRoundtrip(cast[pointer](12345)): """12345"""
  25. when nimvm:
  26. discard
  27. # bugs:
  28. # Error: unhandled exception: 'intVal' is not accessible using discriminant 'kind' of type 'TNode' [
  29. # Error: VM does not support 'cast' from tyNil to tyPointer
  30. else:
  31. testRoundtrip(pointer(nil)): """0"""
  32. testRoundtrip(cast[pointer](nil)): """0"""
  33. # causes workaround in `fromJson` potentially related to
  34. # https://github.com/nim-lang/Nim/issues/12282
  35. testRoundtrip(Foo(1.5)): """1.5"""
  36. block:
  37. testRoundtrip({"z": "Z", "y": "Y"}.toOrderedTable): """{"z":"Z","y":"Y"}"""
  38. when not defined(js): # pending https://github.com/nim-lang/Nim/issues/14574
  39. testRoundtrip({"z": (f1: 'f'), }.toTable): """{"z":{"f1":102}}"""
  40. block:
  41. testRoundtrip({"name": "John", "city": "Monaco"}.newStringTable): """{"mode":"modeCaseSensitive","table":{"city":"Monaco","name":"John"}}"""
  42. block: # complex example
  43. let t = {"z": "Z", "y": "Y"}.newStringTable
  44. type A = ref object
  45. a1: string
  46. let a = (1.1, "fo", 'x', @[10,11], [true, false], [t,newStringTable()], [0'i8,3'i8], -4'i16, (foo: 0.5'f32, bar: A(a1: "abc"), bar2: A.default))
  47. testRoundtrip(a):
  48. """[1.1,"fo",120,[10,11],[true,false],[{"mode":"modeCaseSensitive","table":{"y":"Y","z":"Z"}},{"mode":"modeCaseSensitive","table":{}}],[0,3],-4,{"foo":0.5,"bar":{"a1":"abc"},"bar2":null}]"""
  49. block:
  50. # edge case when user defined `==` doesn't handle `nil` well, eg:
  51. # https://github.com/nim-lang/nimble/blob/63695f490728e3935692c29f3d71944d83bb1e83/src/nimblepkg/version.nim#L105
  52. testRoundtrip(@[Foo(id: 10), nil]): """[{"id":10},null]"""
  53. block:
  54. type Foo = enum f1, f2, f3, f4, f5
  55. type Bar = enum b1, b2, b3, b4
  56. let a = [f2: b2, f3: b3, f4: b4]
  57. doAssert b2.ord == 1 # explains the `1`
  58. testRoundtrip(a): """[1,2,3]"""
  59. block: # case object
  60. type Foo = object
  61. x0: float
  62. case t1: bool
  63. of true: z1: int8
  64. of false: z2: uint16
  65. x1: string
  66. testRoundtrip(Foo(t1: true, z1: 5, x1: "bar")): """{"x0":0.0,"t1":true,"z1":5,"x1":"bar"}"""
  67. testRoundtrip(Foo(x0: 1.5, t1: false, z2: 6)): """{"x0":1.5,"t1":false,"z2":6,"x1":""}"""
  68. type PFoo = ref Foo
  69. testRoundtrip(PFoo(x0: 1.5, t1: false, z2: 6)): """{"x0":1.5,"t1":false,"z2":6,"x1":""}"""
  70. block: # ref case object
  71. type Foo = ref object
  72. x0: float
  73. case t1: bool
  74. of true: z1: int8
  75. of false: z2: uint16
  76. x1: string
  77. testRoundtrip(Foo(t1: true, z1: 5, x1: "bar")): """{"x0":0.0,"t1":true,"z1":5,"x1":"bar"}"""
  78. testRoundtrip(Foo(x0: 1.5, t1: false, z2: 6)): """{"x0":1.5,"t1":false,"z2":6,"x1":""}"""
  79. block: # generic case object
  80. type Foo[T] = ref object
  81. x0: float
  82. case t1: bool
  83. of true: z1: int8
  84. of false: z2: uint16
  85. x1: string
  86. testRoundtrip(Foo[float](t1: true, z1: 5, x1: "bar")): """{"x0":0.0,"t1":true,"z1":5,"x1":"bar"}"""
  87. testRoundtrip(Foo[int](x0: 1.5, t1: false, z2: 6)): """{"x0":1.5,"t1":false,"z2":6,"x1":""}"""
  88. # sanity check: nesting inside a tuple
  89. testRoundtrip((Foo[int](x0: 1.5, t1: false, z2: 6), "foo")): """[{"x0":1.5,"t1":false,"z2":6,"x1":""},"foo"]"""
  90. block: # case object: 2 discriminants, `when` branch, range discriminant
  91. type Foo[T] = object
  92. case t1: bool
  93. of true:
  94. z1: int8
  95. of false:
  96. z2: uint16
  97. when T is float:
  98. case t2: range[0..3]
  99. of 0: z3: int8
  100. of 2,3: z4: uint16
  101. else: discard
  102. testRoundtrip(Foo[float](t1: true, z1: 5, t2: 3, z4: 12)): """{"t1":true,"z1":5,"t2":3,"z4":12}"""
  103. testRoundtrip(Foo[int](t1: false, z2: 7)): """{"t1":false,"z2":7}"""
  104. # pending https://github.com/nim-lang/Nim/issues/14698, test with `type Foo[T] = ref object`
  105. static: fn()
  106. fn()