tmarshal.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. discard """
  2. output: '''{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}
  3. true
  4. true
  5. alpha 100
  6. omega 200
  7. Some(null)
  8. None[JsonNode]
  9. (numeric: "")
  10. hello world
  11. '''
  12. joinable: false
  13. """
  14. #[
  15. joinable: false pending https://github.com/nim-lang/Nim/issues/9754
  16. ]#
  17. import marshal
  18. template testit(x) = discard $$to[typeof(x)]($$x)
  19. var x: array[0..4, array[0..4, string]] = [
  20. ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
  21. ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
  22. ["test", "1", "2", "3", "4"]]
  23. testit(x)
  24. var test2: tuple[name: string, s: int] = ("tuple test", 56)
  25. testit(test2)
  26. type
  27. TE = enum
  28. blah, blah2
  29. TestObj = object
  30. test, asd: int
  31. case test2: TE
  32. of blah:
  33. help: string
  34. else:
  35. discard
  36. PNode = ref TNode
  37. TNode = object
  38. next, prev: PNode
  39. data: string
  40. proc buildList(): PNode =
  41. new(result)
  42. new(result.next)
  43. new(result.prev)
  44. result.data = "middle"
  45. result.next.data = "next"
  46. result.prev.data = "prev"
  47. result.next.next = result.prev
  48. result.next.prev = result
  49. result.prev.next = result
  50. result.prev.prev = result.next
  51. var test3: TestObj
  52. test3.test = 42
  53. test3.test2 = blah
  54. testit(test3)
  55. var test4: ref tuple[a, b: string]
  56. new(test4)
  57. test4.a = "ref string test: A"
  58. test4.b = "ref string test: B"
  59. testit(test4)
  60. var test5 = @[(0,1),(2,3),(4,5)]
  61. testit(test5)
  62. var test7 = buildList()
  63. testit(test7)
  64. var test6: set[char] = {'A'..'Z', '_'}
  65. testit(test6)
  66. # bug #1352
  67. type
  68. Entity = object of RootObj
  69. name: string
  70. Person = object of Entity
  71. age: int
  72. bio: string
  73. blob: string
  74. var instance1 = Person(name: "Cletus", age: 12,
  75. bio: "Я Cletus",
  76. blob: "ABC\x80")
  77. echo($$instance1)
  78. echo(to[Person]($$instance1).bio == instance1.bio) # true
  79. echo(to[Person]($$instance1).blob == instance1.blob) # true
  80. # bug 5757
  81. type
  82. Something = object
  83. x: string
  84. y: int
  85. var data1 = """{"x": "alpha", "y": 100}"""
  86. var data2 = """{"x": "omega", "y": 200}"""
  87. var r = to[Something](data1)
  88. echo r.x, " ", r.y
  89. r = to[Something](data2)
  90. echo r.x, " ", r.y
  91. type
  92. Foo = object
  93. a1: string
  94. a2: string
  95. a3: seq[string]
  96. a4: seq[int]
  97. a5: seq[int]
  98. a6: seq[int]
  99. var foo = Foo(a2: "", a4: @[], a6: @[1])
  100. foo.a6.setLen 0
  101. doAssert $$foo == """{"a1": "", "a2": "", "a3": [], "a4": [], "a5": [], "a6": []}"""
  102. testit(foo)
  103. import options, json
  104. # bug #15934
  105. block:
  106. let
  107. a1 = some(newJNull())
  108. a2 = none(JsonNode)
  109. echo ($$a1).to[:Option[JsonNode]]
  110. echo ($$a2).to[:Option[JsonNode]]
  111. # bug #15620
  112. block:
  113. let str = """{"numeric": null}"""
  114. type
  115. LegacyEntry = object
  116. numeric: string
  117. let test = to[LegacyEntry](str)
  118. echo test
  119. # bug #16022
  120. block:
  121. let p: proc () = proc () = echo "hello world"
  122. let poc = (to[typeof(p)]($$p))
  123. poc()