json.nim 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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. ## Overview
  17. ## ========
  18. ##
  19. ## Parsing JSON
  20. ## ------------
  21. ##
  22. ## JSON often arrives into your program (via an API or a file) as a ``string``.
  23. ## The first step is to change it from its serialized form into a nested object
  24. ## structure called a ``JsonNode``.
  25. ##
  26. ## The ``parseJson`` procedure takes a string containing JSON and returns a
  27. ## ``JsonNode`` object. This is an object variant and it is either a
  28. ## ``JObject``, ``JArray``, ``JString``, ``JInt``, ``JFloat``, ``JBool`` or
  29. ## ``JNull``. You check the kind of this object variant by using the ``kind``
  30. ## accessor.
  31. ##
  32. ## For a ``JsonNode`` who's kind is ``JObject``, you can acess its fields using
  33. ## the ``[]`` operator. The following example shows how to do this:
  34. ##
  35. ## .. code-block:: Nim
  36. ## import json
  37. ##
  38. ## let jsonNode = parseJson("""{"key": 3.14}""")
  39. ##
  40. ## doAssert jsonNode.kind == JObject
  41. ## doAssert jsonNode["key"].kind == JFloat
  42. ##
  43. ## Reading values
  44. ## --------------
  45. ##
  46. ## Once you have a ``JsonNode``, retrieving the values can then be achieved
  47. ## by using one of the helper procedures, which include:
  48. ##
  49. ## * ``getInt``
  50. ## * ``getFloat``
  51. ## * ``getStr``
  52. ## * ``getBool``
  53. ##
  54. ## To retrieve the value of ``"key"`` you can do the following:
  55. ##
  56. ## .. code-block:: Nim
  57. ## import json
  58. ##
  59. ## let jsonNode = parseJson("""{"key": 3.14}""")
  60. ##
  61. ## doAssert jsonNode["key"].getFloat() == 3.14
  62. ##
  63. ## **Important:** The ``[]`` operator will raise an exception when the
  64. ## specified field does not exist.
  65. ##
  66. ## Handling optional keys
  67. ## ----------------------
  68. ##
  69. ## By using the ``{}`` operator instead of ``[]``, it will return ``nil``
  70. ## when the field is not found. The ``get``-family of procedures will return a
  71. ## type's default value when called on ``nil``.
  72. ##
  73. ## .. code-block:: Nim
  74. ## import json
  75. ##
  76. ## let jsonNode = parseJson("{}")
  77. ##
  78. ## doAssert jsonNode{"nope"}.getInt() == 0
  79. ## doAssert jsonNode{"nope"}.getFloat() == 0
  80. ## doAssert jsonNode{"nope"}.getStr() == ""
  81. ## doAssert jsonNode{"nope"}.getBool() == false
  82. ##
  83. ## Using default values
  84. ## --------------------
  85. ##
  86. ## The ``get``-family helpers also accept an additional parameter which allow
  87. ## you to fallback to a default value should the key's values be ``null``:
  88. ##
  89. ## .. code-block:: Nim
  90. ## import json
  91. ##
  92. ## let jsonNode = parseJson("""{"key": 3.14, "key2": null}""")
  93. ##
  94. ## doAssert jsonNode["key"].getFloat(6.28) == 3.14
  95. ## doAssert jsonNode["key2"].getFloat(3.14) == 3.14
  96. ## doAssert jsonNode{"nope"}.getFloat(3.14) == 3.14 # note the {}
  97. ##
  98. ## Unmarshalling
  99. ## -------------
  100. ##
  101. ## In addition to reading dynamic data, Nim can also unmarshall JSON directly
  102. ## into a type with the ``to`` macro.
  103. ##
  104. ## .. code-block:: Nim
  105. ## import json
  106. ##
  107. ## type
  108. ## User = object
  109. ## name: string
  110. ## age: int
  111. ##
  112. ## let userJson = parseJson("""{ "name": "Nim", "age": 12 }""")
  113. ## let user = to(userJson, User)
  114. ##
  115. ## Creating JSON
  116. ## =============
  117. ##
  118. ## This module can also be used to comfortably create JSON using the ``%*``
  119. ## operator:
  120. ##
  121. ## .. code-block:: nim
  122. ## import json
  123. ##
  124. ## var hisName = "John"
  125. ## let herAge = 31
  126. ## var j = %*
  127. ## [
  128. ## { "name": hisName, "age": 30 },
  129. ## { "name": "Susan", "age": herAge }
  130. ## ]
  131. ##
  132. ## var j2 = %* {"name": "Isaac", "books": ["Robot Dreams"]}
  133. ## j2["details"] = %* {"age":35, "pi":3.1415}
  134. ## echo j2
  135. import
  136. hashes, tables, strutils, lexbase, streams, unicode, macros, parsejson
  137. export
  138. tables.`$`
  139. export
  140. parsejson.JsonEventKind, parsejson.JsonError, JsonParser, JsonKindError,
  141. open, close, str, getInt, getFloat, kind, getColumn, getLine, getFilename,
  142. errorMsg, errorMsgExpected, next, JsonParsingError, raiseParseErr
  143. when defined(nimJsonGet):
  144. {.pragma: deprecatedGet, deprecated.}
  145. else:
  146. {.pragma: deprecatedGet.}
  147. type
  148. JsonNodeKind* = enum ## possible JSON node types
  149. JNull,
  150. JBool,
  151. JInt,
  152. JFloat,
  153. JString,
  154. JObject,
  155. JArray
  156. JsonNode* = ref JsonNodeObj ## JSON node
  157. JsonNodeObj* {.acyclic.} = object
  158. case kind*: JsonNodeKind
  159. of JString:
  160. str*: string
  161. of JInt:
  162. num*: BiggestInt
  163. of JFloat:
  164. fnum*: float
  165. of JBool:
  166. bval*: bool
  167. of JNull:
  168. nil
  169. of JObject:
  170. fields*: OrderedTable[string, JsonNode]
  171. of JArray:
  172. elems*: seq[JsonNode]
  173. proc newJString*(s: string): JsonNode =
  174. ## Creates a new `JString JsonNode`.
  175. new(result)
  176. result.kind = JString
  177. result.str = s
  178. proc newJStringMove(s: string): JsonNode =
  179. new(result)
  180. result.kind = JString
  181. shallowCopy(result.str, s)
  182. proc newJInt*(n: BiggestInt): JsonNode =
  183. ## Creates a new `JInt JsonNode`.
  184. new(result)
  185. result.kind = JInt
  186. result.num = n
  187. proc newJFloat*(n: float): JsonNode =
  188. ## Creates a new `JFloat JsonNode`.
  189. new(result)
  190. result.kind = JFloat
  191. result.fnum = n
  192. proc newJBool*(b: bool): JsonNode =
  193. ## Creates a new `JBool JsonNode`.
  194. new(result)
  195. result.kind = JBool
  196. result.bval = b
  197. proc newJNull*(): JsonNode =
  198. ## Creates a new `JNull JsonNode`.
  199. new(result)
  200. proc newJObject*(): JsonNode =
  201. ## Creates a new `JObject JsonNode`
  202. new(result)
  203. result.kind = JObject
  204. result.fields = initOrderedTable[string, JsonNode](4)
  205. proc newJArray*(): JsonNode =
  206. ## Creates a new `JArray JsonNode`
  207. new(result)
  208. result.kind = JArray
  209. result.elems = @[]
  210. proc getStr*(n: JsonNode, default: string = ""): string =
  211. ## Retrieves the string value of a `JString JsonNode`.
  212. ##
  213. ## Returns ``default`` if ``n`` is not a ``JString``, or if ``n`` is nil.
  214. if n.isNil or n.kind != JString: return default
  215. else: return n.str
  216. proc getInt*(n: JsonNode, default: int = 0): int =
  217. ## Retrieves the int value of a `JInt JsonNode`.
  218. ##
  219. ## Returns ``default`` if ``n`` is not a ``JInt``, or if ``n`` is nil.
  220. if n.isNil or n.kind != JInt: return default
  221. else: return int(n.num)
  222. proc getBiggestInt*(n: JsonNode, default: BiggestInt = 0): BiggestInt =
  223. ## Retrieves the BiggestInt value of a `JInt JsonNode`.
  224. ##
  225. ## Returns ``default`` if ``n`` is not a ``JInt``, or if ``n`` is nil.
  226. if n.isNil or n.kind != JInt: return default
  227. else: return n.num
  228. proc getNum*(n: JsonNode, default: BiggestInt = 0): BiggestInt {.deprecated: "use getInt or getBiggestInt instead".} =
  229. ## **Deprecated since v0.18.2:** use ``getInt`` or ``getBiggestInt`` instead.
  230. getBiggestInt(n, default)
  231. proc getFloat*(n: JsonNode, default: float = 0.0): float =
  232. ## Retrieves the float value of a `JFloat JsonNode`.
  233. ##
  234. ## Returns ``default`` if ``n`` is not a ``JFloat`` or ``JInt``, or if ``n`` is nil.
  235. if n.isNil: return default
  236. case n.kind
  237. of JFloat: return n.fnum
  238. of JInt: return float(n.num)
  239. else: return default
  240. proc getFNum*(n: JsonNode, default: float = 0.0): float {.deprecated: "use getFloat instead".} =
  241. ## **Deprecated since v0.18.2:** use ``getFloat`` instead.
  242. getFloat(n, default)
  243. proc getBool*(n: JsonNode, default: bool = false): bool =
  244. ## Retrieves the bool value of a `JBool JsonNode`.
  245. ##
  246. ## Returns ``default`` if ``n`` is not a ``JBool``, or if ``n`` is nil.
  247. if n.isNil or n.kind != JBool: return default
  248. else: return n.bval
  249. proc getBVal*(n: JsonNode, default: bool = false): bool {.deprecated: "use getBool instead".} =
  250. ## **Deprecated since v0.18.2:** use ``getBool`` instead.
  251. getBool(n, default)
  252. proc getFields*(n: JsonNode,
  253. default = initOrderedTable[string, JsonNode](4)):
  254. OrderedTable[string, JsonNode] =
  255. ## Retrieves the key, value pairs of a `JObject JsonNode`.
  256. ##
  257. ## Returns ``default`` if ``n`` is not a ``JObject``, or if ``n`` is nil.
  258. if n.isNil or n.kind != JObject: return default
  259. else: return n.fields
  260. proc getElems*(n: JsonNode, default: seq[JsonNode] = @[]): seq[JsonNode] =
  261. ## Retrieves the array of a `JArray JsonNode`.
  262. ##
  263. ## Returns ``default`` if ``n`` is not a ``JArray``, or if ``n`` is nil.
  264. if n.isNil or n.kind != JArray: return default
  265. else: return n.elems
  266. proc add*(father, child: JsonNode) =
  267. ## Adds `child` to a JArray node `father`.
  268. assert father.kind == JArray
  269. father.elems.add(child)
  270. proc add*(obj: JsonNode, key: string, val: JsonNode) =
  271. ## Sets a field from a `JObject`.
  272. assert obj.kind == JObject
  273. obj.fields[key] = val
  274. proc `%`*(s: string): JsonNode =
  275. ## Generic constructor for JSON data. Creates a new `JString JsonNode`.
  276. new(result)
  277. result.kind = JString
  278. result.str = s
  279. proc `%`*(n: BiggestInt): JsonNode =
  280. ## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
  281. new(result)
  282. result.kind = JInt
  283. result.num = n
  284. proc `%`*(n: float): JsonNode =
  285. ## Generic constructor for JSON data. Creates a new `JFloat JsonNode`.
  286. new(result)
  287. result.kind = JFloat
  288. result.fnum = n
  289. proc `%`*(b: bool): JsonNode =
  290. ## Generic constructor for JSON data. Creates a new `JBool JsonNode`.
  291. new(result)
  292. result.kind = JBool
  293. result.bval = b
  294. proc `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode =
  295. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  296. if keyvals.len == 0: return newJArray()
  297. result = newJObject()
  298. for key, val in items(keyVals): result.fields[key] = val
  299. template `%`*(j: JsonNode): JsonNode = j
  300. proc `%`*[T](elements: openArray[T]): JsonNode =
  301. ## Generic constructor for JSON data. Creates a new `JArray JsonNode`
  302. result = newJArray()
  303. for elem in elements: result.add(%elem)
  304. when false:
  305. # For 'consistency' we could do this, but that only pushes people further
  306. # into that evil comfort zone where they can use Nim without understanding it
  307. # causing problems later on.
  308. proc `%`*(elements: set[bool]): JsonNode =
  309. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`.
  310. ## This can only be used with the empty set ``{}`` and is supported
  311. ## to prevent the gotcha ``%*{}`` which used to produce an empty
  312. ## JSON array.
  313. result = newJObject()
  314. assert false notin elements, "usage error: only empty sets allowed"
  315. assert true notin elements, "usage error: only empty sets allowed"
  316. proc `%`*(o: object): JsonNode =
  317. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  318. result = newJObject()
  319. for k, v in o.fieldPairs: result[k] = %v
  320. proc `%`*(o: ref object): JsonNode =
  321. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  322. if o.isNil:
  323. result = newJNull()
  324. else:
  325. result = %(o[])
  326. proc `%`*(o: enum): JsonNode =
  327. ## Construct a JsonNode that represents the specified enum value as a
  328. ## string. Creates a new ``JString JsonNode``.
  329. result = %($o)
  330. proc toJson(x: NimNode): NimNode {.compiletime.} =
  331. case x.kind
  332. of nnkBracket: # array
  333. if x.len == 0: return newCall(bindSym"newJArray")
  334. result = newNimNode(nnkBracket)
  335. for i in 0 ..< x.len:
  336. result.add(toJson(x[i]))
  337. result = newCall(bindSym("%", brOpen), result)
  338. of nnkTableConstr: # object
  339. if x.len == 0: return newCall(bindSym"newJObject")
  340. result = newNimNode(nnkTableConstr)
  341. for i in 0 ..< x.len:
  342. x[i].expectKind nnkExprColonExpr
  343. result.add newTree(nnkExprColonExpr, x[i][0], toJson(x[i][1]))
  344. result = newCall(bindSym("%", brOpen), result)
  345. of nnkCurly: # empty object
  346. x.expectLen(0)
  347. result = newCall(bindSym"newJObject")
  348. of nnkNilLit:
  349. result = newCall(bindSym"newJNull")
  350. of nnkPar:
  351. if x.len == 1: result = toJson(x[0])
  352. else: result = newCall(bindSym("%", brOpen), x)
  353. else:
  354. result = newCall(bindSym("%", brOpen), x)
  355. macro `%*`*(x: untyped): untyped =
  356. ## Convert an expression to a JsonNode directly, without having to specify
  357. ## `%` for every element.
  358. result = toJson(x)
  359. proc `==`* (a, b: JsonNode): bool =
  360. ## Check two nodes for equality
  361. if a.isNil:
  362. if b.isNil: return true
  363. return false
  364. elif b.isNil or a.kind != b.kind:
  365. return false
  366. else:
  367. case a.kind
  368. of JString:
  369. result = a.str == b.str
  370. of JInt:
  371. result = a.num == b.num
  372. of JFloat:
  373. result = a.fnum == b.fnum
  374. of JBool:
  375. result = a.bval == b.bval
  376. of JNull:
  377. result = true
  378. of JArray:
  379. result = a.elems == b.elems
  380. of JObject:
  381. # we cannot use OrderedTable's equality here as
  382. # the order does not matter for equality here.
  383. if a.fields.len != b.fields.len: return false
  384. for key, val in a.fields:
  385. if not b.fields.hasKey(key): return false
  386. if b.fields[key] != val: return false
  387. result = true
  388. proc hash*(n: OrderedTable[string, JsonNode]): Hash {.noSideEffect.}
  389. proc hash*(n: JsonNode): Hash =
  390. ## Compute the hash for a JSON node
  391. case n.kind
  392. of JArray:
  393. result = hash(n.elems)
  394. of JObject:
  395. result = hash(n.fields)
  396. of JInt:
  397. result = hash(n.num)
  398. of JFloat:
  399. result = hash(n.fnum)
  400. of JBool:
  401. result = hash(n.bval.int)
  402. of JString:
  403. result = hash(n.str)
  404. of JNull:
  405. result = Hash(0)
  406. proc hash*(n: OrderedTable[string, JsonNode]): Hash =
  407. for key, val in n:
  408. result = result xor (hash(key) !& hash(val))
  409. result = !$result
  410. proc len*(n: JsonNode): int =
  411. ## If `n` is a `JArray`, it returns the number of elements.
  412. ## If `n` is a `JObject`, it returns the number of pairs.
  413. ## Else it returns 0.
  414. case n.kind
  415. of JArray: result = n.elems.len
  416. of JObject: result = n.fields.len
  417. else: discard
  418. proc `[]`*(node: JsonNode, name: string): JsonNode {.inline, deprecatedGet.} =
  419. ## Gets a field from a `JObject`, which must not be nil.
  420. ## If the value at `name` does not exist, raises KeyError.
  421. ##
  422. ## **Note:** The behaviour of this procedure changed in version 0.14.0. To
  423. ## get a list of usages and to restore the old behaviour of this procedure,
  424. ## compile with the ``-d:nimJsonGet`` flag.
  425. assert(not isNil(node))
  426. assert(node.kind == JObject)
  427. when defined(nimJsonGet):
  428. if not node.fields.hasKey(name): return nil
  429. result = node.fields[name]
  430. proc `[]`*(node: JsonNode, index: int): JsonNode {.inline.} =
  431. ## Gets the node at `index` in an Array. Result is undefined if `index`
  432. ## is out of bounds, but as long as array bound checks are enabled it will
  433. ## result in an exception.
  434. assert(not isNil(node))
  435. assert(node.kind == JArray)
  436. return node.elems[index]
  437. proc hasKey*(node: JsonNode, key: string): bool =
  438. ## Checks if `key` exists in `node`.
  439. assert(node.kind == JObject)
  440. result = node.fields.hasKey(key)
  441. proc contains*(node: JsonNode, key: string): bool =
  442. ## Checks if `key` exists in `node`.
  443. assert(node.kind == JObject)
  444. node.fields.hasKey(key)
  445. proc contains*(node: JsonNode, val: JsonNode): bool =
  446. ## Checks if `val` exists in array `node`.
  447. assert(node.kind == JArray)
  448. find(node.elems, val) >= 0
  449. proc existsKey*(node: JsonNode, key: string): bool {.deprecated: "use hasKey instead".} = node.hasKey(key)
  450. ## **Deprecated:** use `hasKey` instead.
  451. proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) {.inline.} =
  452. ## Sets a field from a `JObject`.
  453. assert(obj.kind == JObject)
  454. obj.fields[key] = val
  455. proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =
  456. ## Traverses the node and gets the given value. If any of the
  457. ## keys do not exist, returns ``nil``. Also returns ``nil`` if one of the
  458. ## intermediate data structures is not an object.
  459. ##
  460. ## This proc can be used to create tree structures on the
  461. ## fly (sometimes called `autovivification`:idx:):
  462. ##
  463. ## .. code-block:: nim
  464. ## myjson{"parent", "child", "grandchild"} = newJInt(1)
  465. ##
  466. result = node
  467. for key in keys:
  468. if isNil(result) or result.kind != JObject:
  469. return nil
  470. result = result.fields.getOrDefault(key)
  471. proc `{}`*(node: JsonNode, index: varargs[int]): JsonNode =
  472. ## Traverses the node and gets the given value. If any of the
  473. ## indexes do not exist, returns ``nil``. Also returns ``nil`` if one of the
  474. ## intermediate data structures is not an array.
  475. result = node
  476. for i in index:
  477. if isNil(result) or result.kind != JArray or i >= node.len:
  478. return nil
  479. result = result.elems[i]
  480. proc getOrDefault*(node: JsonNode, key: string): JsonNode =
  481. ## Gets a field from a `node`. If `node` is nil or not an object or
  482. ## value at `key` does not exist, returns nil
  483. if not isNil(node) and node.kind == JObject:
  484. result = node.fields.getOrDefault(key)
  485. template simpleGetOrDefault*{`{}`(node, [key])}(node: JsonNode, key: string): JsonNode = node.getOrDefault(key)
  486. proc `{}=`*(node: JsonNode, keys: varargs[string], value: JsonNode) =
  487. ## Traverses the node and tries to set the value at the given location
  488. ## to ``value``. If any of the keys are missing, they are added.
  489. var node = node
  490. for i in 0..(keys.len-2):
  491. if not node.hasKey(keys[i]):
  492. node[keys[i]] = newJObject()
  493. node = node[keys[i]]
  494. node[keys[keys.len-1]] = value
  495. proc delete*(obj: JsonNode, key: string) =
  496. ## Deletes ``obj[key]``.
  497. assert(obj.kind == JObject)
  498. if not obj.fields.hasKey(key):
  499. raise newException(KeyError, "key not in object")
  500. obj.fields.del(key)
  501. proc copy*(p: JsonNode): JsonNode =
  502. ## Performs a deep copy of `a`.
  503. case p.kind
  504. of JString:
  505. result = newJString(p.str)
  506. of JInt:
  507. result = newJInt(p.num)
  508. of JFloat:
  509. result = newJFloat(p.fnum)
  510. of JBool:
  511. result = newJBool(p.bval)
  512. of JNull:
  513. result = newJNull()
  514. of JObject:
  515. result = newJObject()
  516. for key, val in pairs(p.fields):
  517. result.fields[key] = copy(val)
  518. of JArray:
  519. result = newJArray()
  520. for i in items(p.elems):
  521. result.elems.add(copy(i))
  522. # ------------- pretty printing ----------------------------------------------
  523. proc indent(s: var string, i: int) =
  524. s.add(spaces(i))
  525. proc newIndent(curr, indent: int, ml: bool): int =
  526. if ml: return curr + indent
  527. else: return indent
  528. proc nl(s: var string, ml: bool) =
  529. s.add(if ml: "\n" else: " ")
  530. proc escapeJsonUnquoted*(s: string; result: var string) =
  531. ## Converts a string `s` to its JSON representation without quotes.
  532. ## Appends to ``result``.
  533. for c in s:
  534. case c
  535. of '\L': result.add("\\n")
  536. of '\b': result.add("\\b")
  537. of '\f': result.add("\\f")
  538. of '\t': result.add("\\t")
  539. of '\r': result.add("\\r")
  540. of '"': result.add("\\\"")
  541. of '\0'..'\7': result.add("\\u000" & $ord(c))
  542. of '\14'..'\31': result.add("\\u00" & $ord(c))
  543. of '\\': result.add("\\\\")
  544. else: result.add(c)
  545. proc escapeJsonUnquoted*(s: string): string =
  546. ## Converts a string `s` to its JSON representation without quotes.
  547. result = newStringOfCap(s.len + s.len shr 3)
  548. escapeJsonUnquoted(s, result)
  549. proc escapeJson*(s: string; result: var string) =
  550. ## Converts a string `s` to its JSON representation with quotes.
  551. ## Appends to ``result``.
  552. result.add("\"")
  553. escapeJsonUnquoted(s, result)
  554. result.add("\"")
  555. proc escapeJson*(s: string): string =
  556. ## Converts a string `s` to its JSON representation with quotes.
  557. result = newStringOfCap(s.len + s.len shr 3)
  558. escapeJson(s, result)
  559. proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true,
  560. lstArr = false, currIndent = 0) =
  561. case node.kind
  562. of JObject:
  563. if lstArr: result.indent(currIndent) # Indentation
  564. if node.fields.len > 0:
  565. result.add("{")
  566. result.nl(ml) # New line
  567. var i = 0
  568. for key, val in pairs(node.fields):
  569. if i > 0:
  570. result.add(",")
  571. result.nl(ml) # New Line
  572. inc i
  573. # Need to indent more than {
  574. result.indent(newIndent(currIndent, indent, ml))
  575. escapeJson(key, result)
  576. result.add(": ")
  577. toPretty(result, val, indent, ml, false,
  578. newIndent(currIndent, indent, ml))
  579. result.nl(ml)
  580. result.indent(currIndent) # indent the same as {
  581. result.add("}")
  582. else:
  583. result.add("{}")
  584. of JString:
  585. if lstArr: result.indent(currIndent)
  586. escapeJson(node.str, result)
  587. of JInt:
  588. if lstArr: result.indent(currIndent)
  589. when defined(js): result.add($node.num)
  590. else: result.add(node.num)
  591. of JFloat:
  592. if lstArr: result.indent(currIndent)
  593. # Fixme: implement new system.add ops for the JS target
  594. when defined(js): result.add($node.fnum)
  595. else: result.add(node.fnum)
  596. of JBool:
  597. if lstArr: result.indent(currIndent)
  598. result.add(if node.bval: "true" else: "false")
  599. of JArray:
  600. if lstArr: result.indent(currIndent)
  601. if len(node.elems) != 0:
  602. result.add("[")
  603. result.nl(ml)
  604. for i in 0..len(node.elems)-1:
  605. if i > 0:
  606. result.add(",")
  607. result.nl(ml) # New Line
  608. toPretty(result, node.elems[i], indent, ml,
  609. true, newIndent(currIndent, indent, ml))
  610. result.nl(ml)
  611. result.indent(currIndent)
  612. result.add("]")
  613. else: result.add("[]")
  614. of JNull:
  615. if lstArr: result.indent(currIndent)
  616. result.add("null")
  617. proc pretty*(node: JsonNode, indent = 2): string =
  618. ## Returns a JSON Representation of `node`, with indentation and
  619. ## on multiple lines.
  620. result = ""
  621. toPretty(result, node, indent)
  622. proc toUgly*(result: var string, node: JsonNode) =
  623. ## Converts `node` to its JSON Representation, without
  624. ## regard for human readability. Meant to improve ``$`` string
  625. ## conversion performance.
  626. ##
  627. ## JSON representation is stored in the passed `result`
  628. ##
  629. ## This provides higher efficiency than the ``pretty`` procedure as it
  630. ## does **not** attempt to format the resulting JSON to make it human readable.
  631. var comma = false
  632. case node.kind:
  633. of JArray:
  634. result.add "["
  635. for child in node.elems:
  636. if comma: result.add ","
  637. else: comma = true
  638. result.toUgly child
  639. result.add "]"
  640. of JObject:
  641. result.add "{"
  642. for key, value in pairs(node.fields):
  643. if comma: result.add ","
  644. else: comma = true
  645. key.escapeJson(result)
  646. result.add ":"
  647. result.toUgly value
  648. result.add "}"
  649. of JString:
  650. node.str.escapeJson(result)
  651. of JInt:
  652. when defined(js): result.add($node.num)
  653. else: result.add(node.num)
  654. of JFloat:
  655. when defined(js): result.add($node.fnum)
  656. else: result.add(node.fnum)
  657. of JBool:
  658. result.add(if node.bval: "true" else: "false")
  659. of JNull:
  660. result.add "null"
  661. proc `$`*(node: JsonNode): string =
  662. ## Converts `node` to its JSON Representation on one line.
  663. result = newStringOfCap(node.len shl 1)
  664. toUgly(result, node)
  665. iterator items*(node: JsonNode): JsonNode =
  666. ## Iterator for the items of `node`. `node` has to be a JArray.
  667. assert node.kind == JArray
  668. for i in items(node.elems):
  669. yield i
  670. iterator mitems*(node: var JsonNode): var JsonNode =
  671. ## Iterator for the items of `node`. `node` has to be a JArray. Items can be
  672. ## modified.
  673. assert node.kind == JArray
  674. for i in mitems(node.elems):
  675. yield i
  676. iterator pairs*(node: JsonNode): tuple[key: string, val: JsonNode] =
  677. ## Iterator for the child elements of `node`. `node` has to be a JObject.
  678. assert node.kind == JObject
  679. for key, val in pairs(node.fields):
  680. yield (key, val)
  681. iterator mpairs*(node: var JsonNode): tuple[key: string, val: var JsonNode] =
  682. ## Iterator for the child elements of `node`. `node` has to be a JObject.
  683. ## Values can be modified
  684. assert node.kind == JObject
  685. for key, val in mpairs(node.fields):
  686. yield (key, val)
  687. proc parseJson(p: var JsonParser): JsonNode =
  688. ## Parses JSON from a JSON Parser `p`.
  689. case p.tok
  690. of tkString:
  691. # we capture 'p.a' here, so we need to give it a fresh buffer afterwards:
  692. result = newJStringMove(p.a)
  693. p.a = ""
  694. discard getTok(p)
  695. of tkInt:
  696. result = newJInt(parseBiggestInt(p.a))
  697. discard getTok(p)
  698. of tkFloat:
  699. result = newJFloat(parseFloat(p.a))
  700. discard getTok(p)
  701. of tkTrue:
  702. result = newJBool(true)
  703. discard getTok(p)
  704. of tkFalse:
  705. result = newJBool(false)
  706. discard getTok(p)
  707. of tkNull:
  708. result = newJNull()
  709. discard getTok(p)
  710. of tkCurlyLe:
  711. result = newJObject()
  712. discard getTok(p)
  713. while p.tok != tkCurlyRi:
  714. if p.tok != tkString:
  715. raiseParseErr(p, "string literal as key")
  716. var key = p.a
  717. discard getTok(p)
  718. eat(p, tkColon)
  719. var val = parseJson(p)
  720. result[key] = val
  721. if p.tok != tkComma: break
  722. discard getTok(p)
  723. eat(p, tkCurlyRi)
  724. of tkBracketLe:
  725. result = newJArray()
  726. discard getTok(p)
  727. while p.tok != tkBracketRi:
  728. result.add(parseJson(p))
  729. if p.tok != tkComma: break
  730. discard getTok(p)
  731. eat(p, tkBracketRi)
  732. of tkError, tkCurlyRi, tkBracketRi, tkColon, tkComma, tkEof:
  733. raiseParseErr(p, "{")
  734. when not defined(js):
  735. proc parseJson*(s: Stream, filename: string = ""): JsonNode =
  736. ## Parses from a stream `s` into a `JsonNode`. `filename` is only needed
  737. ## for nice error messages.
  738. ## If `s` contains extra data, it will raise `JsonParsingError`.
  739. var p: JsonParser
  740. p.open(s, filename)
  741. try:
  742. discard getTok(p) # read first token
  743. result = p.parseJson()
  744. eat(p, tkEof) # check if there is no extra data
  745. finally:
  746. p.close()
  747. proc parseJson*(buffer: string): JsonNode =
  748. ## Parses JSON from `buffer`.
  749. ## If `buffer` contains extra data, it will raise `JsonParsingError`.
  750. result = parseJson(newStringStream(buffer), "input")
  751. proc parseFile*(filename: string): JsonNode =
  752. ## Parses `file` into a `JsonNode`.
  753. ## If `file` contains extra data, it will raise `JsonParsingError`.
  754. var stream = newFileStream(filename, fmRead)
  755. if stream == nil:
  756. raise newException(IOError, "cannot read from file: " & filename)
  757. result = parseJson(stream, filename)
  758. else:
  759. from math import `mod`
  760. type
  761. JSObject = object
  762. proc parseNativeJson(x: cstring): JSObject {.importc: "JSON.parse".}
  763. proc getVarType(x: JSObject): JsonNodeKind =
  764. result = JNull
  765. proc getProtoName(y: JSObject): cstring
  766. {.importc: "Object.prototype.toString.call".}
  767. case $getProtoName(x) # TODO: Implicit returns fail here.
  768. of "[object Array]": return JArray
  769. of "[object Object]": return JObject
  770. of "[object Number]":
  771. if cast[float](x) mod 1.0 == 0:
  772. return JInt
  773. else:
  774. return JFloat
  775. of "[object Boolean]": return JBool
  776. of "[object Null]": return JNull
  777. of "[object String]": return JString
  778. else: assert false
  779. proc len(x: JSObject): int =
  780. assert x.getVarType == JArray
  781. asm """
  782. `result` = `x`.length;
  783. """
  784. proc `[]`(x: JSObject, y: string): JSObject =
  785. assert x.getVarType == JObject
  786. asm """
  787. `result` = `x`[`y`];
  788. """
  789. proc `[]`(x: JSObject, y: int): JSObject =
  790. assert x.getVarType == JArray
  791. asm """
  792. `result` = `x`[`y`];
  793. """
  794. proc convertObject(x: JSObject): JsonNode =
  795. case getVarType(x)
  796. of JArray:
  797. result = newJArray()
  798. for i in 0 ..< x.len:
  799. result.add(x[i].convertObject())
  800. of JObject:
  801. result = newJObject()
  802. asm """for (var property in `x`) {
  803. if (`x`.hasOwnProperty(property)) {
  804. """
  805. var nimProperty: cstring
  806. var nimValue: JSObject
  807. asm "`nimProperty` = property; `nimValue` = `x`[property];"
  808. result[$nimProperty] = nimValue.convertObject()
  809. asm "}}"
  810. of JInt:
  811. result = newJInt(cast[int](x))
  812. of JFloat:
  813. result = newJFloat(cast[float](x))
  814. of JString:
  815. result = newJString($cast[cstring](x))
  816. of JBool:
  817. result = newJBool(cast[bool](x))
  818. of JNull:
  819. result = newJNull()
  820. proc parseJson*(buffer: string): JsonNode =
  821. return parseNativeJson(buffer).convertObject()
  822. # -- Json deserialiser macro. --
  823. proc createJsonIndexer(jsonNode: NimNode,
  824. index: string | int | NimNode): NimNode
  825. {.compileTime.} =
  826. when index is string:
  827. let indexNode = newStrLitNode(index)
  828. elif index is int:
  829. let indexNode = newIntLitNode(index)
  830. elif index is NimNode:
  831. let indexNode = index
  832. result = newNimNode(nnkBracketExpr).add(
  833. jsonNode,
  834. indexNode
  835. )
  836. proc transformJsonIndexer(jsonNode: NimNode): NimNode =
  837. case jsonNode.kind
  838. of nnkBracketExpr:
  839. result = newNimNode(nnkCurlyExpr)
  840. else:
  841. result = jsonNode.copy()
  842. for child in jsonNode:
  843. result.add(transformJsonIndexer(child))
  844. template verifyJsonKind(node: JsonNode, kinds: set[JsonNodeKind],
  845. ast: string) =
  846. if node.kind notin kinds:
  847. let msg = "Incorrect JSON kind. Wanted '$1' in '$2' but got '$3'." % [
  848. $kinds,
  849. ast,
  850. $node.kind
  851. ]
  852. raise newException(JsonKindError, msg)
  853. proc getEnum(node: JsonNode, ast: string, T: typedesc): T =
  854. when T is SomeInteger:
  855. # TODO: I shouldn't need this proc.
  856. proc convert[T](x: BiggestInt): T = T(x)
  857. verifyJsonKind(node, {JInt}, ast)
  858. return convert[T](node.getBiggestInt())
  859. else:
  860. verifyJsonKind(node, {JString}, ast)
  861. return parseEnum[T](node.getStr())
  862. proc toIdentNode(typeNode: NimNode): NimNode =
  863. ## Converts a Sym type node (returned by getType et al.) into an
  864. ## Ident node. Placing Sym type nodes inside the resulting code AST is
  865. ## unsound (according to @Araq) so this is necessary.
  866. case typeNode.kind
  867. of nnkSym:
  868. return newIdentNode($typeNode)
  869. of nnkBracketExpr:
  870. result = typeNode
  871. for i in 0..<len(result):
  872. result[i] = newIdentNode($result[i])
  873. of nnkIdent:
  874. return typeNode
  875. else:
  876. doAssert false, "Cannot convert typeNode to an ident node: " & $typeNode.kind
  877. proc createGetEnumCall(jsonNode, kindType: NimNode): NimNode =
  878. # -> getEnum(`jsonNode`, `kindType`)
  879. let getEnumSym = bindSym("getEnum")
  880. let astStrLit = toStrLit(jsonNode)
  881. let getEnumCall = newCall(getEnumSym, jsonNode, astStrLit, kindType)
  882. return getEnumCall
  883. proc createOfBranchCond(ofBranch, getEnumCall: NimNode): NimNode =
  884. ## Creates an expression that acts as the condition for an ``of`` branch.
  885. var cond = newIdentNode("false")
  886. for ofCond in ofBranch:
  887. if ofCond.kind == nnkRecList:
  888. break
  889. let comparison = infix(getEnumCall, "==", ofCond)
  890. cond = infix(cond, "or", comparison)
  891. return cond
  892. proc processObjField(field, jsonNode: NimNode): seq[NimNode] {.compileTime.}
  893. proc processOfBranch(ofBranch, jsonNode, kindType,
  894. kindJsonNode: NimNode): seq[NimNode] {.compileTime.} =
  895. ## Processes each field inside of an object's ``of`` branch.
  896. ## For each field a new ExprColonExpr node is created and put in the
  897. ## resulting list.
  898. ##
  899. ## Sample ``ofBranch`` AST:
  900. ##
  901. ## .. code-block::plain
  902. ## OfBranch of 0, 1:
  903. ## IntLit 0 foodPos: float
  904. ## IntLit 1 enemyPos: float
  905. ## RecList
  906. ## Sym "foodPos"
  907. ## Sym "enemyPos"
  908. result = @[]
  909. let getEnumCall = createGetEnumCall(kindJsonNode, kindType)
  910. for branchField in ofBranch[^1]:
  911. let objFields = processObjField(branchField, jsonNode)
  912. for objField in objFields:
  913. let exprColonExpr = newNimNode(nnkExprColonExpr)
  914. result.add(exprColonExpr)
  915. # Add the name of the field.
  916. exprColonExpr.add(toIdentNode(objField[0]))
  917. # Add the value of the field.
  918. let cond = createOfBranchCond(ofBranch, getEnumCall)
  919. exprColonExpr.add(newIfStmt(
  920. (cond, objField[1])
  921. ))
  922. proc processElseBranch(recCaseNode, elseBranch, jsonNode, kindType,
  923. kindJsonNode: NimNode): seq[NimNode] {.compileTime.} =
  924. ## Processes each field inside of a variant object's ``else`` branch.
  925. ##
  926. ## ..code-block::plain
  927. ## Else
  928. ## RecList
  929. ## Sym "other"
  930. result = @[]
  931. let getEnumCall = createGetEnumCall(kindJsonNode, kindType)
  932. # We need to build up a list of conditions from each ``of`` branch so that
  933. # we can then negate it to get ``else``.
  934. var cond = newIdentNode("false")
  935. for i in 1 ..< len(recCaseNode):
  936. if recCaseNode[i].kind == nnkElse:
  937. break
  938. cond = infix(cond, "or", createOfBranchCond(recCaseNode[i], getEnumCall))
  939. # Negate the condition.
  940. cond = prefix(cond, "not")
  941. for branchField in elseBranch[^1]:
  942. let objFields = processObjField(branchField, jsonNode)
  943. for objField in objFields:
  944. let exprColonExpr = newNimNode(nnkExprColonExpr)
  945. result.add(exprColonExpr)
  946. # Add the name of the field.
  947. exprColonExpr.add(toIdentNode(objField[0]))
  948. # Add the value of the field.
  949. let ifStmt = newIfStmt((cond, objField[1]))
  950. exprColonExpr.add(ifStmt)
  951. proc createConstructor(typeSym, jsonNode: NimNode): NimNode {.compileTime.}
  952. proc detectDistinctType(typeSym: NimNode): NimNode =
  953. let
  954. typeImpl = getTypeImpl(typeSym)
  955. typeInst = getTypeInst(typeSym)
  956. result = if typeImpl.typeKind == ntyDistinct: typeImpl else: typeInst
  957. proc processObjField(field, jsonNode: NimNode): seq[NimNode] =
  958. ## Process a field from a ``RecList``.
  959. ##
  960. ## The field will typically be a simple ``Sym`` node, but for object variants
  961. ## it may also be a ``RecCase`` in which case things become complicated.
  962. result = @[]
  963. case field.kind
  964. of nnkSym:
  965. # Ordinary field. For example, `name: string`.
  966. let exprColonExpr = newNimNode(nnkExprColonExpr)
  967. result.add(exprColonExpr)
  968. # Add the field name.
  969. exprColonExpr.add(toIdentNode(field))
  970. # Add the field value.
  971. # -> jsonNode["`field`"]
  972. let indexedJsonNode = createJsonIndexer(jsonNode, $field)
  973. let typeNode = detectDistinctType(field)
  974. exprColonExpr.add(createConstructor(typeNode, indexedJsonNode))
  975. of nnkRecCase:
  976. # A "case" field that introduces a variant.
  977. let exprColonExpr = newNimNode(nnkExprColonExpr)
  978. result.add(exprColonExpr)
  979. # Add the "case" field name (usually "kind").
  980. exprColonExpr.add(toIdentNode(field[0]))
  981. # -> jsonNode["`field[0]`"]
  982. let kindJsonNode = createJsonIndexer(jsonNode, $field[0])
  983. # Add the "case" field's value.
  984. let kindType = toIdentNode(getTypeInst(field[0]))
  985. let getEnumSym = bindSym("getEnum")
  986. let astStrLit = toStrLit(kindJsonNode)
  987. let getEnumCall = newCall(getEnumSym, kindJsonNode, astStrLit, kindType)
  988. exprColonExpr.add(getEnumCall)
  989. # Iterate through each `of` branch.
  990. for i in 1 ..< field.len:
  991. case field[i].kind
  992. of nnkOfBranch:
  993. result.add processOfBranch(field[i], jsonNode, kindType, kindJsonNode)
  994. of nnkElse:
  995. result.add processElseBranch(field, field[i], jsonNode, kindType, kindJsonNode)
  996. else:
  997. doAssert false, "Expected OfBranch or Else node kinds, got: " & $field[i].kind
  998. else:
  999. doAssert false, "Unable to process object field: " & $field.kind
  1000. doAssert result.len > 0
  1001. proc processFields(obj: NimNode,
  1002. jsonNode: NimNode): seq[NimNode] {.compileTime.} =
  1003. ## Process all the fields of an ``ObjectTy`` and any of its
  1004. ## parent type's fields (via inheritance).
  1005. result = @[]
  1006. case obj.kind
  1007. of nnkObjectTy:
  1008. expectKind(obj[2], nnkRecList)
  1009. for field in obj[2]:
  1010. let nodes = processObjField(field, jsonNode)
  1011. result.add(nodes)
  1012. # process parent type fields
  1013. case obj[1].kind
  1014. of nnkBracketExpr:
  1015. assert $obj[1][0] == "ref"
  1016. result.add(processFields(getType(obj[1][1]), jsonNode))
  1017. of nnkSym:
  1018. result.add(processFields(getType(obj[1]), jsonNode))
  1019. else:
  1020. discard
  1021. of nnkTupleTy:
  1022. for identDefs in obj:
  1023. expectKind(identDefs, nnkIdentDefs)
  1024. let nodes = processObjField(identDefs[0], jsonNode)
  1025. result.add(nodes)
  1026. else:
  1027. doAssert false, "Unable to process field type: " & $obj.kind
  1028. proc processType(typeName: NimNode, obj: NimNode,
  1029. jsonNode: NimNode, isRef: bool): NimNode {.compileTime.} =
  1030. ## Process a type such as ``Sym "float"`` or ``ObjectTy ...``.
  1031. ##
  1032. ## Sample ``ObjectTy``:
  1033. ##
  1034. ## .. code-block::plain
  1035. ## ObjectTy
  1036. ## Empty
  1037. ## InheritanceInformation
  1038. ## RecList
  1039. ## Sym "events"
  1040. case obj.kind
  1041. of nnkObjectTy, nnkTupleTy:
  1042. # Create object constructor.
  1043. result =
  1044. if obj.kind == nnkObjectTy: newNimNode(nnkObjConstr)
  1045. else: newNimNode(nnkPar)
  1046. if obj.kind == nnkObjectTy:
  1047. result.add(typeName) # Name of the type to construct.
  1048. # Process each object/tuple field and add it as an exprColonExpr
  1049. result.add(processFields(obj, jsonNode))
  1050. # Object might be null. So we need to check for that.
  1051. if isRef:
  1052. result = quote do:
  1053. verifyJsonKind(`jsonNode`, {JObject, JNull}, astToStr(`jsonNode`))
  1054. if `jsonNode`.kind == JNull:
  1055. nil
  1056. else:
  1057. `result`
  1058. else:
  1059. result = quote do:
  1060. verifyJsonKind(`jsonNode`, {JObject}, astToStr(`jsonNode`));
  1061. `result`
  1062. of nnkEnumTy:
  1063. let instType = toIdentNode(getTypeInst(typeName))
  1064. let getEnumCall = createGetEnumCall(jsonNode, instType)
  1065. result = quote do:
  1066. (
  1067. `getEnumCall`
  1068. )
  1069. of nnkSym:
  1070. let name = normalize($typeName.getTypeImpl())
  1071. case name
  1072. of "string":
  1073. result = quote do:
  1074. (
  1075. verifyJsonKind(`jsonNode`, {JString, JNull}, astToStr(`jsonNode`));
  1076. if `jsonNode`.kind == JNull: "" else: `jsonNode`.str
  1077. )
  1078. of "biggestint":
  1079. result = quote do:
  1080. (
  1081. verifyJsonKind(`jsonNode`, {JInt}, astToStr(`jsonNode`));
  1082. `jsonNode`.num
  1083. )
  1084. of "bool":
  1085. result = quote do:
  1086. (
  1087. verifyJsonKind(`jsonNode`, {JBool}, astToStr(`jsonNode`));
  1088. `jsonNode`.bval
  1089. )
  1090. else:
  1091. if name.startsWith("int") or name.startsWith("uint"):
  1092. result = quote do:
  1093. (
  1094. verifyJsonKind(`jsonNode`, {JInt}, astToStr(`jsonNode`));
  1095. `jsonNode`.num.`obj`
  1096. )
  1097. elif name.startsWith("float"):
  1098. result = quote do:
  1099. (
  1100. verifyJsonKind(`jsonNode`, {JInt, JFloat}, astToStr(`jsonNode`));
  1101. if `jsonNode`.kind == JFloat: `jsonNode`.fnum.`obj` else: `jsonNode`.num.`obj`
  1102. )
  1103. else:
  1104. doAssert false, "Unable to process nnkSym " & $typeName
  1105. else:
  1106. doAssert false, "Unable to process type: " & $obj.kind
  1107. doAssert(not result.isNil(), "processType not initialised.")
  1108. import options
  1109. proc workaroundMacroNone[T](): Option[T] =
  1110. none(T)
  1111. proc depth(n: NimNode, current = 0): int =
  1112. result = 1
  1113. for child in n:
  1114. let d = 1 + child.depth(current + 1)
  1115. if d > result:
  1116. result = d
  1117. proc createConstructor(typeSym, jsonNode: NimNode): NimNode =
  1118. ## Accepts a type description, i.e. "ref Type", "seq[Type]", "Type" etc.
  1119. ##
  1120. ## The ``jsonNode`` refers to the node variable that we are deserialising.
  1121. ##
  1122. ## Returns an object constructor node.
  1123. # echo("--createConsuctor-- \n", treeRepr(typeSym))
  1124. # echo()
  1125. if depth(jsonNode) > 150:
  1126. error("The `to` macro does not support ref objects with cycles.", jsonNode)
  1127. case typeSym.kind
  1128. of nnkBracketExpr:
  1129. var bracketName = ($typeSym[0]).normalize
  1130. case bracketName
  1131. of "option":
  1132. # TODO: Would be good to verify that this is Option[T] from
  1133. # options module I suppose.
  1134. let lenientJsonNode = transformJsonIndexer(jsonNode)
  1135. let optionGeneric = typeSym[1]
  1136. let value = createConstructor(typeSym[1], jsonNode)
  1137. let workaround = bindSym("workaroundMacroNone") # TODO: Nim Bug: This shouldn't be necessary.
  1138. result = quote do:
  1139. (
  1140. if `lenientJsonNode`.isNil or `jsonNode`.kind == JNull: `workaround`[`optionGeneric`]() else: some[`optionGeneric`](`value`)
  1141. )
  1142. of "table", "orderedtable":
  1143. let tableKeyType = typeSym[1]
  1144. if ($tableKeyType).cmpIgnoreStyle("string") != 0:
  1145. error("JSON doesn't support keys of type " & $tableKeyType)
  1146. let tableValueType = typeSym[2]
  1147. let forLoopKey = genSym(nskForVar, "key")
  1148. let indexerNode = createJsonIndexer(jsonNode, forLoopKey)
  1149. let constructorNode = createConstructor(tableValueType, indexerNode)
  1150. let tableInit =
  1151. if bracketName == "table":
  1152. bindSym("initTable")
  1153. else:
  1154. bindSym("initOrderedTable")
  1155. # Create a statement expression containing a for loop.
  1156. result = quote do:
  1157. (
  1158. var map = `tableInit`[`tableKeyType`, `tableValueType`]();
  1159. verifyJsonKind(`jsonNode`, {JObject}, astToStr(`jsonNode`));
  1160. for `forLoopKey` in keys(`jsonNode`.fields): map[`forLoopKey`] = `constructorNode`;
  1161. map
  1162. )
  1163. of "ref":
  1164. # Ref type.
  1165. var typeName = $typeSym[1]
  1166. # Remove the `:ObjectType` suffix.
  1167. if typeName.endsWith(":ObjectType"):
  1168. typeName = typeName[0 .. ^12]
  1169. let obj = getType(typeSym[1])
  1170. result = processType(newIdentNode(typeName), obj, jsonNode, true)
  1171. of "seq":
  1172. let seqT = typeSym[1]
  1173. let forLoopI = genSym(nskForVar, "i")
  1174. let indexerNode = createJsonIndexer(jsonNode, forLoopI)
  1175. let constructorNode = createConstructor(detectDistinctType(seqT), indexerNode)
  1176. # Create a statement expression containing a for loop.
  1177. result = quote do:
  1178. (
  1179. var list: `typeSym` = @[];
  1180. verifyJsonKind(`jsonNode`, {JArray}, astToStr(`jsonNode`));
  1181. for `forLoopI` in 0 ..< `jsonNode`.len: list.add(`constructorNode`);
  1182. list
  1183. )
  1184. of "array":
  1185. let arrayT = typeSym[2]
  1186. let forLoopI = genSym(nskForVar, "i")
  1187. let indexerNode = createJsonIndexer(jsonNode, forLoopI)
  1188. let constructorNode = createConstructor(arrayT, indexerNode)
  1189. # Create a statement expression containing a for loop.
  1190. result = quote do:
  1191. (
  1192. var list: `typeSym`;
  1193. verifyJsonKind(`jsonNode`, {JArray}, astToStr(`jsonNode`));
  1194. for `forLoopI` in 0 ..< `jsonNode`.len: list[`forLoopI`] =`constructorNode`;
  1195. list
  1196. )
  1197. else:
  1198. # Generic type.
  1199. let obj = getType(typeSym)
  1200. result = processType(typeSym, obj, jsonNode, false)
  1201. of nnkSym:
  1202. # Handle JsonNode.
  1203. if ($typeSym).cmpIgnoreStyle("jsonnode") == 0:
  1204. return jsonNode
  1205. # Handle all other types.
  1206. let obj = getType(typeSym)
  1207. let typeNode = getTypeImpl(typeSym)
  1208. if typeNode.typeKind == ntyDistinct:
  1209. result = createConstructor(typeNode, jsonNode)
  1210. elif obj.kind == nnkBracketExpr:
  1211. # When `Sym "Foo"` turns out to be a `ref object`.
  1212. result = createConstructor(obj, jsonNode)
  1213. else:
  1214. result = processType(typeSym, obj, jsonNode, false)
  1215. of nnkTupleTy:
  1216. result = processType(typeSym, typeSym, jsonNode, false)
  1217. of nnkPar, nnkTupleConstr:
  1218. # TODO: The fact that `jsonNode` here works to give a good line number
  1219. # is weird. Specifying typeSym should work but doesn't.
  1220. error("Use a named tuple instead of: " & $toStrLit(typeSym), jsonNode)
  1221. of nnkDistinctTy:
  1222. var baseType = typeSym
  1223. # solve nested distinct types
  1224. while baseType.typeKind == ntyDistinct:
  1225. let impl = getTypeImpl(baseType[0])
  1226. if impl.typeKind != ntyDistinct:
  1227. baseType = baseType[0]
  1228. break
  1229. baseType = impl
  1230. let ret = createConstructor(baseType, jsonNode)
  1231. let typeInst = getTypeInst(typeSym)
  1232. result = quote do:
  1233. (
  1234. `typeInst`(`ret`)
  1235. )
  1236. else:
  1237. doAssert false, "Unable to create constructor for: " & $typeSym.kind
  1238. doAssert(not result.isNil(), "Constructor not initialised.")
  1239. proc postProcess(node: NimNode): NimNode
  1240. proc postProcessValue(value: NimNode): NimNode =
  1241. ## Looks for object constructors and calls the ``postProcess`` procedure
  1242. ## on them. Otherwise it just returns the node as-is.
  1243. case value.kind
  1244. of nnkObjConstr:
  1245. result = postProcess(value)
  1246. else:
  1247. result = value
  1248. for i in 0 ..< len(result):
  1249. result[i] = postProcessValue(result[i])
  1250. proc postProcessExprColonExpr(exprColonExpr, resIdent: NimNode): NimNode =
  1251. ## Transform each field mapping in the ExprColonExpr into a simple
  1252. ## field assignment. Special processing is performed if the field mapping
  1253. ## has an if statement.
  1254. ##
  1255. ## ..code-block::plain
  1256. ## field: (if true: 12) -> if true: `resIdent`.field = 12
  1257. expectKind(exprColonExpr, nnkExprColonExpr)
  1258. let fieldName = exprColonExpr[0]
  1259. let fieldValue = exprColonExpr[1]
  1260. case fieldValue.kind
  1261. of nnkIfStmt:
  1262. doAssert fieldValue.len == 1, "Cannot postProcess two ElifBranches."
  1263. expectKind(fieldValue[0], nnkElifBranch)
  1264. let cond = fieldValue[0][0]
  1265. let bodyValue = postProcessValue(fieldValue[0][1])
  1266. doAssert(bodyValue.kind != nnkNilLit)
  1267. result =
  1268. quote do:
  1269. if `cond`:
  1270. `resIdent`.`fieldName` = `bodyValue`
  1271. else:
  1272. let fieldValue = postProcessValue(fieldValue)
  1273. doAssert(fieldValue.kind != nnkNilLit)
  1274. result =
  1275. quote do:
  1276. `resIdent`.`fieldName` = `fieldValue`
  1277. proc postProcess(node: NimNode): NimNode =
  1278. ## The ``createConstructor`` proc creates a ObjConstr node which contains
  1279. ## if statements for fields that may not be assignable (due to an object
  1280. ## variant). Nim doesn't handle this, but may do in the future.
  1281. ##
  1282. ## For simplicity, we post process the object constructor into multiple
  1283. ## assignments.
  1284. ##
  1285. ## For example:
  1286. ##
  1287. ## ..code-block::plain
  1288. ## Object( (var res = Object();
  1289. ## field: if true: 12 -> if true: res.field = 12;
  1290. ## ) res)
  1291. result = newNimNode(nnkStmtListExpr)
  1292. expectKind(node, nnkObjConstr)
  1293. # Create the type.
  1294. # -> var res = Object()
  1295. var resIdent = genSym(nskVar, "res")
  1296. # TODO: Placing `node[0]` inside quote is buggy
  1297. var resType = toIdentNode(node[0])
  1298. result.add(
  1299. quote do:
  1300. var `resIdent` = `resType`();
  1301. )
  1302. # Process each ExprColonExpr.
  1303. for i in 1..<len(node):
  1304. result.add postProcessExprColonExpr(node[i], resIdent)
  1305. # Return the `res` variable.
  1306. result.add(
  1307. quote do:
  1308. `resIdent`
  1309. )
  1310. macro to*(node: JsonNode, T: typedesc): untyped =
  1311. ## `Unmarshals`:idx: the specified node into the object type specified.
  1312. ##
  1313. ## Known limitations:
  1314. ##
  1315. ## * Heterogeneous arrays are not supported.
  1316. ## * Sets in object variants are not supported.
  1317. ## * Not nil annotations are not supported.
  1318. ##
  1319. ## Example:
  1320. ##
  1321. ## .. code-block:: Nim
  1322. ## let jsonNode = parseJson("""
  1323. ## {
  1324. ## "person": {
  1325. ## "name": "Nimmer",
  1326. ## "age": 21
  1327. ## },
  1328. ## "list": [1, 2, 3, 4]
  1329. ## }
  1330. ## """)
  1331. ##
  1332. ## type
  1333. ## Person = object
  1334. ## name: string
  1335. ## age: int
  1336. ##
  1337. ## Data = object
  1338. ## person: Person
  1339. ## list: seq[int]
  1340. ##
  1341. ## var data = to(jsonNode, Data)
  1342. ## doAssert data.person.name == "Nimmer"
  1343. ## doAssert data.person.age == 21
  1344. ## doAssert data.list == @[1, 2, 3, 4]
  1345. let typeNode = getTypeImpl(T)
  1346. expectKind(typeNode, nnkBracketExpr)
  1347. doAssert(($typeNode[0]).normalize == "typedesc")
  1348. # Create `temp` variable to store the result in case the user calls this
  1349. # on `parseJson` (see bug #6604).
  1350. result = newNimNode(nnkStmtListExpr)
  1351. let temp = genSym(nskLet, "temp")
  1352. result.add quote do:
  1353. let `temp` = `node`
  1354. let constructor = createConstructor(typeNode[1], temp)
  1355. # TODO: Rename postProcessValue and move it (?)
  1356. result.add(postProcessValue(constructor))
  1357. # echo(treeRepr(result))
  1358. # echo(toStrLit(result))
  1359. when false:
  1360. import os
  1361. var s = newFileStream(paramStr(1), fmRead)
  1362. if s == nil: quit("cannot open the file" & paramStr(1))
  1363. var x: JsonParser
  1364. open(x, s, paramStr(1))
  1365. while true:
  1366. next(x)
  1367. case x.kind
  1368. of jsonError:
  1369. Echo(x.errorMsg())
  1370. break
  1371. of jsonEof: break
  1372. of jsonString, jsonInt, jsonFloat: echo(x.str)
  1373. of jsonTrue: echo("!TRUE")
  1374. of jsonFalse: echo("!FALSE")
  1375. of jsonNull: echo("!NULL")
  1376. of jsonObjectStart: echo("{")
  1377. of jsonObjectEnd: echo("}")
  1378. of jsonArrayStart: echo("[")
  1379. of jsonArrayEnd: echo("]")
  1380. close(x)
  1381. # { "json": 5 }
  1382. # To get that we shall use, obj["json"]
  1383. when isMainModule:
  1384. # Note: Macro tests are in tests/stdlib/tjsonmacro.nim
  1385. let testJson = parseJson"""{ "a": [1, 2, 3, 4], "b": "asd", "c": "\ud83c\udf83", "d": "\u00E6"}"""
  1386. # nil passthrough
  1387. doAssert(testJson{"doesnt_exist"}{"anything"}.isNil)
  1388. testJson{["e", "f"]} = %true
  1389. doAssert(testJson["e"]["f"].bval)
  1390. # make sure UTF-16 decoding works.
  1391. doAssert(testJson["c"].str == "🎃")
  1392. doAssert(testJson["d"].str == "æ")
  1393. # make sure no memory leek when parsing invalid string
  1394. let startMemory = getOccupiedMem()
  1395. for i in 0 .. 10000:
  1396. try:
  1397. discard parseJson"""{ invalid"""
  1398. except:
  1399. discard
  1400. # memory diff should less than 4M
  1401. doAssert(abs(getOccupiedMem() - startMemory) < 4 * 1024 * 1024)
  1402. # test `$`
  1403. let stringified = $testJson
  1404. let parsedAgain = parseJson(stringified)
  1405. doAssert(parsedAgain["b"].str == "asd")
  1406. parsedAgain["abc"] = %5
  1407. doAssert parsedAgain["abc"].num == 5
  1408. # Bounds checking
  1409. when compileOption("boundChecks"):
  1410. try:
  1411. let a = testJson["a"][9]
  1412. doAssert(false, "IndexError not thrown")
  1413. except IndexError:
  1414. discard
  1415. try:
  1416. let a = testJson["a"][-1]
  1417. doAssert(false, "IndexError not thrown")
  1418. except IndexError:
  1419. discard
  1420. try:
  1421. doAssert(testJson["a"][0].num == 1, "Index doesn't correspond to its value")
  1422. except:
  1423. doAssert(false, "IndexError thrown for valid index")
  1424. doAssert(testJson{"b"}.getStr()=="asd", "Couldn't fetch a singly nested key with {}")
  1425. doAssert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil")
  1426. doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
  1427. doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
  1428. doAssert(testJson{"a"}==parseJson"[1, 2, 3, 4]", "Didn't return a non-JObject when there was one to be found")
  1429. doAssert(isNil(parseJson("[1, 2, 3]"){"foo"}), "Indexing directly into a list should return nil")
  1430. # Generator:
  1431. var j = %* [{"name": "John", "age": 30}, {"name": "Susan", "age": 31}]
  1432. doAssert j == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
  1433. var j2 = %*
  1434. [
  1435. {
  1436. "name": "John",
  1437. "age": 30
  1438. },
  1439. {
  1440. "name": "Susan",
  1441. "age": 31
  1442. }
  1443. ]
  1444. doAssert j2 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
  1445. var name = "John"
  1446. let herAge = 30
  1447. const hisAge = 31
  1448. var j3 = %*
  1449. [ { "name": "John"
  1450. , "age": herAge
  1451. }
  1452. , { "name": "Susan"
  1453. , "age": hisAge
  1454. }
  1455. ]
  1456. doAssert j3 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
  1457. var j4 = %*{"test": nil}
  1458. doAssert j4 == %{"test": newJNull()}
  1459. let seqOfNodes = @[%1, %2]
  1460. let jSeqOfNodes = %seqOfNodes
  1461. doAssert(jSeqOfNodes[1].num == 2)
  1462. type MyObj = object
  1463. a, b: int
  1464. s: string
  1465. f32: float32
  1466. f64: float64
  1467. next: ref MyObj
  1468. var m: MyObj
  1469. m.s = "hi"
  1470. m.a = 5
  1471. let jMyObj = %m
  1472. doAssert(jMyObj["a"].num == 5)
  1473. doAssert(jMyObj["s"].str == "hi")
  1474. # Test loading of file.
  1475. when not defined(js):
  1476. echo("99% of tests finished. Going to try loading file.")
  1477. var parsed = parseFile("tests/testdata/jsontest.json")
  1478. try:
  1479. discard parsed["key2"][12123]
  1480. doAssert(false)
  1481. except IndexError: doAssert(true)
  1482. var parsed2 = parseFile("tests/testdata/jsontest2.json")
  1483. doAssert(parsed2{"repository", "description"}.str=="IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}")
  1484. doAssert escapeJsonUnquoted("\10Foo🎃barÄ") == "\\nFoo🎃barÄ"
  1485. doAssert escapeJsonUnquoted("\0\7\20") == "\\u0000\\u0007\\u0020" # for #7887
  1486. doAssert escapeJson("\10Foo🎃barÄ") == "\"\\nFoo🎃barÄ\""
  1487. doAssert escapeJson("\0\7\20") == "\"\\u0000\\u0007\\u0020\"" # for #7887
  1488. # Test with extra data
  1489. when not defined(js):
  1490. try:
  1491. discard parseJson("123 456")
  1492. doAssert(false)
  1493. except JsonParsingError:
  1494. doAssert getCurrentExceptionMsg().contains(errorMessages[errEofExpected])
  1495. try:
  1496. discard parseFile("tests/testdata/jsonwithextradata.json")
  1497. doAssert(false)
  1498. except JsonParsingError:
  1499. doAssert getCurrentExceptionMsg().contains(errorMessages[errEofExpected])
  1500. # bug #6438
  1501. doAssert($ %*[] == "[]")
  1502. doAssert($ %*{} == "{}")
  1503. # bug #9111
  1504. block:
  1505. type
  1506. Bar = string
  1507. Foo = object
  1508. a: int
  1509. b: Bar
  1510. let
  1511. js = """{"a": 123, "b": "abc"}""".parseJson
  1512. foo = js.to Foo
  1513. doAssert(foo.b == "abc")