json.nim 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  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 hashes, tables, strutils, lexbase, streams, macros, parsejson
  160. import options # xxx remove this dependency using same approach as https://github.com/nim-lang/Nim/pull/14563
  161. import std/private/since
  162. export
  163. tables.`$`
  164. export
  165. parsejson.JsonEventKind, parsejson.JsonError, JsonParser, JsonKindError,
  166. open, close, str, getInt, getFloat, kind, getColumn, getLine, getFilename,
  167. errorMsg, errorMsgExpected, next, JsonParsingError, raiseParseErr, nimIdentNormalize
  168. type
  169. JsonNodeKind* = enum ## possible JSON node types
  170. JNull,
  171. JBool,
  172. JInt,
  173. JFloat,
  174. JString,
  175. JObject,
  176. JArray
  177. JsonNode* = ref JsonNodeObj ## JSON node
  178. JsonNodeObj* {.acyclic.} = object
  179. isUnquoted: bool # the JString was a number-like token and
  180. # so shouldn't be quoted
  181. case kind*: JsonNodeKind
  182. of JString:
  183. str*: string
  184. of JInt:
  185. num*: BiggestInt
  186. of JFloat:
  187. fnum*: float
  188. of JBool:
  189. bval*: bool
  190. of JNull:
  191. nil
  192. of JObject:
  193. fields*: OrderedTable[string, JsonNode]
  194. of JArray:
  195. elems*: seq[JsonNode]
  196. const DepthLimit = 1000
  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. runnableExamples:
  306. assert $(%[NaN, Inf, -Inf, 0.0, -0.0, 1.0, 1e-2]) == """["nan","inf","-inf",0.0,-0.0,1.0,0.01]"""
  307. assert (%NaN).kind == JString
  308. assert (%0.0).kind == JFloat
  309. # for those special cases, we could also have used `newJRawNumber` but then
  310. # it would've been inconsisten with the case of `parseJson` vs `%` for representing them.
  311. if n != n: newJString("nan")
  312. elif n == Inf: newJString("inf")
  313. elif n == -Inf: newJString("-inf")
  314. else: JsonNode(kind: JFloat, fnum: n)
  315. proc `%`*(b: bool): JsonNode =
  316. ## Generic constructor for JSON data. Creates a new `JBool JsonNode`.
  317. result = JsonNode(kind: JBool, bval: b)
  318. proc `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode =
  319. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  320. if keyVals.len == 0: return newJArray()
  321. result = newJObject()
  322. for key, val in items(keyVals): result.fields[key] = val
  323. template `%`*(j: JsonNode): JsonNode = j
  324. proc `%`*[T](elements: openArray[T]): JsonNode =
  325. ## Generic constructor for JSON data. Creates a new `JArray JsonNode`
  326. result = newJArray()
  327. for elem in elements: result.add(%elem)
  328. proc `%`*[T](table: Table[string, T]|OrderedTable[string, T]): JsonNode =
  329. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`.
  330. result = newJObject()
  331. for k, v in table: result[k] = %v
  332. proc `%`*[T](opt: Option[T]): JsonNode =
  333. ## Generic constructor for JSON data. Creates a new `JNull JsonNode`
  334. ## if `opt` is empty, otherwise it delegates to the underlying value.
  335. if opt.isSome: %opt.get else: newJNull()
  336. when false:
  337. # For 'consistency' we could do this, but that only pushes people further
  338. # into that evil comfort zone where they can use Nim without understanding it
  339. # causing problems later on.
  340. proc `%`*(elements: set[bool]): JsonNode =
  341. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`.
  342. ## This can only be used with the empty set `{}` and is supported
  343. ## to prevent the gotcha `%*{}` which used to produce an empty
  344. ## JSON array.
  345. result = newJObject()
  346. assert false notin elements, "usage error: only empty sets allowed"
  347. assert true notin elements, "usage error: only empty sets allowed"
  348. proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) {.inline.} =
  349. ## Sets a field from a `JObject`.
  350. assert(obj.kind == JObject)
  351. obj.fields[key] = val
  352. proc `%`*[T: object](o: T): JsonNode =
  353. ## Construct JsonNode from tuples and objects.
  354. result = newJObject()
  355. for k, v in o.fieldPairs: result[k] = %v
  356. proc `%`*(o: ref object): JsonNode =
  357. ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  358. if o.isNil:
  359. result = newJNull()
  360. else:
  361. result = %(o[])
  362. proc `%`*(o: enum): JsonNode =
  363. ## Construct a JsonNode that represents the specified enum value as a
  364. ## string. Creates a new `JString JsonNode`.
  365. result = %($o)
  366. proc toJsonImpl(x: NimNode): NimNode =
  367. case x.kind
  368. of nnkBracket: # array
  369. if x.len == 0: return newCall(bindSym"newJArray")
  370. result = newNimNode(nnkBracket)
  371. for i in 0 ..< x.len:
  372. result.add(toJsonImpl(x[i]))
  373. result = newCall(bindSym("%", brOpen), result)
  374. of nnkTableConstr: # object
  375. if x.len == 0: return newCall(bindSym"newJObject")
  376. result = newNimNode(nnkTableConstr)
  377. for i in 0 ..< x.len:
  378. x[i].expectKind nnkExprColonExpr
  379. result.add newTree(nnkExprColonExpr, x[i][0], toJsonImpl(x[i][1]))
  380. result = newCall(bindSym("%", brOpen), result)
  381. of nnkCurly: # empty object
  382. x.expectLen(0)
  383. result = newCall(bindSym"newJObject")
  384. of nnkNilLit:
  385. result = newCall(bindSym"newJNull")
  386. of nnkPar:
  387. if x.len == 1: result = toJsonImpl(x[0])
  388. else: result = newCall(bindSym("%", brOpen), x)
  389. else:
  390. result = newCall(bindSym("%", brOpen), x)
  391. macro `%*`*(x: untyped): untyped =
  392. ## Convert an expression to a JsonNode directly, without having to specify
  393. ## `%` for every element.
  394. result = toJsonImpl(x)
  395. proc `==`*(a, b: JsonNode): bool {.noSideEffect.} =
  396. ## Check two nodes for equality
  397. if a.isNil:
  398. if b.isNil: return true
  399. return false
  400. elif b.isNil or a.kind != b.kind:
  401. return false
  402. else:
  403. case a.kind
  404. of JString:
  405. result = a.str == b.str
  406. of JInt:
  407. result = a.num == b.num
  408. of JFloat:
  409. result = a.fnum == b.fnum
  410. of JBool:
  411. result = a.bval == b.bval
  412. of JNull:
  413. result = true
  414. of JArray:
  415. result = a.elems == b.elems
  416. of JObject:
  417. # we cannot use OrderedTable's equality here as
  418. # the order does not matter for equality here.
  419. if a.fields.len != b.fields.len: return false
  420. for key, val in a.fields:
  421. if not b.fields.hasKey(key): return false
  422. when defined(nimHasEffectsOf):
  423. {.noSideEffect.}:
  424. if b.fields[key] != val: return false
  425. else:
  426. if b.fields[key] != val: return false
  427. result = true
  428. proc hash*(n: OrderedTable[string, JsonNode]): Hash {.noSideEffect.}
  429. proc hash*(n: JsonNode): Hash {.noSideEffect.} =
  430. ## Compute the hash for a JSON node
  431. case n.kind
  432. of JArray:
  433. result = hash(n.elems)
  434. of JObject:
  435. result = hash(n.fields)
  436. of JInt:
  437. result = hash(n.num)
  438. of JFloat:
  439. result = hash(n.fnum)
  440. of JBool:
  441. result = hash(n.bval.int)
  442. of JString:
  443. result = hash(n.str)
  444. of JNull:
  445. result = Hash(0)
  446. proc hash*(n: OrderedTable[string, JsonNode]): Hash =
  447. for key, val in n:
  448. result = result xor (hash(key) !& hash(val))
  449. result = !$result
  450. proc len*(n: JsonNode): int =
  451. ## If `n` is a `JArray`, it returns the number of elements.
  452. ## If `n` is a `JObject`, it returns the number of pairs.
  453. ## Else it returns 0.
  454. case n.kind
  455. of JArray: result = n.elems.len
  456. of JObject: result = n.fields.len
  457. else: discard
  458. proc `[]`*(node: JsonNode, name: string): JsonNode {.inline.} =
  459. ## Gets a field from a `JObject`, which must not be nil.
  460. ## If the value at `name` does not exist, raises KeyError.
  461. assert(not isNil(node))
  462. assert(node.kind == JObject)
  463. when defined(nimJsonGet):
  464. if not node.fields.hasKey(name): return nil
  465. result = node.fields[name]
  466. proc `[]`*(node: JsonNode, index: int): JsonNode {.inline.} =
  467. ## Gets the node at `index` in an Array. Result is undefined if `index`
  468. ## is out of bounds, but as long as array bound checks are enabled it will
  469. ## result in an exception.
  470. assert(not isNil(node))
  471. assert(node.kind == JArray)
  472. return node.elems[index]
  473. proc `[]`*(node: JsonNode, index: BackwardsIndex): JsonNode {.inline, since: (1, 5, 1).} =
  474. ## Gets the node at `array.len-i` in an array through the `^` operator.
  475. ##
  476. ## i.e. `j[^i]` is a shortcut for `j[j.len-i]`.
  477. runnableExamples:
  478. let
  479. j = parseJson("[1,2,3,4,5]")
  480. doAssert j[^1].getInt == 5
  481. doAssert j[^2].getInt == 4
  482. `[]`(node, node.len - int(index))
  483. proc `[]`*[U, V](a: JsonNode, x: HSlice[U, V]): JsonNode =
  484. ## Slice operation for JArray.
  485. ##
  486. ## Returns the inclusive range `[a[x.a], a[x.b]]`:
  487. runnableExamples:
  488. import json
  489. let arr = %[0,1,2,3,4,5]
  490. doAssert arr[2..4] == %[2,3,4]
  491. doAssert arr[2..^2] == %[2,3,4]
  492. doAssert arr[^4..^2] == %[2,3,4]
  493. assert(a.kind == JArray)
  494. result = newJArray()
  495. let xa = (when x.a is BackwardsIndex: a.len - int(x.a) else: int(x.a))
  496. let L = (when x.b is BackwardsIndex: a.len - int(x.b) else: int(x.b)) - xa + 1
  497. for i in 0..<L:
  498. result.add(a[i + xa])
  499. proc hasKey*(node: JsonNode, key: string): bool =
  500. ## Checks if `key` exists in `node`.
  501. assert(node.kind == JObject)
  502. result = node.fields.hasKey(key)
  503. proc contains*(node: JsonNode, key: string): bool =
  504. ## Checks if `key` exists in `node`.
  505. assert(node.kind == JObject)
  506. node.fields.hasKey(key)
  507. proc contains*(node: JsonNode, val: JsonNode): bool =
  508. ## Checks if `val` exists in array `node`.
  509. assert(node.kind == JArray)
  510. find(node.elems, val) >= 0
  511. proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =
  512. ## Traverses the node and gets the given value. If any of the
  513. ## keys do not exist, returns `nil`. Also returns `nil` if one of the
  514. ## intermediate data structures is not an object.
  515. ##
  516. ## This proc can be used to create tree structures on the
  517. ## fly (sometimes called `autovivification`:idx:):
  518. ##
  519. runnableExamples:
  520. var myjson = %* {"parent": {"child": {"grandchild": 1}}}
  521. doAssert myjson{"parent", "child", "grandchild"} == newJInt(1)
  522. result = node
  523. for key in keys:
  524. if isNil(result) or result.kind != JObject:
  525. return nil
  526. result = result.fields.getOrDefault(key)
  527. proc `{}`*(node: JsonNode, index: varargs[int]): JsonNode =
  528. ## Traverses the node and gets the given value. If any of the
  529. ## indexes do not exist, returns `nil`. Also returns `nil` if one of the
  530. ## intermediate data structures is not an array.
  531. result = node
  532. for i in index:
  533. if isNil(result) or result.kind != JArray or i >= node.len:
  534. return nil
  535. result = result.elems[i]
  536. proc getOrDefault*(node: JsonNode, key: string): JsonNode =
  537. ## Gets a field from a `node`. If `node` is nil or not an object or
  538. ## value at `key` does not exist, returns nil
  539. if not isNil(node) and node.kind == JObject:
  540. result = node.fields.getOrDefault(key)
  541. proc `{}`*(node: JsonNode, key: string): JsonNode =
  542. ## Gets a field from a `node`. If `node` is nil or not an object or
  543. ## value at `key` does not exist, returns nil
  544. node.getOrDefault(key)
  545. proc `{}=`*(node: JsonNode, keys: varargs[string], value: JsonNode) =
  546. ## Traverses the node and tries to set the value at the given location
  547. ## to `value`. If any of the keys are missing, they are added.
  548. var node = node
  549. for i in 0..(keys.len-2):
  550. if not node.hasKey(keys[i]):
  551. node[keys[i]] = newJObject()
  552. node = node[keys[i]]
  553. node[keys[keys.len-1]] = value
  554. proc delete*(obj: JsonNode, key: string) =
  555. ## Deletes `obj[key]`.
  556. assert(obj.kind == JObject)
  557. if not obj.fields.hasKey(key):
  558. raise newException(KeyError, "key not in object")
  559. obj.fields.del(key)
  560. proc copy*(p: JsonNode): JsonNode =
  561. ## Performs a deep copy of `a`.
  562. case p.kind
  563. of JString:
  564. result = newJString(p.str)
  565. result.isUnquoted = p.isUnquoted
  566. of JInt:
  567. result = newJInt(p.num)
  568. of JFloat:
  569. result = newJFloat(p.fnum)
  570. of JBool:
  571. result = newJBool(p.bval)
  572. of JNull:
  573. result = newJNull()
  574. of JObject:
  575. result = newJObject()
  576. for key, val in pairs(p.fields):
  577. result.fields[key] = copy(val)
  578. of JArray:
  579. result = newJArray()
  580. for i in items(p.elems):
  581. result.elems.add(copy(i))
  582. # ------------- pretty printing ----------------------------------------------
  583. proc indent(s: var string, i: int) =
  584. s.add(spaces(i))
  585. proc newIndent(curr, indent: int, ml: bool): int =
  586. if ml: return curr + indent
  587. else: return indent
  588. proc nl(s: var string, ml: bool) =
  589. s.add(if ml: "\n" else: " ")
  590. proc escapeJsonUnquoted*(s: string; result: var string) =
  591. ## Converts a string `s` to its JSON representation without quotes.
  592. ## Appends to `result`.
  593. for c in s:
  594. case c
  595. of '\L': result.add("\\n")
  596. of '\b': result.add("\\b")
  597. of '\f': result.add("\\f")
  598. of '\t': result.add("\\t")
  599. of '\v': result.add("\\u000b")
  600. of '\r': result.add("\\r")
  601. of '"': result.add("\\\"")
  602. of '\0'..'\7': result.add("\\u000" & $ord(c))
  603. of '\14'..'\31': result.add("\\u00" & toHex(ord(c), 2))
  604. of '\\': result.add("\\\\")
  605. else: result.add(c)
  606. proc escapeJsonUnquoted*(s: string): string =
  607. ## Converts a string `s` to its JSON representation without quotes.
  608. result = newStringOfCap(s.len + s.len shr 3)
  609. escapeJsonUnquoted(s, result)
  610. proc escapeJson*(s: string; result: var string) =
  611. ## Converts a string `s` to its JSON representation with quotes.
  612. ## Appends to `result`.
  613. result.add("\"")
  614. escapeJsonUnquoted(s, result)
  615. result.add("\"")
  616. proc escapeJson*(s: string): string =
  617. ## Converts a string `s` to its JSON representation with quotes.
  618. result = newStringOfCap(s.len + s.len shr 3)
  619. escapeJson(s, result)
  620. proc toUgly*(result: var string, node: JsonNode) =
  621. ## Converts `node` to its JSON Representation, without
  622. ## regard for human readability. Meant to improve `$` string
  623. ## conversion performance.
  624. ##
  625. ## JSON representation is stored in the passed `result`
  626. ##
  627. ## This provides higher efficiency than the `pretty` procedure as it
  628. ## does **not** attempt to format the resulting JSON to make it human readable.
  629. var comma = false
  630. case node.kind:
  631. of JArray:
  632. result.add "["
  633. for child in node.elems:
  634. if comma: result.add ","
  635. else: comma = true
  636. result.toUgly child
  637. result.add "]"
  638. of JObject:
  639. result.add "{"
  640. for key, value in pairs(node.fields):
  641. if comma: result.add ","
  642. else: comma = true
  643. key.escapeJson(result)
  644. result.add ":"
  645. result.toUgly value
  646. result.add "}"
  647. of JString:
  648. if node.isUnquoted:
  649. result.add node.str
  650. else:
  651. escapeJson(node.str, result)
  652. of JInt:
  653. result.addInt(node.num)
  654. of JFloat:
  655. result.addFloat(node.fnum)
  656. of JBool:
  657. result.add(if node.bval: "true" else: "false")
  658. of JNull:
  659. result.add "null"
  660. proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true,
  661. lstArr = false, currIndent = 0) =
  662. case node.kind
  663. of JObject:
  664. if lstArr: result.indent(currIndent) # Indentation
  665. if node.fields.len > 0:
  666. result.add("{")
  667. result.nl(ml) # New line
  668. var i = 0
  669. for key, val in pairs(node.fields):
  670. if i > 0:
  671. result.add(",")
  672. result.nl(ml) # New Line
  673. inc i
  674. # Need to indent more than {
  675. result.indent(newIndent(currIndent, indent, ml))
  676. escapeJson(key, result)
  677. result.add(": ")
  678. toPretty(result, val, indent, ml, false,
  679. newIndent(currIndent, indent, ml))
  680. result.nl(ml)
  681. result.indent(currIndent) # indent the same as {
  682. result.add("}")
  683. else:
  684. result.add("{}")
  685. of JString:
  686. if lstArr: result.indent(currIndent)
  687. toUgly(result, node)
  688. of JInt:
  689. if lstArr: result.indent(currIndent)
  690. result.addInt(node.num)
  691. of JFloat:
  692. if lstArr: result.indent(currIndent)
  693. result.addFloat(node.fnum)
  694. of JBool:
  695. if lstArr: result.indent(currIndent)
  696. result.add(if node.bval: "true" else: "false")
  697. of JArray:
  698. if lstArr: result.indent(currIndent)
  699. if len(node.elems) != 0:
  700. result.add("[")
  701. result.nl(ml)
  702. for i in 0..len(node.elems)-1:
  703. if i > 0:
  704. result.add(",")
  705. result.nl(ml) # New Line
  706. toPretty(result, node.elems[i], indent, ml,
  707. true, newIndent(currIndent, indent, ml))
  708. result.nl(ml)
  709. result.indent(currIndent)
  710. result.add("]")
  711. else: result.add("[]")
  712. of JNull:
  713. if lstArr: result.indent(currIndent)
  714. result.add("null")
  715. proc pretty*(node: JsonNode, indent = 2): string =
  716. ## Returns a JSON Representation of `node`, with indentation and
  717. ## on multiple lines.
  718. ##
  719. ## Similar to prettyprint in Python.
  720. runnableExamples:
  721. let j = %* {"name": "Isaac", "books": ["Robot Dreams"],
  722. "details": {"age": 35, "pi": 3.1415}}
  723. doAssert pretty(j) == """
  724. {
  725. "name": "Isaac",
  726. "books": [
  727. "Robot Dreams"
  728. ],
  729. "details": {
  730. "age": 35,
  731. "pi": 3.1415
  732. }
  733. }"""
  734. result = ""
  735. toPretty(result, node, indent)
  736. proc `$`*(node: JsonNode): string =
  737. ## Converts `node` to its JSON Representation on one line.
  738. result = newStringOfCap(node.len shl 1)
  739. toUgly(result, node)
  740. iterator items*(node: JsonNode): JsonNode =
  741. ## Iterator for the items of `node`. `node` has to be a JArray.
  742. assert node.kind == JArray, ": items() can not iterate a JsonNode of kind " & $node.kind
  743. for i in items(node.elems):
  744. yield i
  745. iterator mitems*(node: var JsonNode): var JsonNode =
  746. ## Iterator for the items of `node`. `node` has to be a JArray. Items can be
  747. ## modified.
  748. assert node.kind == JArray, ": mitems() can not iterate a JsonNode of kind " & $node.kind
  749. for i in mitems(node.elems):
  750. yield i
  751. iterator pairs*(node: JsonNode): tuple[key: string, val: JsonNode] =
  752. ## Iterator for the child elements of `node`. `node` has to be a JObject.
  753. assert node.kind == JObject, ": pairs() can not iterate a JsonNode of kind " & $node.kind
  754. for key, val in pairs(node.fields):
  755. yield (key, val)
  756. iterator keys*(node: JsonNode): string =
  757. ## Iterator for the keys in `node`. `node` has to be a JObject.
  758. assert node.kind == JObject, ": keys() can not iterate a JsonNode of kind " & $node.kind
  759. for key in node.fields.keys:
  760. yield key
  761. iterator mpairs*(node: var JsonNode): tuple[key: string, val: var JsonNode] =
  762. ## Iterator for the child elements of `node`. `node` has to be a JObject.
  763. ## Values can be modified
  764. assert node.kind == JObject, ": mpairs() can not iterate a JsonNode of kind " & $node.kind
  765. for key, val in mpairs(node.fields):
  766. yield (key, val)
  767. proc parseJson(p: var JsonParser; rawIntegers, rawFloats: bool, depth = 0): JsonNode =
  768. ## Parses JSON from a JSON Parser `p`.
  769. case p.tok
  770. of tkString:
  771. # we capture 'p.a' here, so we need to give it a fresh buffer afterwards:
  772. result = newJStringMove(p.a)
  773. p.a = ""
  774. discard getTok(p)
  775. of tkInt:
  776. if rawIntegers:
  777. result = newJRawNumber(p.a)
  778. else:
  779. try:
  780. result = newJInt(parseBiggestInt(p.a))
  781. except ValueError:
  782. result = newJRawNumber(p.a)
  783. discard getTok(p)
  784. of tkFloat:
  785. if rawFloats:
  786. result = newJRawNumber(p.a)
  787. else:
  788. try:
  789. result = newJFloat(parseFloat(p.a))
  790. except ValueError:
  791. result = newJRawNumber(p.a)
  792. discard getTok(p)
  793. of tkTrue:
  794. result = newJBool(true)
  795. discard getTok(p)
  796. of tkFalse:
  797. result = newJBool(false)
  798. discard getTok(p)
  799. of tkNull:
  800. result = newJNull()
  801. discard getTok(p)
  802. of tkCurlyLe:
  803. if depth > DepthLimit:
  804. raiseParseErr(p, "}")
  805. result = newJObject()
  806. discard getTok(p)
  807. while p.tok != tkCurlyRi:
  808. if p.tok != tkString:
  809. raiseParseErr(p, "string literal as key")
  810. var key = p.a
  811. discard getTok(p)
  812. eat(p, tkColon)
  813. var val = parseJson(p, rawIntegers, rawFloats, depth+1)
  814. result[key] = val
  815. if p.tok != tkComma: break
  816. discard getTok(p)
  817. eat(p, tkCurlyRi)
  818. of tkBracketLe:
  819. if depth > DepthLimit:
  820. raiseParseErr(p, "]")
  821. result = newJArray()
  822. discard getTok(p)
  823. while p.tok != tkBracketRi:
  824. result.add(parseJson(p, rawIntegers, rawFloats, depth+1))
  825. if p.tok != tkComma: break
  826. discard getTok(p)
  827. eat(p, tkBracketRi)
  828. of tkError, tkCurlyRi, tkBracketRi, tkColon, tkComma, tkEof:
  829. raiseParseErr(p, "{")
  830. iterator parseJsonFragments*(s: Stream, filename: string = ""; rawIntegers = false, rawFloats = false): JsonNode =
  831. ## Parses from a stream `s` into `JsonNodes`. `filename` is only needed
  832. ## for nice error messages.
  833. ## The JSON fragments are separated by whitespace. This can be substantially
  834. ## faster than the comparable loop
  835. ## `for x in splitWhitespace(s): yield parseJson(x)`.
  836. ## This closes the stream `s` after it's done.
  837. ## If `rawIntegers` is true, integer literals will not be converted to a `JInt`
  838. ## field but kept as raw numbers via `JString`.
  839. ## If `rawFloats` is true, floating point literals will not be converted to a `JFloat`
  840. ## field but kept as raw numbers via `JString`.
  841. var p: JsonParser
  842. p.open(s, filename)
  843. try:
  844. discard getTok(p) # read first token
  845. while p.tok != tkEof:
  846. yield p.parseJson(rawIntegers, rawFloats)
  847. finally:
  848. p.close()
  849. proc parseJson*(s: Stream, filename: string = ""; rawIntegers = false, rawFloats = false): JsonNode =
  850. ## Parses from a stream `s` into a `JsonNode`. `filename` is only needed
  851. ## for nice error messages.
  852. ## If `s` contains extra data, it will raise `JsonParsingError`.
  853. ## This closes the stream `s` after it's done.
  854. ## If `rawIntegers` is true, integer literals will not be converted to a `JInt`
  855. ## field but kept as raw numbers via `JString`.
  856. ## If `rawFloats` is true, floating point literals will not be converted to a `JFloat`
  857. ## field but kept as raw numbers via `JString`.
  858. var p: JsonParser
  859. p.open(s, filename)
  860. try:
  861. discard getTok(p) # read first token
  862. result = p.parseJson(rawIntegers, rawFloats)
  863. eat(p, tkEof) # check if there is no extra data
  864. finally:
  865. p.close()
  866. when defined(js):
  867. from math import `mod`
  868. from std/jsffi import JsObject, `[]`, to
  869. from std/private/jsutils import getProtoName, isInteger, isSafeInteger
  870. proc parseNativeJson(x: cstring): JsObject {.importjs: "JSON.parse(#)".}
  871. proc getVarType(x: JsObject, isRawNumber: var bool): JsonNodeKind =
  872. result = JNull
  873. case $getProtoName(x) # TODO: Implicit returns fail here.
  874. of "[object Array]": return JArray
  875. of "[object Object]": return JObject
  876. of "[object Number]":
  877. if isInteger(x) and 1.0 / cast[float](x) != -Inf: # preserve -0.0 as float
  878. if isSafeInteger(x):
  879. return JInt
  880. else:
  881. isRawNumber = true
  882. return JString
  883. else:
  884. return JFloat
  885. of "[object Boolean]": return JBool
  886. of "[object Null]": return JNull
  887. of "[object String]": return JString
  888. else: assert false
  889. proc len(x: JsObject): int =
  890. asm """
  891. `result` = `x`.length;
  892. """
  893. proc convertObject(x: JsObject): JsonNode =
  894. var isRawNumber = false
  895. case getVarType(x, isRawNumber)
  896. of JArray:
  897. result = newJArray()
  898. for i in 0 ..< x.len:
  899. result.add(x[i].convertObject())
  900. of JObject:
  901. result = newJObject()
  902. asm """for (var property in `x`) {
  903. if (`x`.hasOwnProperty(property)) {
  904. """
  905. var nimProperty: cstring
  906. var nimValue: JsObject
  907. asm "`nimProperty` = property; `nimValue` = `x`[property];"
  908. result[$nimProperty] = nimValue.convertObject()
  909. asm "}}"
  910. of JInt:
  911. result = newJInt(x.to(int))
  912. of JFloat:
  913. result = newJFloat(x.to(float))
  914. of JString:
  915. # Dunno what to do with isUnquoted here
  916. if isRawNumber:
  917. var value: cstring
  918. {.emit: "`value` = `x`.toString();".}
  919. result = newJRawNumber($value)
  920. else:
  921. result = newJString($x.to(cstring))
  922. of JBool:
  923. result = newJBool(x.to(bool))
  924. of JNull:
  925. result = newJNull()
  926. proc parseJson*(buffer: string): JsonNode =
  927. when nimvm:
  928. return parseJson(newStringStream(buffer), "input")
  929. else:
  930. return parseNativeJson(buffer).convertObject()
  931. else:
  932. proc parseJson*(buffer: string; rawIntegers = false, rawFloats = false): JsonNode =
  933. ## Parses JSON from `buffer`.
  934. ## If `buffer` contains extra data, it will raise `JsonParsingError`.
  935. ## If `rawIntegers` is true, integer literals will not be converted to a `JInt`
  936. ## field but kept as raw numbers via `JString`.
  937. ## If `rawFloats` is true, floating point literals will not be converted to a `JFloat`
  938. ## field but kept as raw numbers via `JString`.
  939. result = parseJson(newStringStream(buffer), "input", rawIntegers, rawFloats)
  940. proc parseFile*(filename: string): JsonNode =
  941. ## Parses `file` into a `JsonNode`.
  942. ## If `file` contains extra data, it will raise `JsonParsingError`.
  943. var stream = newFileStream(filename, fmRead)
  944. if stream == nil:
  945. raise newException(IOError, "cannot read from file: " & filename)
  946. result = parseJson(stream, filename, rawIntegers=false, rawFloats=false)
  947. # -- Json deserialiser. --
  948. template verifyJsonKind(node: JsonNode, kinds: set[JsonNodeKind],
  949. ast: string) =
  950. if node == nil:
  951. raise newException(KeyError, "key not found: " & ast)
  952. elif node.kind notin kinds:
  953. let msg = "Incorrect JSON kind. Wanted '$1' in '$2' but got '$3'." % [
  954. $kinds,
  955. ast,
  956. $node.kind
  957. ]
  958. raise newException(JsonKindError, msg)
  959. when defined(nimFixedForwardGeneric):
  960. macro isRefSkipDistinct*(arg: typed): untyped =
  961. ## internal only, do not use
  962. var impl = getTypeImpl(arg)
  963. if impl.kind == nnkBracketExpr and impl[0].eqIdent("typeDesc"):
  964. impl = getTypeImpl(impl[1])
  965. while impl.kind == nnkDistinctTy:
  966. impl = getTypeImpl(impl[0])
  967. result = newLit(impl.kind == nnkRefTy)
  968. # The following forward declarations don't work in older versions of Nim
  969. # forward declare all initFromJson
  970. proc initFromJson(dst: var string; jsonNode: JsonNode; jsonPath: var string)
  971. proc initFromJson(dst: var bool; jsonNode: JsonNode; jsonPath: var string)
  972. proc initFromJson(dst: var JsonNode; jsonNode: JsonNode; jsonPath: var string)
  973. proc initFromJson[T: SomeInteger](dst: var T; jsonNode: JsonNode, jsonPath: var string)
  974. proc initFromJson[T: SomeFloat](dst: var T; jsonNode: JsonNode; jsonPath: var string)
  975. proc initFromJson[T: enum](dst: var T; jsonNode: JsonNode; jsonPath: var string)
  976. proc initFromJson[T](dst: var seq[T]; jsonNode: JsonNode; jsonPath: var string)
  977. proc initFromJson[S, T](dst: var array[S, T]; jsonNode: JsonNode; jsonPath: var string)
  978. proc initFromJson[T](dst: var Table[string, T]; jsonNode: JsonNode; jsonPath: var string)
  979. proc initFromJson[T](dst: var OrderedTable[string, T]; jsonNode: JsonNode; jsonPath: var string)
  980. proc initFromJson[T](dst: var ref T; jsonNode: JsonNode; jsonPath: var string)
  981. proc initFromJson[T](dst: var Option[T]; jsonNode: JsonNode; jsonPath: var string)
  982. proc initFromJson[T: distinct](dst: var T; jsonNode: JsonNode; jsonPath: var string)
  983. proc initFromJson[T: object|tuple](dst: var T; jsonNode: JsonNode; jsonPath: var string)
  984. # initFromJson definitions
  985. proc initFromJson(dst: var string; jsonNode: JsonNode; jsonPath: var string) =
  986. verifyJsonKind(jsonNode, {JString, JNull}, jsonPath)
  987. # since strings don't have a nil state anymore, this mapping of
  988. # JNull to the default string is questionable. `none(string)` and
  989. # `some("")` have the same potentional json value `JNull`.
  990. if jsonNode.kind == JNull:
  991. dst = ""
  992. else:
  993. dst = jsonNode.str
  994. proc initFromJson(dst: var bool; jsonNode: JsonNode; jsonPath: var string) =
  995. verifyJsonKind(jsonNode, {JBool}, jsonPath)
  996. dst = jsonNode.bval
  997. proc initFromJson(dst: var JsonNode; jsonNode: JsonNode; jsonPath: var string) =
  998. if jsonNode == nil:
  999. raise newException(KeyError, "key not found: " & jsonPath)
  1000. dst = jsonNode.copy
  1001. proc initFromJson[T: SomeInteger](dst: var T; jsonNode: JsonNode, jsonPath: var string) =
  1002. when T is uint|uint64 or (not defined(js) and int.sizeof == 4):
  1003. verifyJsonKind(jsonNode, {JInt, JString}, jsonPath)
  1004. case jsonNode.kind
  1005. of JString:
  1006. let x = parseBiggestUInt(jsonNode.str)
  1007. dst = cast[T](x)
  1008. else:
  1009. dst = T(jsonNode.num)
  1010. else:
  1011. verifyJsonKind(jsonNode, {JInt}, jsonPath)
  1012. dst = cast[T](jsonNode.num)
  1013. proc initFromJson[T: SomeFloat](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  1014. verifyJsonKind(jsonNode, {JInt, JFloat, JString}, jsonPath)
  1015. if jsonNode.kind == JString:
  1016. case jsonNode.str
  1017. of "nan":
  1018. let b = NaN
  1019. dst = T(b)
  1020. # dst = NaN # would fail some tests because range conversions would cause CT error
  1021. # in some cases; but this is not a hot-spot inside this branch and backend can optimize this.
  1022. of "inf":
  1023. let b = Inf
  1024. dst = T(b)
  1025. of "-inf":
  1026. let b = -Inf
  1027. dst = T(b)
  1028. else: raise newException(JsonKindError, "expected 'nan|inf|-inf', got " & jsonNode.str)
  1029. else:
  1030. if jsonNode.kind == JFloat:
  1031. dst = T(jsonNode.fnum)
  1032. else:
  1033. dst = T(jsonNode.num)
  1034. proc initFromJson[T: enum](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  1035. verifyJsonKind(jsonNode, {JString}, jsonPath)
  1036. dst = parseEnum[T](jsonNode.getStr)
  1037. proc initFromJson[T](dst: var seq[T]; jsonNode: JsonNode; jsonPath: var string) =
  1038. verifyJsonKind(jsonNode, {JArray}, jsonPath)
  1039. dst.setLen jsonNode.len
  1040. let orignalJsonPathLen = jsonPath.len
  1041. for i in 0 ..< jsonNode.len:
  1042. jsonPath.add '['
  1043. jsonPath.addInt i
  1044. jsonPath.add ']'
  1045. initFromJson(dst[i], jsonNode[i], jsonPath)
  1046. jsonPath.setLen orignalJsonPathLen
  1047. proc initFromJson[S,T](dst: var array[S,T]; jsonNode: JsonNode; jsonPath: var string) =
  1048. verifyJsonKind(jsonNode, {JArray}, jsonPath)
  1049. let originalJsonPathLen = jsonPath.len
  1050. for i in 0 ..< jsonNode.len:
  1051. jsonPath.add '['
  1052. jsonPath.addInt i
  1053. jsonPath.add ']'
  1054. initFromJson(dst[i.S], jsonNode[i], jsonPath) # `.S` for enum indexed arrays
  1055. jsonPath.setLen originalJsonPathLen
  1056. proc initFromJson[T](dst: var Table[string,T]; jsonNode: JsonNode; jsonPath: var string) =
  1057. dst = initTable[string, T]()
  1058. verifyJsonKind(jsonNode, {JObject}, jsonPath)
  1059. let originalJsonPathLen = jsonPath.len
  1060. for key in keys(jsonNode.fields):
  1061. jsonPath.add '.'
  1062. jsonPath.add key
  1063. initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath)
  1064. jsonPath.setLen originalJsonPathLen
  1065. proc initFromJson[T](dst: var OrderedTable[string,T]; jsonNode: JsonNode; jsonPath: var string) =
  1066. dst = initOrderedTable[string,T]()
  1067. verifyJsonKind(jsonNode, {JObject}, jsonPath)
  1068. let originalJsonPathLen = jsonPath.len
  1069. for key in keys(jsonNode.fields):
  1070. jsonPath.add '.'
  1071. jsonPath.add key
  1072. initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath)
  1073. jsonPath.setLen originalJsonPathLen
  1074. proc initFromJson[T](dst: var ref T; jsonNode: JsonNode; jsonPath: var string) =
  1075. verifyJsonKind(jsonNode, {JObject, JNull}, jsonPath)
  1076. if jsonNode.kind == JNull:
  1077. dst = nil
  1078. else:
  1079. dst = new(T)
  1080. initFromJson(dst[], jsonNode, jsonPath)
  1081. proc initFromJson[T](dst: var Option[T]; jsonNode: JsonNode; jsonPath: var string) =
  1082. if jsonNode != nil and jsonNode.kind != JNull:
  1083. when T is ref:
  1084. dst = some(new(T))
  1085. else:
  1086. dst = some(default(T))
  1087. initFromJson(dst.get, jsonNode, jsonPath)
  1088. macro assignDistinctImpl[T: distinct](dst: var T;jsonNode: JsonNode; jsonPath: var string) =
  1089. let typInst = getTypeInst(dst)
  1090. let typImpl = getTypeImpl(dst)
  1091. let baseTyp = typImpl[0]
  1092. result = quote do:
  1093. initFromJson(`baseTyp`(`dst`), `jsonNode`, `jsonPath`)
  1094. proc initFromJson[T: distinct](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  1095. assignDistinctImpl(dst, jsonNode, jsonPath)
  1096. proc detectIncompatibleType(typeExpr, lineinfoNode: NimNode) =
  1097. if typeExpr.kind == nnkTupleConstr:
  1098. error("Use a named tuple instead of: " & typeExpr.repr, lineinfoNode)
  1099. proc foldObjectBody(dst, typeNode, tmpSym, jsonNode, jsonPath, originalJsonPathLen: NimNode) =
  1100. case typeNode.kind
  1101. of nnkEmpty:
  1102. discard
  1103. of nnkRecList, nnkTupleTy:
  1104. for it in typeNode:
  1105. foldObjectBody(dst, it, tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1106. of nnkIdentDefs:
  1107. typeNode.expectLen 3
  1108. let fieldSym = typeNode[0]
  1109. let fieldNameLit = newLit(fieldSym.strVal)
  1110. let fieldPathLit = newLit("." & fieldSym.strVal)
  1111. let fieldType = typeNode[1]
  1112. # Detecting incompatiple tuple types in `assignObjectImpl` only
  1113. # would be much cleaner, but the ast for tuple types does not
  1114. # contain usable type information.
  1115. detectIncompatibleType(fieldType, fieldSym)
  1116. dst.add quote do:
  1117. jsonPath.add `fieldPathLit`
  1118. when nimvm:
  1119. when isRefSkipDistinct(`tmpSym`.`fieldSym`):
  1120. # workaround #12489
  1121. var tmp: `fieldType`
  1122. initFromJson(tmp, getOrDefault(`jsonNode`,`fieldNameLit`), `jsonPath`)
  1123. `tmpSym`.`fieldSym` = tmp
  1124. else:
  1125. initFromJson(`tmpSym`.`fieldSym`, getOrDefault(`jsonNode`,`fieldNameLit`), `jsonPath`)
  1126. else:
  1127. initFromJson(`tmpSym`.`fieldSym`, getOrDefault(`jsonNode`,`fieldNameLit`), `jsonPath`)
  1128. jsonPath.setLen `originalJsonPathLen`
  1129. of nnkRecCase:
  1130. let kindSym = typeNode[0][0]
  1131. let kindNameLit = newLit(kindSym.strVal)
  1132. let kindPathLit = newLit("." & kindSym.strVal)
  1133. let kindType = typeNode[0][1]
  1134. let kindOffsetLit = newLit(uint(getOffset(kindSym)))
  1135. dst.add quote do:
  1136. var kindTmp: `kindType`
  1137. jsonPath.add `kindPathLit`
  1138. initFromJson(kindTmp, `jsonNode`[`kindNameLit`], `jsonPath`)
  1139. jsonPath.setLen `originalJsonPathLen`
  1140. when defined js:
  1141. `tmpSym`.`kindSym` = kindTmp
  1142. else:
  1143. when nimvm:
  1144. `tmpSym`.`kindSym` = kindTmp
  1145. else:
  1146. # fuck it, assign kind field anyway
  1147. ((cast[ptr `kindType`](cast[uint](`tmpSym`.addr) + `kindOffsetLit`))[]) = kindTmp
  1148. dst.add nnkCaseStmt.newTree(nnkDotExpr.newTree(tmpSym, kindSym))
  1149. for i in 1 ..< typeNode.len:
  1150. foldObjectBody(dst, typeNode[i], tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1151. of nnkOfBranch, nnkElse:
  1152. let ofBranch = newNimNode(typeNode.kind)
  1153. for i in 0 ..< typeNode.len-1:
  1154. ofBranch.add copyNimTree(typeNode[i])
  1155. let dstInner = newNimNode(nnkStmtListExpr)
  1156. foldObjectBody(dstInner, typeNode[^1], tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1157. # resOuter now contains the inner stmtList
  1158. ofBranch.add dstInner
  1159. dst[^1].expectKind nnkCaseStmt
  1160. dst[^1].add ofBranch
  1161. of nnkObjectTy:
  1162. typeNode[0].expectKind nnkEmpty
  1163. typeNode[1].expectKind {nnkEmpty, nnkOfInherit}
  1164. if typeNode[1].kind == nnkOfInherit:
  1165. let base = typeNode[1][0]
  1166. var impl = getTypeImpl(base)
  1167. while impl.kind in {nnkRefTy, nnkPtrTy}:
  1168. impl = getTypeImpl(impl[0])
  1169. foldObjectBody(dst, impl, tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1170. let body = typeNode[2]
  1171. foldObjectBody(dst, body, tmpSym, jsonNode, jsonPath, originalJsonPathLen)
  1172. else:
  1173. error("unhandled kind: " & $typeNode.kind, typeNode)
  1174. macro assignObjectImpl[T](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  1175. let typeSym = getTypeInst(dst)
  1176. let originalJsonPathLen = genSym(nskLet, "originalJsonPathLen")
  1177. result = newStmtList()
  1178. result.add quote do:
  1179. let `originalJsonPathLen` = len(`jsonPath`)
  1180. if typeSym.kind in {nnkTupleTy, nnkTupleConstr}:
  1181. # both, `dst` and `typeSym` don't have good lineinfo. But nothing
  1182. # else is available here.
  1183. detectIncompatibleType(typeSym, dst)
  1184. foldObjectBody(result, typeSym, dst, jsonNode, jsonPath, originalJsonPathLen)
  1185. else:
  1186. foldObjectBody(result, typeSym.getTypeImpl, dst, jsonNode, jsonPath, originalJsonPathLen)
  1187. proc initFromJson[T: object|tuple](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
  1188. assignObjectImpl(dst, jsonNode, jsonPath)
  1189. proc to*[T](node: JsonNode, t: typedesc[T]): T =
  1190. ## `Unmarshals`:idx: the specified node into the object type specified.
  1191. ##
  1192. ## Known limitations:
  1193. ##
  1194. ## * Heterogeneous arrays are not supported.
  1195. ## * Sets in object variants are not supported.
  1196. ## * Not nil annotations are not supported.
  1197. ##
  1198. runnableExamples:
  1199. let jsonNode = parseJson("""
  1200. {
  1201. "person": {
  1202. "name": "Nimmer",
  1203. "age": 21
  1204. },
  1205. "list": [1, 2, 3, 4]
  1206. }
  1207. """)
  1208. type
  1209. Person = object
  1210. name: string
  1211. age: int
  1212. Data = object
  1213. person: Person
  1214. list: seq[int]
  1215. var data = to(jsonNode, Data)
  1216. doAssert data.person.name == "Nimmer"
  1217. doAssert data.person.age == 21
  1218. doAssert data.list == @[1, 2, 3, 4]
  1219. var jsonPath = ""
  1220. initFromJson(result, node, jsonPath)
  1221. when false:
  1222. import os
  1223. var s = newFileStream(paramStr(1), fmRead)
  1224. if s == nil: quit("cannot open the file" & paramStr(1))
  1225. var x: JsonParser
  1226. open(x, s, paramStr(1))
  1227. while true:
  1228. next(x)
  1229. case x.kind
  1230. of jsonError:
  1231. Echo(x.errorMsg())
  1232. break
  1233. of jsonEof: break
  1234. of jsonString, jsonInt, jsonFloat: echo(x.str)
  1235. of jsonTrue: echo("!TRUE")
  1236. of jsonFalse: echo("!FALSE")
  1237. of jsonNull: echo("!NULL")
  1238. of jsonObjectStart: echo("{")
  1239. of jsonObjectEnd: echo("}")
  1240. of jsonArrayStart: echo("[")
  1241. of jsonArrayEnd: echo("]")
  1242. close(x)
  1243. # { "json": 5 }
  1244. # To get that we shall use, obj["json"]