json.nim 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf, Dominik Picheta
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a simple high performance `JSON`:idx:
  10. ## parser. JSON (JavaScript Object Notation) is a lightweight
  11. ## data-interchange format that is easy for humans to read and write
  12. ## (unlike XML). It is easy for machines to parse and generate.
  13. ## JSON is based on a subset of the JavaScript Programming Language,
  14. ## Standard ECMA-262 3rd Edition - December 1999.
  15. ##
  16. ## See also
  17. ## ========
  18. ## * `std/parsejson <parsejson.html>`_
  19. ## * `std/jsonutils <jsonutils.html>`_
  20. ## * `std/marshal <marshal.html>`_
  21. ## * `std/jscore <jscore.html>`_
  22. ##
  23. ##
  24. ## Overview
  25. ## ========
  26. ##
  27. ## Parsing JSON
  28. ## ------------
  29. ##
  30. ## JSON often arrives into your program (via an API or a file) as a `string`.
  31. ## The first step is to change it from its serialized form into a nested object
  32. ## structure called a `JsonNode`.
  33. ##
  34. ## The `parseJson` procedure takes a string containing JSON and returns a
  35. ## `JsonNode` object. This is an object variant and it is either a
  36. ## `JObject`, `JArray`, `JString`, `JInt`, `JFloat`, `JBool` or
  37. ## `JNull`. You check the kind of this object variant by using the `kind`
  38. ## accessor.
  39. ##
  40. ## For a `JsonNode` who's kind is `JObject`, you can access its fields using
  41. ## the `[]` operator. The following example shows how to do this:
  42. ##
  43. ## .. code-block:: Nim
  44. ## import std/json
  45. ##
  46. ## let jsonNode = parseJson("""{"key": 3.14}""")
  47. ##
  48. ## doAssert jsonNode.kind == JObject
  49. ## doAssert jsonNode["key"].kind == JFloat
  50. ##
  51. ## Reading values
  52. ## --------------
  53. ##
  54. ## Once you have a `JsonNode`, retrieving the values can then be achieved
  55. ## by using one of the helper procedures, which include:
  56. ##
  57. ## * `getInt`
  58. ## * `getFloat`
  59. ## * `getStr`
  60. ## * `getBool`
  61. ##
  62. ## To retrieve the value of `"key"` you can do the following:
  63. ##
  64. ## .. code-block:: Nim
  65. ## import std/json
  66. ##
  67. ## let jsonNode = parseJson("""{"key": 3.14}""")
  68. ##
  69. ## doAssert jsonNode["key"].getFloat() == 3.14
  70. ##
  71. ## **Important:** The `[]` operator will raise an exception when the
  72. ## specified field does not exist.
  73. ##
  74. ## Handling optional keys
  75. ## ----------------------
  76. ##
  77. ## By using the `{}` operator instead of `[]`, it will return `nil`
  78. ## when the field is not found. The `get`-family of procedures will return a
  79. ## type's default value when called on `nil`.
  80. ##
  81. ## .. code-block:: Nim
  82. ## import std/json
  83. ##
  84. ## let jsonNode = parseJson("{}")
  85. ##
  86. ## doAssert jsonNode{"nope"}.getInt() == 0
  87. ## doAssert jsonNode{"nope"}.getFloat() == 0
  88. ## doAssert jsonNode{"nope"}.getStr() == ""
  89. ## doAssert jsonNode{"nope"}.getBool() == false
  90. ##
  91. ## Using default values
  92. ## --------------------
  93. ##
  94. ## The `get`-family helpers also accept an additional parameter which allow
  95. ## you to fallback to a default value should the key's values be `null`:
  96. ##
  97. ## .. code-block:: Nim
  98. ## import std/json
  99. ##
  100. ## let jsonNode = parseJson("""{"key": 3.14, "key2": null}""")
  101. ##
  102. ## doAssert jsonNode["key"].getFloat(6.28) == 3.14
  103. ## doAssert jsonNode["key2"].getFloat(3.14) == 3.14
  104. ## doAssert jsonNode{"nope"}.getFloat(3.14) == 3.14 # note the {}
  105. ##
  106. ## Unmarshalling
  107. ## -------------
  108. ##
  109. ## In addition to reading dynamic data, Nim can also unmarshal JSON directly
  110. ## into a type with the `to` macro.
  111. ##
  112. ## Note: Use `Option <options.html#Option>`_ for keys sometimes missing in json
  113. ## responses, and backticks around keys with a reserved keyword as name.
  114. ##
  115. ## .. code-block:: Nim
  116. ## import std/json
  117. ## import std/options
  118. ##
  119. ## type
  120. ## User = object
  121. ## name: string
  122. ## age: int
  123. ## `type`: Option[string]
  124. ##
  125. ## let userJson = parseJson("""{ "name": "Nim", "age": 12 }""")
  126. ## let user = to(userJson, User)
  127. ## if user.`type`.isSome():
  128. ## assert user.`type`.get() != "robot"
  129. ##
  130. ## Creating JSON
  131. ## =============
  132. ##
  133. ## This module can also be used to comfortably create JSON using the `%*`
  134. ## operator:
  135. ##
  136. ## .. code-block:: nim
  137. ## import std/json
  138. ##
  139. ## var hisName = "John"
  140. ## let herAge = 31
  141. ## var j = %*
  142. ## [
  143. ## { "name": hisName, "age": 30 },
  144. ## { "name": "Susan", "age": herAge }
  145. ## ]
  146. ##
  147. ## var j2 = %* {"name": "Isaac", "books": ["Robot Dreams"]}
  148. ## j2["details"] = %* {"age":35, "pi":3.1415}
  149. ## echo j2
  150. ##
  151. ## See also: std/jsonutils for hookable json serialization/deserialization
  152. ## of arbitrary types.
  153. runnableExamples:
  154. ## Note: for JObject, key ordering is preserved, unlike in some languages,
  155. ## this is convenient for some use cases. Example:
  156. type Foo = object
  157. a1, a2, a0, a3, a4: int
  158. doAssert $(%* Foo()) == """{"a1":0,"a2":0,"a0":0,"a3":0,"a4":0}"""
  159. import
  160. std/[hashes, tables, strutils, lexbase, streams, macros, parsejson]
  161. import std/options # xxx remove this dependency using same approach as https://github.com/nim-lang/Nim/pull/14563
  162. import std/private/since
  163. export
  164. tables.`$`
  165. export
  166. parsejson.JsonEventKind, parsejson.JsonError, JsonParser, JsonKindError,
  167. open, close, str, getInt, getFloat, kind, getColumn, getLine, getFilename,
  168. errorMsg, errorMsgExpected, next, JsonParsingError, raiseParseErr, nimIdentNormalize
  169. type
  170. JsonNodeKind* = enum ## possible JSON node types
  171. JNull,
  172. JBool,
  173. JInt,
  174. JFloat,
  175. JString,
  176. JObject,
  177. JArray
  178. JsonNode* = ref JsonNodeObj ## JSON node
  179. JsonNodeObj* {.acyclic.} = object
  180. isUnquoted: bool # the JString was a number-like token and
  181. # so shouldn't be quoted
  182. case kind*: JsonNodeKind
  183. of JString:
  184. str*: string
  185. of JInt:
  186. num*: BiggestInt
  187. of JFloat:
  188. fnum*: float
  189. of JBool:
  190. bval*: bool
  191. of JNull:
  192. nil
  193. of JObject:
  194. fields*: OrderedTable[string, JsonNode]
  195. of JArray:
  196. elems*: seq[JsonNode]
  197. proc newJString*(s: string): JsonNode =
  198. ## Creates a new `JString JsonNode`.
  199. result = JsonNode(kind: JString, str: s)
  200. proc newJRawNumber(s: string): JsonNode =
  201. ## Creates a "raw JS number", that is a number that does not
  202. ## fit into Nim's `BiggestInt` field. This is really a `JString`
  203. ## with the additional information that it should be converted back
  204. ## to the string representation without the quotes.
  205. result = JsonNode(kind: JString, str: s, isUnquoted: true)
  206. proc newJStringMove(s: string): JsonNode =
  207. result = JsonNode(kind: JString)
  208. shallowCopy(result.str, s)
  209. proc newJInt*(n: BiggestInt): JsonNode =
  210. ## Creates a new `JInt JsonNode`.
  211. result = JsonNode(kind: JInt, num: n)
  212. proc newJFloat*(n: float): JsonNode =
  213. ## Creates a new `JFloat JsonNode`.
  214. result = JsonNode(kind: JFloat, fnum: n)
  215. proc newJBool*(b: bool): JsonNode =
  216. ## Creates a new `JBool JsonNode`.
  217. result = JsonNode(kind: JBool, bval: b)
  218. proc newJNull*(): JsonNode =
  219. ## Creates a new `JNull JsonNode`.
  220. result = JsonNode(kind: JNull)
  221. proc newJObject*(): JsonNode =
  222. ## Creates a new `JObject JsonNode`
  223. result = JsonNode(kind: JObject, fields: initOrderedTable[string, JsonNode](2))
  224. proc newJArray*(): JsonNode =
  225. ## Creates a new `JArray JsonNode`
  226. result = JsonNode(kind: JArray, elems: @[])
  227. proc getStr*(n: JsonNode, default: string = ""): string =
  228. ## Retrieves the string value of a `JString JsonNode`.
  229. ##
  230. ## Returns `default` if `n` is not a `JString`, or if `n` is nil.
  231. if n.isNil or n.kind != JString: return default
  232. else: return n.str
  233. proc getInt*(n: JsonNode, default: int = 0): int =
  234. ## Retrieves the int value of a `JInt JsonNode`.
  235. ##
  236. ## Returns `default` if `n` is not a `JInt`, or if `n` is nil.
  237. if n.isNil or n.kind != JInt: return default
  238. else: return int(n.num)
  239. proc getBiggestInt*(n: JsonNode, default: BiggestInt = 0): BiggestInt =
  240. ## Retrieves the BiggestInt value of a `JInt JsonNode`.
  241. ##
  242. ## Returns `default` if `n` is not a `JInt`, or if `n` is nil.
  243. if n.isNil or n.kind != JInt: return default
  244. else: return n.num
  245. proc getFloat*(n: JsonNode, default: float = 0.0): float =
  246. ## Retrieves the float value of a `JFloat JsonNode`.
  247. ##
  248. ## Returns `default` if `n` is not a `JFloat` or `JInt`, or if `n` is nil.
  249. if n.isNil: return default
  250. case n.kind
  251. of JFloat: return n.fnum
  252. of JInt: return float(n.num)
  253. else: return default
  254. proc getBool*(n: JsonNode, default: bool = false): bool =
  255. ## Retrieves the bool value of a `JBool JsonNode`.
  256. ##
  257. ## Returns `default` if `n` is not a `JBool`, or if `n` is nil.
  258. if n.isNil or n.kind != JBool: return default
  259. else: return n.bval
  260. proc getFields*(n: JsonNode,
  261. default = initOrderedTable[string, JsonNode](2)):
  262. OrderedTable[string, JsonNode] =
  263. ## Retrieves the key, value pairs of a `JObject JsonNode`.
  264. ##
  265. ## Returns `default` if `n` is not a `JObject`, or if `n` is nil.
  266. if n.isNil or n.kind != JObject: return default
  267. else: return n.fields
  268. proc getElems*(n: JsonNode, default: seq[JsonNode] = @[]): seq[JsonNode] =
  269. ## Retrieves the array of a `JArray JsonNode`.
  270. ##
  271. ## Returns `default` if `n` is not a `JArray`, or if `n` is nil.
  272. if n.isNil or n.kind != JArray: return default
  273. else: return n.elems
  274. proc add*(father, child: JsonNode) =
  275. ## Adds `child` to a JArray node `father`.
  276. assert father.kind == JArray
  277. father.elems.add(child)
  278. proc add*(obj: JsonNode, key: string, val: JsonNode) =
  279. ## Sets a field from a `JObject`.
  280. assert obj.kind == JObject
  281. obj.fields[key] = val
  282. proc `%`*(s: string): JsonNode =
  283. ## Generic constructor for JSON data. Creates a new `JString JsonNode`.
  284. result = JsonNode(kind: JString, str: s)
  285. proc `%`*(n: uint): JsonNode =
  286. ## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
  287. if n > cast[uint](int.high):
  288. result = newJRawNumber($n)
  289. else:
  290. result = JsonNode(kind: JInt, num: BiggestInt(n))
  291. proc `%`*(n: int): JsonNode =
  292. ## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
  293. result = JsonNode(kind: JInt, num: n)
  294. proc `%`*(n: BiggestUInt): JsonNode =
  295. ## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
  296. if n > cast[BiggestUInt](BiggestInt.high):
  297. result = newJRawNumber($n)
  298. else:
  299. result = JsonNode(kind: JInt, num: BiggestInt(n))
  300. proc `%`*(n: BiggestInt): JsonNode =
  301. ## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
  302. result = JsonNode(kind: JInt, num: n)
  303. proc `%`*(n: float): JsonNode =
  304. ## Generic constructor for JSON data. Creates a new `JFloat JsonNode`.
  305. result = JsonNode(kind: JFloat, fnum: n)
  306. proc `%`*(b: bool): JsonNode =
  307. ## Generic constructor for JSON data. Creates a new `JBool JsonNode`.
  308. result = JsonNode(kind: JBool, bval: b)
  309. proc `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode =
  310. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  311. if keyVals.len == 0: return newJArray()
  312. result = newJObject()
  313. for key, val in items(keyVals): result.fields[key] = val
  314. template `%`*(j: JsonNode): JsonNode = j
  315. proc `%`*[T](elements: openArray[T]): JsonNode =
  316. ## Generic constructor for JSON data. Creates a new `JArray JsonNode`
  317. result = newJArray()
  318. for elem in elements: result.add(%elem)
  319. proc `%`*[T](table: Table[string, T]|OrderedTable[string, T]): JsonNode =
  320. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`.
  321. result = newJObject()
  322. for k, v in table: result[k] = %v
  323. proc `%`*[T](opt: Option[T]): JsonNode =
  324. ## Generic constructor for JSON data. Creates a new `JNull JsonNode`
  325. ## if `opt` is empty, otherwise it delegates to the underlying value.
  326. if opt.isSome: %opt.get else: newJNull()
  327. when false:
  328. # For 'consistency' we could do this, but that only pushes people further
  329. # into that evil comfort zone where they can use Nim without understanding it
  330. # causing problems later on.
  331. proc `%`*(elements: set[bool]): JsonNode =
  332. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`.
  333. ## This can only be used with the empty set `{}` and is supported
  334. ## to prevent the gotcha `%*{}` which used to produce an empty
  335. ## JSON array.
  336. result = newJObject()
  337. assert false notin elements, "usage error: only empty sets allowed"
  338. assert true notin elements, "usage error: only empty sets allowed"
  339. proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) {.inline.} =
  340. ## Sets a field from a `JObject`.
  341. assert(obj.kind == JObject)
  342. obj.fields[key] = val
  343. proc `%`*[T: object](o: T): JsonNode =
  344. ## Construct JsonNode from tuples and objects.
  345. result = newJObject()
  346. for k, v in o.fieldPairs: result[k] = %v
  347. proc `%`*(o: ref object): JsonNode =
  348. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  349. if o.isNil:
  350. result = newJNull()
  351. else:
  352. result = %(o[])
  353. proc `%`*(o: enum): JsonNode =
  354. ## Construct a JsonNode that represents the specified enum value as a
  355. ## string. Creates a new `JString JsonNode`.
  356. result = %($o)
  357. proc toJsonImpl(x: NimNode): NimNode =
  358. case x.kind
  359. of nnkBracket: # array
  360. if x.len == 0: return newCall(bindSym"newJArray")
  361. result = newNimNode(nnkBracket)
  362. for i in 0 ..< x.len:
  363. result.add(toJsonImpl(x[i]))
  364. result = newCall(bindSym("%", brOpen), result)
  365. of nnkTableConstr: # object
  366. if x.len == 0: return newCall(bindSym"newJObject")
  367. result = newNimNode(nnkTableConstr)
  368. for i in 0 ..< x.len:
  369. x[i].expectKind nnkExprColonExpr
  370. result.add newTree(nnkExprColonExpr, x[i][0], toJsonImpl(x[i][1]))
  371. result = newCall(bindSym("%", brOpen), result)
  372. of nnkCurly: # empty object
  373. x.expectLen(0)
  374. result = newCall(bindSym"newJObject")
  375. of nnkNilLit:
  376. result = newCall(bindSym"newJNull")
  377. of nnkPar:
  378. if x.len == 1: result = toJsonImpl(x[0])
  379. else: result = newCall(bindSym("%", brOpen), x)
  380. else:
  381. result = newCall(bindSym("%", brOpen), x)
  382. macro `%*`*(x: untyped): untyped =
  383. ## Convert an expression to a JsonNode directly, without having to specify
  384. ## `%` for every element.
  385. result = toJsonImpl(x)
  386. proc `==`*(a, b: JsonNode): bool =
  387. ## Check two nodes for equality
  388. if a.isNil:
  389. if b.isNil: return true
  390. return false
  391. elif b.isNil or a.kind != b.kind:
  392. return false
  393. else:
  394. case a.kind
  395. of JString:
  396. result = a.str == b.str
  397. of JInt:
  398. result = a.num == b.num
  399. of JFloat:
  400. result = a.fnum == b.fnum
  401. of JBool:
  402. result = a.bval == b.bval
  403. of JNull:
  404. result = true
  405. of JArray:
  406. result = a.elems == b.elems
  407. of JObject:
  408. # we cannot use OrderedTable's equality here as
  409. # the order does not matter for equality here.
  410. if a.fields.len != b.fields.len: return false
  411. for key, val in a.fields:
  412. if not b.fields.hasKey(key): return false
  413. if b.fields[key] != val: return false
  414. result = true
  415. proc hash*(n: OrderedTable[string, JsonNode]): Hash {.noSideEffect.}
  416. proc hash*(n: JsonNode): Hash =
  417. ## Compute the hash for a JSON node
  418. case n.kind
  419. of JArray:
  420. result = hash(n.elems)
  421. of JObject:
  422. result = hash(n.fields)
  423. of JInt:
  424. result = hash(n.num)
  425. of JFloat:
  426. result = hash(n.fnum)
  427. of JBool:
  428. result = hash(n.bval.int)
  429. of JString:
  430. result = hash(n.str)
  431. of JNull:
  432. result = Hash(0)
  433. proc hash*(n: OrderedTable[string, JsonNode]): Hash =
  434. for key, val in n:
  435. result = result xor (hash(key) !& hash(val))
  436. result = !$result
  437. proc len*(n: JsonNode): int =
  438. ## If `n` is a `JArray`, it returns the number of elements.
  439. ## If `n` is a `JObject`, it returns the number of pairs.
  440. ## Else it returns 0.
  441. case n.kind
  442. of JArray: result = n.elems.len
  443. of JObject: result = n.fields.len
  444. else: discard
  445. proc `[]`*(node: JsonNode, name: string): JsonNode {.inline.} =
  446. ## Gets a field from a `JObject`, which must not be nil.
  447. ## If the value at `name` does not exist, raises KeyError.
  448. assert(not isNil(node))
  449. assert(node.kind == JObject)
  450. when defined(nimJsonGet):
  451. if not node.fields.hasKey(name): return nil
  452. result = node.fields[name]
  453. proc `[]`*(node: JsonNode, index: int): JsonNode {.inline.} =
  454. ## Gets the node at `index` in an Array. Result is undefined if `index`
  455. ## is out of bounds, but as long as array bound checks are enabled it will
  456. ## result in an exception.
  457. assert(not isNil(node))
  458. assert(node.kind == JArray)
  459. return node.elems[index]
  460. proc `[]`*(node: JsonNode, index: BackwardsIndex): JsonNode {.inline, since: (1, 5, 1).} =
  461. ## Gets the node at `array.len-i` in an array through the `^` operator.
  462. ##
  463. ## i.e. `j[^i]` is a shortcut for `j[j.len-i]`.
  464. runnableExamples:
  465. let
  466. j = parseJson("[1,2,3,4,5]")
  467. doAssert j[^1].getInt == 5
  468. doAssert j[^2].getInt == 4
  469. `[]`(node, node.len - int(index))
  470. proc hasKey*(node: JsonNode, key: string): bool =
  471. ## Checks if `key` exists in `node`.
  472. assert(node.kind == JObject)
  473. result = node.fields.hasKey(key)
  474. proc contains*(node: JsonNode, key: string): bool =
  475. ## Checks if `key` exists in `node`.
  476. assert(node.kind == JObject)
  477. node.fields.hasKey(key)
  478. proc contains*(node: JsonNode, val: JsonNode): bool =
  479. ## Checks if `val` exists in array `node`.
  480. assert(node.kind == JArray)
  481. find(node.elems, val) >= 0
  482. proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =
  483. ## Traverses the node and gets the given value. If any of the
  484. ## keys do not exist, returns `nil`. Also returns `nil` if one of the
  485. ## intermediate data structures is not an object.
  486. ##
  487. ## This proc can be used to create tree structures on the
  488. ## fly (sometimes called `autovivification`:idx:):
  489. ##
  490. runnableExamples:
  491. var myjson = %* {"parent": {"child": {"grandchild": 1}}}
  492. doAssert myjson{"parent", "child", "grandchild"} == newJInt(1)
  493. result = node
  494. for key in keys:
  495. if isNil(result) or result.kind != JObject:
  496. return nil
  497. result = result.fields.getOrDefault(key)
  498. proc `{}`*(node: JsonNode, index: varargs[int]): JsonNode =
  499. ## Traverses the node and gets the given value. If any of the
  500. ## indexes do not exist, returns `nil`. Also returns `nil` if one of the
  501. ## intermediate data structures is not an array.
  502. result = node
  503. for i in index:
  504. if isNil(result) or result.kind != JArray or i >= node.len:
  505. return nil
  506. result = result.elems[i]
  507. proc getOrDefault*(node: JsonNode, key: string): JsonNode =
  508. ## Gets a field from a `node`. If `node` is nil or not an object or
  509. ## value at `key` does not exist, returns nil
  510. if not isNil(node) and node.kind == JObject:
  511. result = node.fields.getOrDefault(key)
  512. proc `{}`*(node: JsonNode, key: string): JsonNode =
  513. ## Gets a field from a `node`. If `node` is nil or not an object or
  514. ## value at `key` does not exist, returns nil
  515. node.getOrDefault(key)
  516. proc `{}=`*(node: JsonNode, keys: varargs[string], value: JsonNode) =
  517. ## Traverses the node and tries to set the value at the given location
  518. ## to `value`. If any of the keys are missing, they are added.
  519. var node = node
  520. for i in 0..(keys.len-2):
  521. if not node.hasKey(keys[i]):
  522. node[keys[i]] = newJObject()
  523. node = node[keys[i]]
  524. node[keys[keys.len-1]] = value
  525. proc delete*(obj: JsonNode, key: string) =
  526. ## Deletes `obj[key]`.
  527. assert(obj.kind == JObject)
  528. if not obj.fields.hasKey(key):
  529. raise newException(KeyError, "key not in object")
  530. obj.fields.del(key)
  531. proc copy*(p: JsonNode): JsonNode =
  532. ## Performs a deep copy of `a`.
  533. case p.kind
  534. of JString:
  535. result = newJString(p.str)
  536. result.isUnquoted = p.isUnquoted
  537. of JInt:
  538. result = newJInt(p.num)
  539. of JFloat:
  540. result = newJFloat(p.fnum)
  541. of JBool:
  542. result = newJBool(p.bval)
  543. of JNull:
  544. result = newJNull()
  545. of JObject:
  546. result = newJObject()
  547. for key, val in pairs(p.fields):
  548. result.fields[key] = copy(val)
  549. of JArray:
  550. result = newJArray()
  551. for i in items(p.elems):
  552. result.elems.add(copy(i))
  553. # ------------- pretty printing ----------------------------------------------
  554. proc indent(s: var string, i: int) =
  555. s.add(spaces(i))
  556. proc newIndent(curr, indent: int, ml: bool): int =
  557. if ml: return curr + indent
  558. else: return indent
  559. proc nl(s: var string, ml: bool) =
  560. s.add(if ml: "\n" else: " ")
  561. proc escapeJsonUnquoted*(s: string; result: var string) =
  562. ## Converts a string `s` to its JSON representation without quotes.
  563. ## Appends to `result`.
  564. for c in s:
  565. case c
  566. of '\L': result.add("\\n")
  567. of '\b': result.add("\\b")
  568. of '\f': result.add("\\f")
  569. of '\t': result.add("\\t")
  570. of '\v': result.add("\\u000b")
  571. of '\r': result.add("\\r")
  572. of '"': result.add("\\\"")
  573. of '\0'..'\7': result.add("\\u000" & $ord(c))
  574. of '\14'..'\31': result.add("\\u00" & toHex(ord(c), 2))
  575. of '\\': result.add("\\\\")
  576. else: result.add(c)
  577. proc escapeJsonUnquoted*(s: string): string =
  578. ## Converts a string `s` to its JSON representation without quotes.
  579. result = newStringOfCap(s.len + s.len shr 3)
  580. escapeJsonUnquoted(s, result)
  581. proc escapeJson*(s: string; result: var string) =
  582. ## Converts a string `s` to its JSON representation with quotes.
  583. ## Appends to `result`.
  584. result.add("\"")
  585. escapeJsonUnquoted(s, result)
  586. result.add("\"")
  587. proc escapeJson*(s: string): string =
  588. ## Converts a string `s` to its JSON representation with quotes.
  589. result = newStringOfCap(s.len + s.len shr 3)
  590. escapeJson(s, result)
  591. proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true,
  592. lstArr = false, currIndent = 0) =
  593. case node.kind
  594. of JObject:
  595. if lstArr: result.indent(currIndent) # Indentation
  596. if node.fields.len > 0:
  597. result.add("{")
  598. result.nl(ml) # New line
  599. var i = 0
  600. for key, val in pairs(node.fields):
  601. if i > 0:
  602. result.add(",")
  603. result.nl(ml) # New Line
  604. inc i
  605. # Need to indent more than {
  606. result.indent(newIndent(currIndent, indent, ml))
  607. escapeJson(key, result)
  608. result.add(": ")
  609. toPretty(result, val, indent, ml, false,
  610. newIndent(currIndent, indent, ml))
  611. result.nl(ml)
  612. result.indent(currIndent) # indent the same as {
  613. result.add("}")
  614. else:
  615. result.add("{}")
  616. of JString:
  617. if lstArr: result.indent(currIndent)
  618. if node.isUnquoted:
  619. result.add node.str
  620. else:
  621. escapeJson(node.str, result)
  622. of JInt:
  623. if lstArr: result.indent(currIndent)
  624. result.addInt(node.num)
  625. of JFloat:
  626. if lstArr: result.indent(currIndent)
  627. result.addFloat(node.fnum)
  628. of JBool:
  629. if lstArr: result.indent(currIndent)
  630. result.add(if node.bval: "true" else: "false")
  631. of JArray:
  632. if lstArr: result.indent(currIndent)
  633. if len(node.elems) != 0:
  634. result.add("[")
  635. result.nl(ml)
  636. for i in 0..len(node.elems)-1:
  637. if i > 0:
  638. result.add(",")
  639. result.nl(ml) # New Line
  640. toPretty(result, node.elems[i], indent, ml,
  641. true, newIndent(currIndent, indent, ml))
  642. result.nl(ml)
  643. result.indent(currIndent)
  644. result.add("]")
  645. else: result.add("[]")
  646. of JNull:
  647. if lstArr: result.indent(currIndent)
  648. result.add("null")
  649. proc pretty*(node: JsonNode, indent = 2): string =
  650. ## Returns a JSON Representation of `node`, with indentation and
  651. ## on multiple lines.
  652. ##
  653. ## Similar to prettyprint in Python.
  654. runnableExamples:
  655. let j = %* {"name": "Isaac", "books": ["Robot Dreams"],
  656. "details": {"age": 35, "pi": 3.1415}}
  657. doAssert pretty(j) == """
  658. {
  659. "name": "Isaac",
  660. "books": [
  661. "Robot Dreams"
  662. ],
  663. "details": {
  664. "age": 35,
  665. "pi": 3.1415
  666. }
  667. }"""
  668. result = ""
  669. toPretty(result, node, indent)
  670. proc toUgly*(result: var string, node: JsonNode) =
  671. ## Converts `node` to its JSON Representation, without
  672. ## regard for human readability. Meant to improve `$` string
  673. ## conversion performance.
  674. ##
  675. ## JSON representation is stored in the passed `result`
  676. ##
  677. ## This provides higher efficiency than the `pretty` procedure as it
  678. ## does **not** attempt to format the resulting JSON to make it human readable.
  679. var comma = false
  680. case node.kind:
  681. of JArray:
  682. result.add "["
  683. for child in node.elems:
  684. if comma: result.add ","
  685. else: comma = true
  686. result.toUgly child
  687. result.add "]"
  688. of JObject:
  689. result.add "{"
  690. for key, value in pairs(node.fields):
  691. if comma: result.add ","
  692. else: comma = true
  693. key.escapeJson(result)
  694. result.add ":"
  695. result.toUgly value
  696. result.add "}"
  697. of JString:
  698. if node.isUnquoted:
  699. result.add node.str
  700. else:
  701. node.str.escapeJson(result)
  702. of JInt:
  703. result.addInt(node.num)
  704. of JFloat:
  705. result.addFloat(node.fnum)
  706. of JBool:
  707. result.add(if node.bval: "true" else: "false")
  708. of JNull:
  709. result.add "null"
  710. proc `$`*(node: JsonNode): string =
  711. ## Converts `node` to its JSON Representation on one line.
  712. result = newStringOfCap(node.len shl 1)
  713. toUgly(result, node)
  714. iterator items*(node: JsonNode): JsonNode =
  715. ## Iterator for the items of `node`. `node` has to be a JArray.
  716. assert node.kind == JArray, ": items() can not iterate a JsonNode of kind " & $node.kind
  717. for i in items(node.elems):
  718. yield i
  719. iterator mitems*(node: var JsonNode): var JsonNode =
  720. ## Iterator for the items of `node`. `node` has to be a JArray. Items can be
  721. ## modified.
  722. assert node.kind == JArray, ": mitems() can not iterate a JsonNode of kind " & $node.kind
  723. for i in mitems(node.elems):
  724. yield i
  725. iterator pairs*(node: JsonNode): tuple[key: string, val: JsonNode] =
  726. ## Iterator for the child elements of `node`. `node` has to be a JObject.
  727. assert node.kind == JObject, ": pairs() can not iterate a JsonNode of kind " & $node.kind
  728. for key, val in pairs(node.fields):
  729. yield (key, val)
  730. iterator keys*(node: JsonNode): string =
  731. ## Iterator for the keys in `node`. `node` has to be a JObject.
  732. assert node.kind == JObject, ": keys() can not iterate a JsonNode of kind " & $node.kind
  733. for key in node.fields.keys:
  734. yield key
  735. iterator mpairs*(node: var JsonNode): tuple[key: string, val: var JsonNode] =
  736. ## Iterator for the child elements of `node`. `node` has to be a JObject.
  737. ## Values can be modified
  738. assert node.kind == JObject, ": mpairs() can not iterate a JsonNode of kind " & $node.kind
  739. for key, val in mpairs(node.fields):
  740. yield (key, val)
  741. proc parseJson(p: var JsonParser; rawIntegers, rawFloats: bool): JsonNode =
  742. ## Parses JSON from a JSON Parser `p`.
  743. case p.tok
  744. of tkString:
  745. # we capture 'p.a' here, so we need to give it a fresh buffer afterwards:
  746. result = newJStringMove(p.a)
  747. p.a = ""
  748. discard getTok(p)
  749. of tkInt:
  750. if rawIntegers:
  751. result = newJRawNumber(p.a)
  752. else:
  753. try:
  754. result = newJInt(parseBiggestInt(p.a))
  755. except ValueError:
  756. result = newJRawNumber(p.a)
  757. discard getTok(p)
  758. of tkFloat:
  759. if rawFloats:
  760. result = newJRawNumber(p.a)
  761. else:
  762. try:
  763. result = newJFloat(parseFloat(p.a))
  764. except ValueError:
  765. result = newJRawNumber(p.a)
  766. discard getTok(p)
  767. of tkTrue:
  768. result = newJBool(true)
  769. discard getTok(p)
  770. of tkFalse:
  771. result = newJBool(false)
  772. discard getTok(p)
  773. of tkNull:
  774. result = newJNull()
  775. discard getTok(p)
  776. of tkCurlyLe:
  777. result = newJObject()
  778. discard getTok(p)
  779. while p.tok != tkCurlyRi:
  780. if p.tok != tkString:
  781. raiseParseErr(p, "string literal as key")
  782. var key = p.a
  783. discard getTok(p)
  784. eat(p, tkColon)
  785. var val = parseJson(p, rawIntegers, rawFloats)
  786. result[key] = val
  787. if p.tok != tkComma: break
  788. discard getTok(p)
  789. eat(p, tkCurlyRi)
  790. of tkBracketLe:
  791. result = newJArray()
  792. discard getTok(p)
  793. while p.tok != tkBracketRi:
  794. result.add(parseJson(p, rawIntegers, rawFloats))
  795. if p.tok != tkComma: break
  796. discard getTok(p)
  797. eat(p, tkBracketRi)
  798. of tkError, tkCurlyRi, tkBracketRi, tkColon, tkComma, tkEof:
  799. raiseParseErr(p, "{")
  800. iterator parseJsonFragments*(s: Stream, filename: string = ""; rawIntegers = false, rawFloats = false): JsonNode =
  801. ## Parses from a stream `s` into `JsonNodes`. `filename` is only needed
  802. ## for nice error messages.
  803. ## The JSON fragments are separated by whitespace. This can be substantially
  804. ## faster than the comparable loop
  805. ## `for x in splitWhitespace(s): yield parseJson(x)`.
  806. ## This closes the stream `s` after it's done.
  807. ## If `rawIntegers` is true, integer literals will not be converted to a `JInt`
  808. ## field but kept as raw numbers via `JString`.
  809. ## If `rawFloats` is true, floating point literals will not be converted to a `JFloat`
  810. ## field but kept as raw numbers via `JString`.
  811. var p: JsonParser
  812. p.open(s, filename)
  813. try:
  814. discard getTok(p) # read first token
  815. while p.tok != tkEof:
  816. yield p.parseJson(rawIntegers, rawFloats)
  817. finally:
  818. p.close()
  819. proc parseJson*(s: Stream, filename: string = ""; rawIntegers = false, rawFloats = false): JsonNode =
  820. ## Parses from a stream `s` into a `JsonNode`. `filename` is only needed
  821. ## for nice error messages.
  822. ## If `s` contains extra data, it will raise `JsonParsingError`.
  823. ## This closes the stream `s` after it's done.
  824. ## If `rawIntegers` is true, integer literals will not be converted to a `JInt`
  825. ## field but kept as raw numbers via `JString`.
  826. ## If `rawFloats` is true, floating point literals will not be converted to a `JFloat`
  827. ## field but kept as raw numbers via `JString`.
  828. var p: JsonParser
  829. p.open(s, filename)
  830. try:
  831. discard getTok(p) # read first token
  832. result = p.parseJson(rawIntegers, rawFloats)
  833. eat(p, tkEof) # check if there is no extra data
  834. finally:
  835. p.close()
  836. when defined(js):
  837. from std/math import `mod`
  838. from std/jsffi import JSObject, `[]`, to
  839. from std/private/jsutils import getProtoName, isInteger, isSafeInteger
  840. proc parseNativeJson(x: cstring): JSObject {.importjs: "JSON.parse(#)".}
  841. proc getVarType(x: JSObject, isRawNumber: var bool): JsonNodeKind =
  842. result = JNull
  843. case $getProtoName(x) # TODO: Implicit returns fail here.
  844. of "[object Array]": return JArray
  845. of "[object Object]": return JObject
  846. of "[object Number]":
  847. if isInteger(x):
  848. if isSafeInteger(x):
  849. return JInt
  850. else:
  851. isRawNumber = true
  852. return JString
  853. else:
  854. return JFloat
  855. of "[object Boolean]": return JBool
  856. of "[object Null]": return JNull
  857. of "[object String]": return JString
  858. else: assert false
  859. proc len(x: JSObject): int =
  860. asm """
  861. `result` = `x`.length;
  862. """
  863. proc convertObject(x: JSObject): JsonNode =
  864. var isRawNumber = false
  865. case getVarType(x, isRawNumber)
  866. of JArray:
  867. result = newJArray()
  868. for i in 0 ..< x.len:
  869. result.add(x[i].convertObject())
  870. of JObject:
  871. result = newJObject()
  872. asm """for (var property in `x`) {
  873. if (`x`.hasOwnProperty(property)) {
  874. """
  875. var nimProperty: cstring
  876. var nimValue: JSObject
  877. asm "`nimProperty` = property; `nimValue` = `x`[property];"
  878. result[$nimProperty] = nimValue.convertObject()
  879. asm "}}"
  880. of JInt:
  881. result = newJInt(x.to(int))
  882. of JFloat:
  883. result = newJFloat(x.to(float))
  884. of JString:
  885. # Dunno what to do with isUnquoted here
  886. if isRawNumber:
  887. var value: cstring
  888. {.emit: "`value` = `x`.toString();".}
  889. result = newJRawNumber($value)
  890. else:
  891. result = newJString($x.to(cstring))
  892. of JBool:
  893. result = newJBool(x.to(bool))
  894. of JNull:
  895. result = newJNull()
  896. proc parseJson*(buffer: string): JsonNode =
  897. when nimvm:
  898. return parseJson(newStringStream(buffer), "input")
  899. else:
  900. return parseNativeJson(buffer).convertObject()
  901. else:
  902. proc parseJson*(buffer: string; rawIntegers = false, rawFloats = false): JsonNode =
  903. ## Parses JSON from `buffer`.
  904. ## If `buffer` contains extra data, it will raise `JsonParsingError`.
  905. ## If `rawIntegers` is true, integer literals will not be converted to a `JInt`
  906. ## field but kept as raw numbers via `JString`.
  907. ## If `rawFloats` is true, floating point literals will not be converted to a `JFloat`
  908. ## field but kept as raw numbers via `JString`.
  909. result = parseJson(newStringStream(buffer), "input", rawIntegers, rawFloats)
  910. proc parseFile*(filename: string): JsonNode =
  911. ## Parses `file` into a `JsonNode`.
  912. ## If `file` contains extra data, it will raise `JsonParsingError`.
  913. var stream = newFileStream(filename, fmRead)
  914. if stream == nil:
  915. raise newException(IOError, "cannot read from file: " & filename)
  916. result = parseJson(stream, filename, rawIntegers=false, rawFloats=false)
  917. # -- Json deserialiser. --
  918. template verifyJsonKind(node: JsonNode, kinds: set[JsonNodeKind],
  919. ast: string) =
  920. if node == nil:
  921. raise newException(KeyError, "key not found: " & ast)
  922. elif node.kind notin kinds:
  923. let msg = "Incorrect JSON kind. Wanted '$1' in '$2' but got '$3'." % [
  924. $kinds,
  925. ast,
  926. $node.kind
  927. ]
  928. raise newException(JsonKindError, msg)
  929. when defined(nimFixedForwardGeneric):
  930. macro isRefSkipDistinct*(arg: typed): untyped =
  931. ## internal only, do not use
  932. var impl = getTypeImpl(arg)
  933. if impl.kind == nnkBracketExpr and impl[0].eqIdent("typeDesc"):
  934. impl = getTypeImpl(impl[1])
  935. while impl.kind == nnkDistinctTy:
  936. impl = getTypeImpl(impl[0])
  937. result = newLit(impl.kind == nnkRefTy)
  938. # The following forward declarations don't work in older versions of Nim
  939. # forward declare all initFromJson
  940. proc initFromJson(dst: var string; jsonNode: JsonNode; jsonPath: var string)
  941. proc initFromJson(dst: var bool; jsonNode: JsonNode; jsonPath: var string)
  942. proc initFromJson(dst: var JsonNode; jsonNode: JsonNode; jsonPath: var string)
  943. proc initFromJson[T: SomeInteger](dst: var T; jsonNode: JsonNode, jsonPath: var string)
  944. proc initFromJson[T: SomeFloat](dst: var T; jsonNode: JsonNode; jsonPath: var string)
  945. proc initFromJson[T: enum](dst: var T; jsonNode: JsonNode; jsonPath: var string)
  946. proc initFromJson[T](dst: var seq[T]; jsonNode: JsonNode; jsonPath: var string)
  947. proc initFromJson[S, T](dst: var array[S, T]; jsonNode: JsonNode; jsonPath: var string)
  948. proc initFromJson[T](dst: var Table[string, T]; jsonNode: JsonNode; jsonPath: var string)
  949. proc initFromJson[T](dst: var OrderedTable[string, T]; jsonNode: JsonNode; jsonPath: var string)
  950. proc initFromJson[T](dst: var ref T; jsonNode: JsonNode; jsonPath: var string)
  951. proc initFromJson[T](dst: var Option[T]; jsonNode: JsonNode; jsonPath: var string)
  952. proc initFromJson[T: distinct](dst: var T; jsonNode: JsonNode; jsonPath: var string)
  953. proc initFromJson[T: object|tuple](dst: var T; jsonNode: JsonNode; jsonPath: var string)
  954. # initFromJson definitions
  955. proc initFromJson(dst: var string; jsonNode: JsonNode; jsonPath: var string) =
  956. verifyJsonKind(jsonNode, {JString, JNull}, jsonPath)
  957. # since strings don't have a nil state anymore, this mapping of
  958. # JNull to the default string is questionable. `none(string)` and
  959. # `some("")` have the same potentional json value `JNull`.
  960. if jsonNode.kind == JNull:
  961. dst = ""
  962. else:
  963. dst = jsonNode.str
  964. proc initFromJson(dst: var bool; jsonNode: JsonNode; jsonPath: var string) =
  965. verifyJsonKind(jsonNode, {JBool}, jsonPath)
  966. dst = jsonNode.bval
  967. proc initFromJson(dst: var JsonNode; jsonNode: JsonNode; jsonPath: var string) =
  968. if jsonNode == nil:
  969. raise newException(KeyError, "key not found: " & jsonPath)
  970. dst = jsonNode.copy
  971. proc initFromJson[T: SomeInteger](dst: var T; jsonNode: JsonNode, jsonPath: var string) =
  972. when T is uint|uint64 or (not defined(js) and int.sizeof == 4):
  973. verifyJsonKind(jsonNode, {JInt, JString}, jsonPath)
  974. case jsonNode.kind
  975. of JString:
  976. let x = parseBiggestUInt(jsonNode.str)
  977. dst = cast[T](x)
  978. else:
  979. dst = T(jsonNode.num)
  980. else:
  981. verifyJsonKind(jsonNode, {JInt}, jsonPath)
  982. dst = cast[T](jsonNode.num)
  983. proc initFromJson[T: SomeFloat](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  984. verifyJsonKind(jsonNode, {JInt, JFloat}, jsonPath)
  985. if jsonNode.kind == JFloat:
  986. dst = T(jsonNode.fnum)
  987. else:
  988. dst = T(jsonNode.num)
  989. proc initFromJson[T: enum](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  990. verifyJsonKind(jsonNode, {JString}, jsonPath)
  991. dst = parseEnum[T](jsonNode.getStr)
  992. proc initFromJson[T](dst: var seq[T]; jsonNode: JsonNode; jsonPath: var string) =
  993. verifyJsonKind(jsonNode, {JArray}, jsonPath)
  994. dst.setLen jsonNode.len
  995. let orignalJsonPathLen = jsonPath.len
  996. for i in 0 ..< jsonNode.len:
  997. jsonPath.add '['
  998. jsonPath.addInt i
  999. jsonPath.add ']'
  1000. initFromJson(dst[i], jsonNode[i], jsonPath)
  1001. jsonPath.setLen orignalJsonPathLen
  1002. proc initFromJson[S,T](dst: var array[S,T]; jsonNode: JsonNode; jsonPath: var string) =
  1003. verifyJsonKind(jsonNode, {JArray}, jsonPath)
  1004. let originalJsonPathLen = jsonPath.len
  1005. for i in 0 ..< jsonNode.len:
  1006. jsonPath.add '['
  1007. jsonPath.addInt i
  1008. jsonPath.add ']'
  1009. initFromJson(dst[i.S], jsonNode[i], jsonPath) # `.S` for enum indexed arrays
  1010. jsonPath.setLen originalJsonPathLen
  1011. proc initFromJson[T](dst: var Table[string,T]; jsonNode: JsonNode; jsonPath: var string) =
  1012. dst = initTable[string, T]()
  1013. verifyJsonKind(jsonNode, {JObject}, jsonPath)
  1014. let originalJsonPathLen = jsonPath.len
  1015. for key in keys(jsonNode.fields):
  1016. jsonPath.add '.'
  1017. jsonPath.add key
  1018. initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath)
  1019. jsonPath.setLen originalJsonPathLen
  1020. proc initFromJson[T](dst: var OrderedTable[string,T]; jsonNode: JsonNode; jsonPath: var string) =
  1021. dst = initOrderedTable[string,T]()
  1022. verifyJsonKind(jsonNode, {JObject}, jsonPath)
  1023. let originalJsonPathLen = jsonPath.len
  1024. for key in keys(jsonNode.fields):
  1025. jsonPath.add '.'
  1026. jsonPath.add key
  1027. initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath)
  1028. jsonPath.setLen originalJsonPathLen
  1029. proc initFromJson[T](dst: var ref T; jsonNode: JsonNode; jsonPath: var string) =
  1030. verifyJsonKind(jsonNode, {JObject, JNull}, jsonPath)
  1031. if jsonNode.kind == JNull:
  1032. dst = nil
  1033. else:
  1034. dst = new(T)
  1035. initFromJson(dst[], jsonNode, jsonPath)
  1036. proc initFromJson[T](dst: var Option[T]; jsonNode: JsonNode; jsonPath: var string) =
  1037. if jsonNode != nil and jsonNode.kind != JNull:
  1038. when T is ref:
  1039. dst = some(new(T))
  1040. else:
  1041. dst = some(default(T))
  1042. initFromJson(dst.get, jsonNode, jsonPath)
  1043. macro assignDistinctImpl[T: distinct](dst: var T;jsonNode: JsonNode; jsonPath: var string) =
  1044. let typInst = getTypeInst(dst)
  1045. let typImpl = getTypeImpl(dst)
  1046. let baseTyp = typImpl[0]
  1047. result = quote do:
  1048. when nimvm:
  1049. # workaround #12282
  1050. var tmp: `baseTyp`
  1051. initFromJson( tmp, `jsonNode`, `jsonPath`)
  1052. `dst` = `typInst`(tmp)
  1053. else:
  1054. initFromJson( `baseTyp`(`dst`), `jsonNode`, `jsonPath`)
  1055. proc initFromJson[T: distinct](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  1056. assignDistinctImpl(dst, jsonNode, jsonPath)
  1057. proc detectIncompatibleType(typeExpr, lineinfoNode: NimNode) =
  1058. if typeExpr.kind == nnkTupleConstr:
  1059. error("Use a named tuple instead of: " & typeExpr.repr, lineinfoNode)
  1060. proc foldObjectBody(dst, typeNode, tmpSym, jsonNode, jsonPath, originalJsonPathLen: NimNode) =
  1061. case typeNode.kind
  1062. of nnkEmpty:
  1063. discard
  1064. of nnkRecList, nnkTupleTy:
  1065. for it in typeNode:
  1066. foldObjectBody(dst, it, tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1067. of nnkIdentDefs:
  1068. typeNode.expectLen 3
  1069. let fieldSym = typeNode[0]
  1070. let fieldNameLit = newLit(fieldSym.strVal)
  1071. let fieldPathLit = newLit("." & fieldSym.strVal)
  1072. let fieldType = typeNode[1]
  1073. # Detecting incompatiple tuple types in `assignObjectImpl` only
  1074. # would be much cleaner, but the ast for tuple types does not
  1075. # contain usable type information.
  1076. detectIncompatibleType(fieldType, fieldSym)
  1077. dst.add quote do:
  1078. jsonPath.add `fieldPathLit`
  1079. when nimvm:
  1080. when isRefSkipDistinct(`tmpSym`.`fieldSym`):
  1081. # workaround #12489
  1082. var tmp: `fieldType`
  1083. initFromJson(tmp, getOrDefault(`jsonNode`,`fieldNameLit`), `jsonPath`)
  1084. `tmpSym`.`fieldSym` = tmp
  1085. else:
  1086. initFromJson(`tmpSym`.`fieldSym`, getOrDefault(`jsonNode`,`fieldNameLit`), `jsonPath`)
  1087. else:
  1088. initFromJson(`tmpSym`.`fieldSym`, getOrDefault(`jsonNode`,`fieldNameLit`), `jsonPath`)
  1089. jsonPath.setLen `originalJsonPathLen`
  1090. of nnkRecCase:
  1091. let kindSym = typeNode[0][0]
  1092. let kindNameLit = newLit(kindSym.strVal)
  1093. let kindPathLit = newLit("." & kindSym.strVal)
  1094. let kindType = typeNode[0][1]
  1095. let kindOffsetLit = newLit(uint(getOffset(kindSym)))
  1096. dst.add quote do:
  1097. var kindTmp: `kindType`
  1098. jsonPath.add `kindPathLit`
  1099. initFromJson(kindTmp, `jsonNode`[`kindNameLit`], `jsonPath`)
  1100. jsonPath.setLen `originalJsonPathLen`
  1101. when defined js:
  1102. `tmpSym`.`kindSym` = kindTmp
  1103. else:
  1104. when nimvm:
  1105. `tmpSym`.`kindSym` = kindTmp
  1106. else:
  1107. # fuck it, assign kind field anyway
  1108. ((cast[ptr `kindType`](cast[uint](`tmpSym`.addr) + `kindOffsetLit`))[]) = kindTmp
  1109. dst.add nnkCaseStmt.newTree(nnkDotExpr.newTree(tmpSym, kindSym))
  1110. for i in 1 ..< typeNode.len:
  1111. foldObjectBody(dst, typeNode[i], tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1112. of nnkOfBranch, nnkElse:
  1113. let ofBranch = newNimNode(typeNode.kind)
  1114. for i in 0 ..< typeNode.len-1:
  1115. ofBranch.add copyNimTree(typeNode[i])
  1116. let dstInner = newNimNode(nnkStmtListExpr)
  1117. foldObjectBody(dstInner, typeNode[^1], tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1118. # resOuter now contains the inner stmtList
  1119. ofBranch.add dstInner
  1120. dst[^1].expectKind nnkCaseStmt
  1121. dst[^1].add ofBranch
  1122. of nnkObjectTy:
  1123. typeNode[0].expectKind nnkEmpty
  1124. typeNode[1].expectKind {nnkEmpty, nnkOfInherit}
  1125. if typeNode[1].kind == nnkOfInherit:
  1126. let base = typeNode[1][0]
  1127. var impl = getTypeImpl(base)
  1128. while impl.kind in {nnkRefTy, nnkPtrTy}:
  1129. impl = getTypeImpl(impl[0])
  1130. foldObjectBody(dst, impl, tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1131. let body = typeNode[2]
  1132. foldObjectBody(dst, body, tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1133. else:
  1134. error("unhandled kind: " & $typeNode.kind, typeNode)
  1135. macro assignObjectImpl[T](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  1136. let typeSym = getTypeInst(dst)
  1137. let originalJsonPathLen = genSym(nskLet, "originalJsonPathLen")
  1138. result = newStmtList()
  1139. result.add quote do:
  1140. let `originalJsonPathLen` = len(`jsonPath`)
  1141. if typeSym.kind in {nnkTupleTy, nnkTupleConstr}:
  1142. # both, `dst` and `typeSym` don't have good lineinfo. But nothing
  1143. # else is available here.
  1144. detectIncompatibleType(typeSym, dst)
  1145. foldObjectBody(result, typeSym, dst, jsonNode, jsonPath, originalJsonPathLen)
  1146. else:
  1147. foldObjectBody(result, typeSym.getTypeImpl, dst, jsonNode, jsonPath, originalJsonPathLen)
  1148. proc initFromJson[T: object|tuple](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  1149. assignObjectImpl(dst, jsonNode, jsonPath)
  1150. proc to*[T](node: JsonNode, t: typedesc[T]): T =
  1151. ## `Unmarshals`:idx: the specified node into the object type specified.
  1152. ##
  1153. ## Known limitations:
  1154. ##
  1155. ## * Heterogeneous arrays are not supported.
  1156. ## * Sets in object variants are not supported.
  1157. ## * Not nil annotations are not supported.
  1158. ##
  1159. runnableExamples:
  1160. let jsonNode = parseJson("""
  1161. {
  1162. "person": {
  1163. "name": "Nimmer",
  1164. "age": 21
  1165. },
  1166. "list": [1, 2, 3, 4]
  1167. }
  1168. """)
  1169. type
  1170. Person = object
  1171. name: string
  1172. age: int
  1173. Data = object
  1174. person: Person
  1175. list: seq[int]
  1176. var data = to(jsonNode, Data)
  1177. doAssert data.person.name == "Nimmer"
  1178. doAssert data.person.age == 21
  1179. doAssert data.list == @[1, 2, 3, 4]
  1180. var jsonPath = ""
  1181. initFromJson(result, node, jsonPath)
  1182. when false:
  1183. import os
  1184. var s = newFileStream(paramStr(1), fmRead)
  1185. if s == nil: quit("cannot open the file" & paramStr(1))
  1186. var x: JsonParser
  1187. open(x, s, paramStr(1))
  1188. while true:
  1189. next(x)
  1190. case x.kind
  1191. of jsonError:
  1192. Echo(x.errorMsg())
  1193. break
  1194. of jsonEof: break
  1195. of jsonString, jsonInt, jsonFloat: echo(x.str)
  1196. of jsonTrue: echo("!TRUE")
  1197. of jsonFalse: echo("!FALSE")
  1198. of jsonNull: echo("!NULL")
  1199. of jsonObjectStart: echo("{")
  1200. of jsonObjectEnd: echo("}")
  1201. of jsonArrayStart: echo("[")
  1202. of jsonArrayEnd: echo("]")
  1203. close(x)
  1204. # { "json": 5 }
  1205. # To get that we shall use, obj["json"]