json.nim 45 KB

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