json.nim 45 KB

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