json.nim 45 KB

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