tjsonutils.nim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. targets: "c cpp js"
  4. """
  5. import std/jsonutils
  6. import std/json
  7. from std/math import isNaN, signbit
  8. from std/fenv import epsilon
  9. from stdtest/testutils import whenRuntimeJs
  10. import std/[assertions, objectdollar]
  11. proc testRoundtrip[T](t: T, expected: string) =
  12. # checks that `T => json => T2 => json2` is such that json2 = json
  13. let j = t.toJson
  14. doAssert $j == expected, "\n" & $j & "\n" & expected
  15. doAssert j.jsonTo(T).toJson == j
  16. var t2: T = default(T)
  17. t2.fromJson(j)
  18. doAssert t2.toJson == j
  19. proc testRoundtripVal[T](t: T, expected: string) =
  20. # similar to testRoundtrip, but also checks that the `T => json => T2` is such that `T2 == T`
  21. # note that this isn't always possible, e.g. for pointer-like types.
  22. let j = t.toJson
  23. let j2 = $j
  24. doAssert j2 == expected, j2
  25. let j3 = j2.parseJson
  26. let t2 = j3.jsonTo(T)
  27. doAssert t2 == t
  28. doAssert $t2.toJson == j2 # still needed, because -0.0 = 0.0 but their json representation differs
  29. import tables, sets, algorithm, sequtils, options, strtabs
  30. from strutils import contains
  31. type Foo = ref object
  32. id: int
  33. proc `==`(a, b: Foo): bool =
  34. a.id == b.id
  35. type MyEnum = enum me0, me1 = "me1Alt", me2, me3, me4
  36. proc `$`(a: MyEnum): string =
  37. # putting this here pending https://github.com/nim-lang/Nim/issues/13747
  38. if a == me2: "me2Modif"
  39. else: system.`$`(a)
  40. template fn() =
  41. block: # toJson, jsonTo
  42. type Foo = distinct float
  43. testRoundtrip('x', """120""")
  44. when not defined(js):
  45. testRoundtrip(cast[pointer](12345)): """12345"""
  46. when nimvm:
  47. discard
  48. # bugs:
  49. # Error: unhandled exception: 'intVal' is not accessible using discriminant 'kind' of type 'TNode' [
  50. # Error: VM does not support 'cast' from tyNil to tyPointer
  51. else:
  52. testRoundtrip(pointer(nil)): """0"""
  53. testRoundtrip(cast[pointer](nil)): """0"""
  54. # refs bug #9423
  55. testRoundtrip(Foo(1.5)): """1.5"""
  56. block: # OrderedTable
  57. testRoundtrip({"z": "Z", "y": "Y"}.toOrderedTable): """{"z":"Z","y":"Y"}"""
  58. doAssert toJson({"z": 10, "": 11}.newTable).`$`.contains """"":11""" # allows hash to change
  59. testRoundtrip({"z".cstring: 1, "".cstring: 2}.toOrderedTable): """{"z":1,"":2}"""
  60. testRoundtrip({"z": (f1: 'f'), }.toTable): """{"z":{"f1":102}}"""
  61. block: # StringTable
  62. testRoundtrip({"name": "John", "city": "Monaco"}.newStringTable): """{"mode":"modeCaseSensitive","table":{"city":"Monaco","name":"John"}}"""
  63. block: # complex example
  64. let t = {"z": "Z", "y": "Y"}.newStringTable
  65. type A = ref object
  66. a1: string
  67. 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, cstring1: "foo", cstring2: "", cstring3: cstring(nil)))
  68. testRoundtrip(a):
  69. """[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,"cstring1":"foo","cstring2":"","cstring3":null}]"""
  70. block:
  71. # edge case when user defined `==` doesn't handle `nil` well, e.g.:
  72. # https://github.com/nim-lang/nimble/blob/63695f490728e3935692c29f3d71944d83bb1e83/src/nimblepkg/version.nim#L105
  73. testRoundtrip(@[Foo(id: 10), nil]): """[{"id":10},null]"""
  74. block: # enum
  75. type Foo = enum f1, f2, f3, f4, f5
  76. type Bar = enum b1, b2, b3, b4
  77. let a = [f2: b2, f3: b3, f4: b4]
  78. doAssert b2.ord == 1 # explains the `1`
  79. testRoundtrip(a): """[1,2,3]"""
  80. block: # JsonNode
  81. let a = ((1, 2.5, "abc").toJson, (3, 4.5, "foo"))
  82. testRoundtripVal(a): """[[1,2.5,"abc"],[3,4.5,"foo"]]"""
  83. block:
  84. template toInt(a): untyped = cast[int](a)
  85. let a = 3.toJson
  86. let b = (a, a)
  87. let c1 = b.toJson
  88. doAssert c1[0].toInt == a.toInt
  89. doAssert c1[1].toInt == a.toInt
  90. let c2 = b.toJson(ToJsonOptions(jsonNodeMode: joptJsonNodeAsCopy))
  91. doAssert c2[0].toInt != a.toInt
  92. doAssert c2[1].toInt != c2[0].toInt
  93. doAssert c2[1] == c2[0]
  94. let c3 = b.toJson(ToJsonOptions(jsonNodeMode: joptJsonNodeAsObject))
  95. doAssert $c3 == """[{"isUnquoted":false,"kind":2,"num":3},{"isUnquoted":false,"kind":2,"num":3}]"""
  96. block: # ToJsonOptions
  97. let a = (me1, me2)
  98. doAssert $a.toJson() == "[1,2]"
  99. doAssert $a.toJson(ToJsonOptions(enumMode: joptEnumSymbol)) == """["me1","me2"]"""
  100. doAssert $a.toJson(ToJsonOptions(enumMode: joptEnumString)) == """["me1Alt","me2Modif"]"""
  101. block: # set
  102. type Foo = enum f1, f2, f3, f4, f5
  103. type Goo = enum g1 = 10, g2 = 15, g3 = 17, g4
  104. let a = ({f1, f3}, {1'u8, 7'u8}, {'0'..'9'}, {123'u16, 456, 789, 1121, 1122, 1542}, {g2, g3})
  105. testRoundtrip(a): """[[0,2],[1,7],[48,49,50,51,52,53,54,55,56,57],[123,456,789,1121,1122,1542],[15,17]]"""
  106. block: # bug #17383
  107. block:
  108. let a = (int32.high, uint32.high)
  109. testRoundtrip(a): "[2147483647,4294967295]"
  110. when int.sizeof > 4:
  111. block:
  112. let a = (int64.high, uint64.high)
  113. testRoundtrip(a): "[9223372036854775807,18446744073709551615]"
  114. block:
  115. let a = (int.high, uint.high)
  116. when int.sizeof == 4:
  117. testRoundtrip(a): "[2147483647,4294967295]"
  118. else:
  119. testRoundtrip(a): "[9223372036854775807,18446744073709551615]"
  120. block: # bug #18007
  121. testRoundtrip((NaN, Inf, -Inf, 0.0, -0.0, 1.0)): """["nan","inf","-inf",0.0,-0.0,1.0]"""
  122. testRoundtrip((float32(NaN), Inf, -Inf, 0.0, -0.0, 1.0)): """["nan","inf","-inf",0.0,-0.0,1.0]"""
  123. testRoundtripVal((Inf, -Inf, 0.0, -0.0, 1.0)): """["inf","-inf",0.0,-0.0,1.0]"""
  124. doAssert ($NaN.toJson).parseJson.jsonTo(float).isNaN
  125. block: # bug #18009; unfixable unless we change parseJson (which would have overhead),
  126. # but at least we can guarantee that the distinction between 0.0 and -0.0 is preserved.
  127. let a = (0, 0.0, -0.0, 0.5, 1, 1.0)
  128. testRoundtripVal(a): "[0,0.0,-0.0,0.5,1,1.0]"
  129. let a2 = $($a.toJson).parseJson
  130. whenRuntimeJs:
  131. doAssert a2 == "[0,0,-0.0,0.5,1,1]"
  132. do:
  133. doAssert a2 == "[0,0.0,-0.0,0.5,1,1.0]"
  134. let b = a2.parseJson.jsonTo(type(a))
  135. doAssert not b[1].signbit
  136. doAssert b[2].signbit
  137. doAssert not b[3].signbit
  138. block: # bug #15397, bug #13196
  139. let a = 0.1
  140. let x = 0.12345678901234567890123456789
  141. let b = (a + 0.2, 0.3, x)
  142. testRoundtripVal(b): "[0.30000000000000004,0.3,0.12345678901234568]"
  143. testRoundtripVal(0.12345678901234567890123456789): "0.12345678901234568"
  144. testRoundtripVal(epsilon(float64)): "2.220446049250313e-16"
  145. testRoundtripVal(1.0 + epsilon(float64)): "1.0000000000000002"
  146. block: # case object
  147. type Foo = object
  148. x0: float
  149. case t1: bool
  150. of true: z1: int8
  151. of false: z2: uint16
  152. x1: string
  153. testRoundtrip(Foo(t1: true, z1: 5, x1: "bar")): """{"x0":0.0,"t1":true,"z1":5,"x1":"bar"}"""
  154. testRoundtrip(Foo(x0: 1.5, t1: false, z2: 6)): """{"x0":1.5,"t1":false,"z2":6,"x1":""}"""
  155. type PFoo = ref Foo
  156. testRoundtrip(PFoo(x0: 1.5, t1: false, z2: 6)): """{"x0":1.5,"t1":false,"z2":6,"x1":""}"""
  157. block: # ref case object
  158. type Foo = ref object
  159. x0: float
  160. case t1: bool
  161. of true: z1: int8
  162. of false: z2: uint16
  163. x1: string
  164. testRoundtrip(Foo(t1: true, z1: 5, x1: "bar")): """{"x0":0.0,"t1":true,"z1":5,"x1":"bar"}"""
  165. testRoundtrip(Foo(x0: 1.5, t1: false, z2: 6)): """{"x0":1.5,"t1":false,"z2":6,"x1":""}"""
  166. block: # generic case object
  167. type Foo[T] = ref object
  168. x0: float
  169. case t1: bool
  170. of true: z1: int8
  171. of false: z2: uint16
  172. x1: string
  173. testRoundtrip(Foo[float](t1: true, z1: 5, x1: "bar")): """{"x0":0.0,"t1":true,"z1":5,"x1":"bar"}"""
  174. testRoundtrip(Foo[int](x0: 1.5, t1: false, z2: 6)): """{"x0":1.5,"t1":false,"z2":6,"x1":""}"""
  175. # sanity check: nesting inside a tuple
  176. testRoundtrip((Foo[int](x0: 1.5, t1: false, z2: 6), "foo")): """[{"x0":1.5,"t1":false,"z2":6,"x1":""},"foo"]"""
  177. block: # generic case object using generic type
  178. type Foo[T] = ref object
  179. x0: float
  180. case t1: bool
  181. of true: z1: int8
  182. of false: z2: uint16
  183. x1: string
  184. x2: T
  185. testRoundtrip(Foo[float](t1: true, z1: 5, x1: "bar", x2: 2.5)): """{"x0":0.0,"t1":true,"z1":5,"x1":"bar","x2":2.5}"""
  186. testRoundtrip(Foo[int](x0: 1.5, t1: false, z2: 6, x2: 2)): """{"x0":1.5,"t1":false,"z2":6,"x1":"","x2":2}"""
  187. # sanity check: nesting inside a tuple
  188. testRoundtrip((Foo[int](x0: 1.5, t1: false, z2: 6, x2: 2), "foo")): """[{"x0":1.5,"t1":false,"z2":6,"x1":"","x2":2},"foo"]"""
  189. block: # case object: 2 discriminants, `when` branch, range discriminant
  190. type Foo[T] = object
  191. case t1: bool
  192. of true:
  193. z1: int8
  194. of false:
  195. z2: uint16
  196. when T is float:
  197. case t2: range[0..3]
  198. of 0: z3: int8
  199. of 2,3: z4: uint16
  200. else: discard
  201. testRoundtrip(Foo[float](t1: true, z1: 5, t2: 3, z4: 12)): """{"t1":true,"z1":5,"t2":3,"z4":12}"""
  202. testRoundtrip(Foo[int](t1: false, z2: 7)): """{"t1":false,"z2":7}"""
  203. # pending https://github.com/nim-lang/Nim/issues/14698, test with `type Foo[T] = ref object`
  204. block: # bug: pass opt params in fromJson
  205. type Foo = object
  206. a: int
  207. b: string
  208. c: float
  209. type Bar = object
  210. foo: Foo
  211. boo: string
  212. var f: seq[Foo]
  213. try:
  214. fromJson(f, parseJson """[{"b": "bbb"}]""")
  215. doAssert false
  216. except ValueError:
  217. doAssert true
  218. fromJson(f, parseJson """[{"b": "bbb"}]""", Joptions(allowExtraKeys: true, allowMissingKeys: true))
  219. doAssert f == @[Foo(a: 0, b: "bbb", c: 0.0)]
  220. var b: Bar
  221. fromJson(b, parseJson """{"foo": {"b": "bbb"}}""", Joptions(allowExtraKeys: true, allowMissingKeys: true))
  222. doAssert b == Bar(foo: Foo(a: 0, b: "bbb", c: 0.0))
  223. block: # jsonTo with `opt`
  224. let b2 = """{"foo": {"b": "bbb"}}""".parseJson.jsonTo(Bar, Joptions(allowExtraKeys: true, allowMissingKeys: true))
  225. doAssert b2 == Bar(foo: Foo(a: 0, b: "bbb", c: 0.0))
  226. block testHashSet:
  227. testRoundtrip(HashSet[string]()): "[]"
  228. testRoundtrip([""].toHashSet): """[""]"""
  229. testRoundtrip(["one"].toHashSet): """["one"]"""
  230. var s: HashSet[string]
  231. fromJson(s, parseJson("""["one","two"]"""))
  232. doAssert s == ["one", "two"].toHashSet
  233. let jsonNode = toJson(s)
  234. doAssert jsonNode.elems.mapIt(it.str).sorted == @["one", "two"]
  235. block testOrderedSet:
  236. testRoundtrip(["one", "two", "three"].toOrderedSet):
  237. """["one","two","three"]"""
  238. block testOption:
  239. testRoundtrip(some("test")): "\"test\""
  240. testRoundtrip(none[string]()): "null"
  241. testRoundtrip(some(42)): "42"
  242. testRoundtrip(none[int]()): "null"
  243. block testStrtabs:
  244. testRoundtrip(newStringTable(modeStyleInsensitive)):
  245. """{"mode":"modeStyleInsensitive","table":{}}"""
  246. testRoundtrip(
  247. newStringTable("name", "John", "surname", "Doe", modeCaseSensitive)):
  248. """{"mode":"modeCaseSensitive","table":{"name":"John","surname":"Doe"}}"""
  249. block testJoptions:
  250. type
  251. AboutLifeUniverseAndEverythingElse = object
  252. question: string
  253. answer: int
  254. block testExceptionOnExtraKeys:
  255. var guide: AboutLifeUniverseAndEverythingElse
  256. let json = parseJson(
  257. """{"question":"6*9=?","answer":42,"author":"Douglas Adams"}""")
  258. doAssertRaises ValueError, fromJson(guide, json)
  259. doAssertRaises ValueError,
  260. fromJson(guide, json, Joptions(allowMissingKeys: true))
  261. type
  262. A = object
  263. a1,a2,a3: int
  264. var a: A
  265. let j = parseJson("""{"a3": 1, "a4": 2}""")
  266. doAssertRaises ValueError,
  267. fromJson(a, j, Joptions(allowMissingKeys: true))
  268. block testExceptionOnMissingKeys:
  269. var guide: AboutLifeUniverseAndEverythingElse
  270. let json = parseJson("""{"answer":42}""")
  271. doAssertRaises ValueError, fromJson(guide, json)
  272. doAssertRaises ValueError,
  273. fromJson(guide, json, Joptions(allowExtraKeys: true))
  274. block testAllowExtraKeys:
  275. var guide: AboutLifeUniverseAndEverythingElse
  276. let json = parseJson(
  277. """{"question":"6*9=?","answer":42,"author":"Douglas Adams"}""")
  278. fromJson(guide, json, Joptions(allowExtraKeys: true))
  279. doAssert guide == AboutLifeUniverseAndEverythingElse(
  280. question: "6*9=?", answer: 42)
  281. block refObject: #bug 17986
  282. type A = ref object
  283. case is_a: bool
  284. of true:
  285. a: int
  286. else:
  287. b: int
  288. var a = A()
  289. fromJson(a, """{"is_a": true, "a":1, "extra_key": 1}""".parseJson, Joptions(allowExtraKeys: true))
  290. doAssert $a[] == "(is_a: true, a: 1)"
  291. block testAllowMissingKeys:
  292. var guide = AboutLifeUniverseAndEverythingElse(
  293. question: "6*9=?", answer: 54)
  294. let json = parseJson("""{"answer":42}""")
  295. fromJson(guide, json, Joptions(allowMissingKeys: true))
  296. doAssert guide == AboutLifeUniverseAndEverythingElse(
  297. question: "6*9=?", answer: 42)
  298. block testAllowExtraAndMissingKeys:
  299. var guide = AboutLifeUniverseAndEverythingElse(
  300. question: "6*9=?", answer: 54)
  301. let json = parseJson(
  302. """{"answer":42,"author":"Douglas Adams"}""")
  303. fromJson(guide, json, Joptions(
  304. allowExtraKeys: true, allowMissingKeys: true))
  305. doAssert guide == AboutLifeUniverseAndEverythingElse(
  306. question: "6*9=?", answer: 42)
  307. type
  308. Foo = object
  309. a: array[2, string]
  310. case b: bool
  311. of false: f: float
  312. of true: t: tuple[i: int, s: string]
  313. case c: range[0 .. 2]
  314. of 0: c0: int
  315. of 1: c1: float
  316. of 2: c2: string
  317. block testExceptionOnMissingDiscriminantKey:
  318. var foo: Foo
  319. let json = parseJson("""{"a":["one","two"]}""")
  320. doAssertRaises ValueError, fromJson(foo, json)
  321. block testDoNotResetMissingFieldsWhenHaveDiscriminantKey:
  322. var foo = Foo(a: ["one", "two"], b: true, t: (i: 42, s: "s"),
  323. c: 0, c0: 1)
  324. let json = parseJson("""{"b":true,"c":2}""")
  325. fromJson(foo, json, Joptions(allowMissingKeys: true))
  326. doAssert foo.a == ["one", "two"]
  327. doAssert foo.b
  328. doAssert foo.t == (i: 42, s: "s")
  329. doAssert foo.c == 2
  330. doAssert foo.c2 == ""
  331. block testAllowMissingDiscriminantKeys:
  332. var foo: Foo
  333. let json = parseJson("""{"a":["one","two"],"c":1,"c1":3.14159}""")
  334. fromJson(foo, json, Joptions(allowMissingKeys: true))
  335. doAssert foo.a == ["one", "two"]
  336. doAssert not foo.b
  337. doAssert foo.f == 0.0
  338. doAssert foo.c == 1
  339. doAssert foo.c1 == 3.14159
  340. block testExceptionOnWrongDiscirminatBranchInJson:
  341. var foo = Foo(b: false, f: 3.14159, c: 0, c0: 42)
  342. let json = parseJson("""{"c2": "hello"}""")
  343. doAssertRaises ValueError,
  344. fromJson(foo, json, Joptions(allowMissingKeys: true))
  345. # Test that the original fields are not reset.
  346. doAssert not foo.b
  347. doAssert foo.f == 3.14159
  348. doAssert foo.c == 0
  349. doAssert foo.c0 == 42
  350. block testNoExceptionOnRightDiscriminantBranchInJson:
  351. var foo = Foo(b: false, f: 0, c:1, c1: 0)
  352. let json = parseJson("""{"f":2.71828,"c1": 3.14159}""")
  353. fromJson(foo, json, Joptions(allowMissingKeys: true))
  354. doAssert not foo.b
  355. doAssert foo.f == 2.71828
  356. doAssert foo.c == 1
  357. doAssert foo.c1 == 3.14159
  358. block testAllowExtraKeysInJsonOnWrongDisciriminatBranch:
  359. var foo = Foo(b: false, f: 3.14159, c: 0, c0: 42)
  360. let json = parseJson("""{"c2": "hello"}""")
  361. fromJson(foo, json, Joptions(allowMissingKeys: true,
  362. allowExtraKeys: true))
  363. # Test that the original fields are not reset.
  364. doAssert not foo.b
  365. doAssert foo.f == 3.14159
  366. doAssert foo.c == 0
  367. doAssert foo.c0 == 42
  368. block testInvalidTupleLength:
  369. let json = parseJson("[0]")
  370. # Should raise ValueError instead of index error
  371. doAssertRaises(ValueError):
  372. discard json.jsonTo((int, int))
  373. type
  374. InnerEnum = enum
  375. A
  376. B
  377. C
  378. InnerObject = object
  379. x: string
  380. y: InnerEnum
  381. block testOptionsArePassedWhenDeserialising:
  382. let json = parseJson("""{"x": "hello"}""")
  383. let inner = json.jsonTo(Option[InnerObject], Joptions(allowMissingKeys: true))
  384. doAssert inner.isSome()
  385. doAssert inner.get().x == "hello"
  386. doAssert inner.get().y == A
  387. block testOptionsArePassedWhenSerialising:
  388. let inner = some InnerObject(x: "hello", y: A)
  389. let json = inner.toJson(ToJsonOptions(enumMode: joptEnumSymbol))
  390. doAssert $json == """{"x":"hello","y":"A"}"""
  391. block: # bug #21638
  392. type Something = object
  393. doAssert "{}".parseJson.jsonTo(Something) == Something()
  394. when false:
  395. ## TODO: Implement support for nested variant objects allowing the tests
  396. ## bellow to pass.
  397. block testNestedVariantObjects:
  398. type
  399. Variant = object
  400. case b: bool
  401. of false:
  402. case bf: bool
  403. of false: bff: int
  404. of true: bft: float
  405. of true:
  406. case bt: bool
  407. of false: btf: string
  408. of true: btt: char
  409. testRoundtrip(Variant(b: false, bf: false, bff: 42)):
  410. """{"b": false, "bf": false, "bff": 42}"""
  411. testRoundtrip(Variant(b: false, bf: true, bft: 3.14159)):
  412. """{"b": false, "bf": true, "bft": 3.14159}"""
  413. testRoundtrip(Variant(b: true, bt: false, btf: "test")):
  414. """{"b": true, "bt": false, "btf": "test"}"""
  415. testRoundtrip(Variant(b: true, bt: true, btt: 'c')):
  416. """{"b": true, "bt": true, "btt": "c"}"""
  417. # TODO: Add additional tests with missing and extra JSON keys, both when
  418. # allowed and forbidden analogous to the tests for the not nested
  419. # variant objects.
  420. static: fn()
  421. fn()