json.nim 45 KB

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