jsonutils.nim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. ##[
  2. This module implements a hookable (de)serialization for arbitrary types.
  3. Design goal: avoid importing modules where a custom serialization is needed;
  4. see strtabs.fromJsonHook,toJsonHook for an example.
  5. ]##
  6. runnableExamples:
  7. import std/[strtabs,json]
  8. type Foo = ref object
  9. t: bool
  10. z1: int8
  11. let a = (1.5'f32, (b: "b2", a: "a2"), 'x', @[Foo(t: true, z1: -3), nil], [{"name": "John"}.newStringTable])
  12. let j = a.toJson
  13. assert j.jsonTo(typeof(a)).toJson == j
  14. assert $[NaN, Inf, -Inf, 0.0, -0.0, 1.0, 1e-2].toJson == """["nan","inf","-inf",0.0,-0.0,1.0,0.01]"""
  15. assert 0.0.toJson.kind == JFloat
  16. assert Inf.toJson.kind == JString
  17. import json, strutils, tables, sets, strtabs, options
  18. #[
  19. Future directions:
  20. add a way to customize serialization, for e.g.:
  21. * field renaming
  22. * allow serializing `enum` and `char` as `string` instead of `int`
  23. (enum is more compact/efficient, and robust to enum renamings, but string
  24. is more human readable)
  25. * handle cyclic references, using a cache of already visited addresses
  26. * implement support for serialization and de-serialization of nested variant
  27. objects.
  28. ]#
  29. import macros
  30. from enumutils import symbolName
  31. from typetraits import OrdinalEnum
  32. when defined(nimPreviewSlimSystem):
  33. import std/assertions
  34. when not defined(nimFixedForwardGeneric):
  35. # xxx remove pending csources_v1 update >= 1.2.0
  36. proc to[T](node: JsonNode, t: typedesc[T]): T =
  37. when T is string: node.getStr
  38. elif T is bool: node.getBool
  39. else: static: doAssert false, $T # support as needed (only needed during bootstrap)
  40. proc isNamedTuple(T: typedesc): bool = # old implementation
  41. when T isnot tuple: result = false
  42. else:
  43. var t: T
  44. for name, _ in t.fieldPairs:
  45. when name == "Field0": return compiles(t.Field0)
  46. else: return true
  47. return false
  48. else:
  49. proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
  50. type
  51. Joptions* = object # xxx rename FromJsonOptions
  52. ## Options controlling the behavior of `fromJson`.
  53. allowExtraKeys*: bool
  54. ## If `true` Nim's object to which the JSON is parsed is not required to
  55. ## have a field for every JSON key.
  56. allowMissingKeys*: bool
  57. ## If `true` Nim's object to which JSON is parsed is allowed to have
  58. ## fields without corresponding JSON keys.
  59. # in future work: a key rename could be added
  60. EnumMode* = enum
  61. joptEnumOrd
  62. joptEnumSymbol
  63. joptEnumString
  64. JsonNodeMode* = enum ## controls `toJson` for JsonNode types
  65. joptJsonNodeAsRef ## returns the ref as is
  66. joptJsonNodeAsCopy ## returns a deep copy of the JsonNode
  67. joptJsonNodeAsObject ## treats JsonNode as a regular ref object
  68. ToJsonOptions* = object
  69. enumMode*: EnumMode
  70. jsonNodeMode*: JsonNodeMode
  71. # xxx charMode, etc
  72. proc initToJsonOptions*(): ToJsonOptions =
  73. ## initializes `ToJsonOptions` with sane options.
  74. ToJsonOptions(enumMode: joptEnumOrd, jsonNodeMode: joptJsonNodeAsRef)
  75. proc distinctBase(T: typedesc, recursive: static bool = true): typedesc {.magic: "TypeTrait".}
  76. template distinctBase[T](a: T, recursive: static bool = true): untyped = distinctBase(typeof(a), recursive)(a)
  77. macro getDiscriminants(a: typedesc): seq[string] =
  78. ## return the discriminant keys
  79. # candidate for std/typetraits
  80. var a = a.getTypeImpl
  81. doAssert a.kind == nnkBracketExpr
  82. let sym = a[1]
  83. let t = sym.getTypeImpl
  84. let t2 = t[2]
  85. doAssert t2.kind == nnkRecList
  86. result = newTree(nnkBracket)
  87. for ti in t2:
  88. if ti.kind == nnkRecCase:
  89. let key = ti[0][0]
  90. let typ = ti[0][1]
  91. result.add newLit key.strVal
  92. if result.len > 0:
  93. result = quote do:
  94. @`result`
  95. else:
  96. result = quote do:
  97. seq[string].default
  98. macro initCaseObject(T: typedesc, fun: untyped): untyped =
  99. ## does the minimum to construct a valid case object, only initializing
  100. ## the discriminant fields; see also `getDiscriminants`
  101. # maybe candidate for std/typetraits
  102. var a = T.getTypeImpl
  103. doAssert a.kind == nnkBracketExpr
  104. let sym = a[1]
  105. let t = sym.getTypeImpl
  106. var t2: NimNode
  107. case t.kind
  108. of nnkObjectTy: t2 = t[2]
  109. of nnkRefTy: t2 = t[0].getTypeImpl[2]
  110. else: doAssert false, $t.kind # xxx `nnkPtrTy` could be handled too
  111. doAssert t2.kind == nnkRecList
  112. result = newTree(nnkObjConstr)
  113. result.add sym
  114. for ti in t2:
  115. if ti.kind == nnkRecCase:
  116. let key = ti[0][0]
  117. let typ = ti[0][1]
  118. let key2 = key.strVal
  119. let val = quote do:
  120. `fun`(`key2`, typedesc[`typ`])
  121. result.add newTree(nnkExprColonExpr, key, val)
  122. proc raiseJsonException(condStr: string, msg: string) {.noinline.} =
  123. # just pick 1 exception type for simplicity; other choices would be:
  124. # JsonError, JsonParser, JsonKindError
  125. raise newException(ValueError, condStr & " failed: " & msg)
  126. template checkJson(cond: untyped, msg = "") =
  127. if not cond:
  128. raiseJsonException(astToStr(cond), msg)
  129. proc hasField[T](obj: T, field: string): bool =
  130. for k, _ in fieldPairs(obj):
  131. if k == field:
  132. return true
  133. return false
  134. macro accessField(obj: typed, name: static string): untyped =
  135. newDotExpr(obj, ident(name))
  136. template fromJsonFields(newObj, oldObj, json, discKeys, opt) =
  137. type T = typeof(newObj)
  138. # we could customize whether to allow JNull
  139. checkJson json.kind == JObject, $json.kind
  140. var num, numMatched = 0
  141. for key, val in fieldPairs(newObj):
  142. num.inc
  143. when key notin discKeys:
  144. if json.hasKey key:
  145. numMatched.inc
  146. fromJson(val, json[key], opt)
  147. elif opt.allowMissingKeys:
  148. # if there are no discriminant keys the `oldObj` must always have the
  149. # same keys as the new one. Otherwise we must check, because they could
  150. # be set to different branches.
  151. when typeof(oldObj) isnot typeof(nil):
  152. if discKeys.len == 0 or hasField(oldObj, key):
  153. val = accessField(oldObj, key)
  154. else:
  155. checkJson false, $($T, key, json)
  156. else:
  157. if json.hasKey key:
  158. numMatched.inc
  159. let ok =
  160. if opt.allowExtraKeys and opt.allowMissingKeys:
  161. true
  162. elif opt.allowExtraKeys:
  163. # This check is redundant because if here missing keys are not allowed,
  164. # and if `num != numMatched` it will fail in the loop above but it is left
  165. # for clarity.
  166. assert num == numMatched
  167. num == numMatched
  168. elif opt.allowMissingKeys:
  169. json.len == numMatched
  170. else:
  171. json.len == num and num == numMatched
  172. checkJson ok, $(json.len, num, numMatched, $T, json)
  173. proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions())
  174. proc discKeyMatch[T](obj: T, json: JsonNode, key: static string): bool =
  175. if not json.hasKey key:
  176. return true
  177. let field = accessField(obj, key)
  178. var jsonVal: typeof(field)
  179. fromJson(jsonVal, json[key])
  180. if jsonVal != field:
  181. return false
  182. return true
  183. macro discKeysMatchBodyGen(obj: typed, json: JsonNode,
  184. keys: static seq[string]): untyped =
  185. result = newStmtList()
  186. let r = ident("result")
  187. for key in keys:
  188. let keyLit = newLit key
  189. result.add quote do:
  190. `r` = `r` and discKeyMatch(`obj`, `json`, `keyLit`)
  191. proc discKeysMatch[T](obj: T, json: JsonNode, keys: static seq[string]): bool =
  192. result = true
  193. discKeysMatchBodyGen(obj, json, keys)
  194. proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
  195. ## inplace version of `jsonTo`
  196. #[
  197. adding "json path" leading to `b` can be added in future work.
  198. ]#
  199. checkJson b != nil, $($T, b)
  200. when compiles(fromJsonHook(a, b)): fromJsonHook(a, b)
  201. elif T is bool: a = to(b,T)
  202. elif T is enum:
  203. case b.kind
  204. of JInt: a = T(b.getBiggestInt())
  205. of JString: a = parseEnum[T](b.getStr())
  206. else: checkJson false, $($T, " ", b)
  207. elif T is uint|uint64: a = T(to(b, uint64))
  208. elif T is Ordinal: a = cast[T](to(b, int))
  209. elif T is pointer: a = cast[pointer](to(b, int))
  210. elif T is distinct:
  211. when nimvm:
  212. # bug, potentially related to https://github.com/nim-lang/Nim/issues/12282
  213. a = T(jsonTo(b, distinctBase(T)))
  214. else:
  215. a.distinctBase.fromJson(b)
  216. elif T is string|SomeNumber: a = to(b,T)
  217. elif T is cstring:
  218. case b.kind
  219. of JNull: a = nil
  220. of JString: a = b.str
  221. else: checkJson false, $($T, " ", b)
  222. elif T is JsonNode: a = b
  223. elif T is ref | ptr:
  224. if b.kind == JNull: a = nil
  225. else:
  226. a = T()
  227. fromJson(a[], b, opt)
  228. elif T is array:
  229. checkJson a.len == b.len, $(a.len, b.len, $T)
  230. var i = 0
  231. for ai in mitems(a):
  232. fromJson(ai, b[i], opt)
  233. i.inc
  234. elif T is set:
  235. type E = typeof(for ai in a: ai)
  236. for val in b.getElems:
  237. incl a, jsonTo(val, E)
  238. elif T is seq:
  239. a.setLen b.len
  240. for i, val in b.getElems:
  241. fromJson(a[i], val, opt)
  242. elif T is object:
  243. template fun(key, typ): untyped {.used.} =
  244. if b.hasKey key:
  245. jsonTo(b[key], typ)
  246. elif hasField(a, key):
  247. accessField(a, key)
  248. else:
  249. default(typ)
  250. const keys = getDiscriminants(T)
  251. when keys.len == 0:
  252. fromJsonFields(a, nil, b, keys, opt)
  253. else:
  254. if discKeysMatch(a, b, keys):
  255. fromJsonFields(a, nil, b, keys, opt)
  256. else:
  257. var newObj = initCaseObject(T, fun)
  258. fromJsonFields(newObj, a, b, keys, opt)
  259. a = newObj
  260. elif T is tuple:
  261. when isNamedTuple(T):
  262. fromJsonFields(a, nil, b, seq[string].default, opt)
  263. else:
  264. checkJson b.kind == JArray, $(b.kind) # we could customize whether to allow JNull
  265. var i = 0
  266. for val in fields(a):
  267. fromJson(val, b[i], opt)
  268. i.inc
  269. checkJson b.len == i, $(b.len, i, $T, b) # could customize
  270. else:
  271. # checkJson not appropriate here
  272. static: doAssert false, "not yet implemented: " & $T
  273. proc jsonTo*(b: JsonNode, T: typedesc, opt = Joptions()): T =
  274. ## reverse of `toJson`
  275. fromJson(result, b, opt)
  276. proc toJson*[T](a: T, opt = initToJsonOptions()): JsonNode =
  277. ## serializes `a` to json; uses `toJsonHook(a: T)` if it's in scope to
  278. ## customize serialization, see strtabs.toJsonHook for an example.
  279. ##
  280. ## .. note:: With `-d:nimPreviewJsonutilsHoleyEnum`, `toJson` now can
  281. ## serialize/deserialize holey enums as regular enums (via `ord`) instead of as strings.
  282. ## It is expected that this behavior becomes the new default in upcoming versions.
  283. when compiles(toJsonHook(a)): result = toJsonHook(a)
  284. elif T is object | tuple:
  285. when T is object or isNamedTuple(T):
  286. result = newJObject()
  287. for k, v in a.fieldPairs: result[k] = toJson(v, opt)
  288. else:
  289. result = newJArray()
  290. for v in a.fields: result.add toJson(v, opt)
  291. elif T is ref | ptr:
  292. template impl =
  293. if system.`==`(a, nil): result = newJNull()
  294. else: result = toJson(a[], opt)
  295. when T is JsonNode:
  296. case opt.jsonNodeMode
  297. of joptJsonNodeAsRef: result = a
  298. of joptJsonNodeAsCopy: result = copy(a)
  299. of joptJsonNodeAsObject: impl()
  300. else: impl()
  301. elif T is array | seq | set:
  302. result = newJArray()
  303. for ai in a: result.add toJson(ai, opt)
  304. elif T is pointer: result = toJson(cast[int](a), opt)
  305. # edge case: `a == nil` could've also led to `newJNull()`, but this results
  306. # in simpler code for `toJson` and `fromJson`.
  307. elif T is distinct: result = toJson(a.distinctBase, opt)
  308. elif T is bool: result = %(a)
  309. elif T is SomeInteger: result = %a
  310. elif T is enum:
  311. case opt.enumMode
  312. of joptEnumOrd:
  313. when T is Ordinal or defined(nimPreviewJsonutilsHoleyEnum): %(a.ord)
  314. else: toJson($a, opt)
  315. of joptEnumSymbol:
  316. when T is OrdinalEnum:
  317. toJson(symbolName(a), opt)
  318. else:
  319. toJson($a, opt)
  320. of joptEnumString: toJson($a, opt)
  321. elif T is Ordinal: result = %(a.ord)
  322. elif T is cstring: (if a == nil: result = newJNull() else: result = % $a)
  323. else: result = %a
  324. proc fromJsonHook*[K: string|cstring, V](t: var (Table[K, V] | OrderedTable[K, V]),
  325. jsonNode: JsonNode) =
  326. ## Enables `fromJson` for `Table` and `OrderedTable` types.
  327. ##
  328. ## See also:
  329. ## * `toJsonHook proc<#toJsonHook>`_
  330. runnableExamples:
  331. import std/[tables, json]
  332. var foo: tuple[t: Table[string, int], ot: OrderedTable[string, int]]
  333. fromJson(foo, parseJson("""
  334. {"t":{"two":2,"one":1},"ot":{"one":1,"three":3}}"""))
  335. assert foo.t == [("one", 1), ("two", 2)].toTable
  336. assert foo.ot == [("one", 1), ("three", 3)].toOrderedTable
  337. assert jsonNode.kind == JObject,
  338. "The kind of the `jsonNode` must be `JObject`, but its actual " &
  339. "type is `" & $jsonNode.kind & "`."
  340. clear(t)
  341. for k, v in jsonNode:
  342. t[k] = jsonTo(v, V)
  343. proc toJsonHook*[K: string|cstring, V](t: (Table[K, V] | OrderedTable[K, V])): JsonNode =
  344. ## Enables `toJson` for `Table` and `OrderedTable` types.
  345. ##
  346. ## See also:
  347. ## * `fromJsonHook proc<#fromJsonHook,,JsonNode>`_
  348. # pending PR #9217 use: toSeq(a) instead of `collect` in `runnableExamples`.
  349. runnableExamples:
  350. import std/[tables, json, sugar]
  351. let foo = (
  352. t: [("two", 2)].toTable,
  353. ot: [("one", 1), ("three", 3)].toOrderedTable)
  354. assert $toJson(foo) == """{"t":{"two":2},"ot":{"one":1,"three":3}}"""
  355. # if keys are not string|cstring, you can use this:
  356. let a = {10: "foo", 11: "bar"}.newOrderedTable
  357. let a2 = collect: (for k,v in a: (k,v))
  358. assert $toJson(a2) == """[[10,"foo"],[11,"bar"]]"""
  359. result = newJObject()
  360. for k, v in pairs(t):
  361. # not sure if $k has overhead for string
  362. result[(when K is string: k else: $k)] = toJson(v)
  363. proc fromJsonHook*[A](s: var SomeSet[A], jsonNode: JsonNode) =
  364. ## Enables `fromJson` for `HashSet` and `OrderedSet` types.
  365. ##
  366. ## See also:
  367. ## * `toJsonHook proc<#toJsonHook,SomeSet[A]>`_
  368. runnableExamples:
  369. import std/[sets, json]
  370. var foo: tuple[hs: HashSet[string], os: OrderedSet[string]]
  371. fromJson(foo, parseJson("""
  372. {"hs": ["hash", "set"], "os": ["ordered", "set"]}"""))
  373. assert foo.hs == ["hash", "set"].toHashSet
  374. assert foo.os == ["ordered", "set"].toOrderedSet
  375. assert jsonNode.kind == JArray,
  376. "The kind of the `jsonNode` must be `JArray`, but its actual " &
  377. "type is `" & $jsonNode.kind & "`."
  378. clear(s)
  379. for v in jsonNode:
  380. incl(s, jsonTo(v, A))
  381. proc toJsonHook*[A](s: SomeSet[A]): JsonNode =
  382. ## Enables `toJson` for `HashSet` and `OrderedSet` types.
  383. ##
  384. ## See also:
  385. ## * `fromJsonHook proc<#fromJsonHook,SomeSet[A],JsonNode>`_
  386. runnableExamples:
  387. import std/[sets, json]
  388. let foo = (hs: ["hash"].toHashSet, os: ["ordered", "set"].toOrderedSet)
  389. assert $toJson(foo) == """{"hs":["hash"],"os":["ordered","set"]}"""
  390. result = newJArray()
  391. for k in s:
  392. add(result, toJson(k))
  393. proc fromJsonHook*[T](self: var Option[T], jsonNode: JsonNode) =
  394. ## Enables `fromJson` for `Option` types.
  395. ##
  396. ## See also:
  397. ## * `toJsonHook proc<#toJsonHook,Option[T]>`_
  398. runnableExamples:
  399. import std/[options, json]
  400. var opt: Option[string]
  401. fromJsonHook(opt, parseJson("\"test\""))
  402. assert get(opt) == "test"
  403. fromJson(opt, parseJson("null"))
  404. assert isNone(opt)
  405. if jsonNode.kind != JNull:
  406. self = some(jsonTo(jsonNode, T))
  407. else:
  408. self = none[T]()
  409. proc toJsonHook*[T](self: Option[T]): JsonNode =
  410. ## Enables `toJson` for `Option` types.
  411. ##
  412. ## See also:
  413. ## * `fromJsonHook proc<#fromJsonHook,Option[T],JsonNode>`_
  414. runnableExamples:
  415. import std/[options, json]
  416. let optSome = some("test")
  417. assert $toJson(optSome) == "\"test\""
  418. let optNone = none[string]()
  419. assert $toJson(optNone) == "null"
  420. if isSome(self):
  421. toJson(get(self))
  422. else:
  423. newJNull()
  424. proc fromJsonHook*(a: var StringTableRef, b: JsonNode) =
  425. ## Enables `fromJson` for `StringTableRef` type.
  426. ##
  427. ## See also:
  428. ## * `toJsonHook proc<#toJsonHook,StringTableRef>`_
  429. runnableExamples:
  430. import std/[strtabs, json]
  431. var t = newStringTable(modeCaseSensitive)
  432. let jsonStr = """{"mode": 0, "table": {"name": "John", "surname": "Doe"}}"""
  433. fromJsonHook(t, parseJson(jsonStr))
  434. assert t[] == newStringTable("name", "John", "surname", "Doe",
  435. modeCaseSensitive)[]
  436. var mode = jsonTo(b["mode"], StringTableMode)
  437. a = newStringTable(mode)
  438. let b2 = b["table"]
  439. for k,v in b2: a[k] = jsonTo(v, string)
  440. proc toJsonHook*(a: StringTableRef): JsonNode =
  441. ## Enables `toJson` for `StringTableRef` type.
  442. ##
  443. ## See also:
  444. ## * `fromJsonHook proc<#fromJsonHook,StringTableRef,JsonNode>`_
  445. runnableExamples:
  446. import std/[strtabs, json]
  447. let t = newStringTable("name", "John", "surname", "Doe", modeCaseSensitive)
  448. let jsonStr = """{"mode": "modeCaseSensitive",
  449. "table": {"name": "John", "surname": "Doe"}}"""
  450. assert toJson(t) == parseJson(jsonStr)
  451. result = newJObject()
  452. result["mode"] = toJson($a.mode)
  453. let t = newJObject()
  454. for k,v in a: t[k] = toJson(v)
  455. result["table"] = t