json doc: Note about Option and reserved keywords (#13895)
This commit is contained in:
parent
37692baf49
commit
b41931bf67
1 changed files with 7 additions and 0 deletions
|
|
@ -102,16 +102,23 @@
|
||||||
## In addition to reading dynamic data, Nim can also unmarshal JSON directly
|
## In addition to reading dynamic data, Nim can also unmarshal JSON directly
|
||||||
## into a type with the ``to`` macro.
|
## into a type with the ``to`` macro.
|
||||||
##
|
##
|
||||||
|
## Note: Use `Option <options.html#Option>`_ for keys sometimes missing in json
|
||||||
|
## responses, and backticks around keys with a reserved keyword as name.
|
||||||
|
##
|
||||||
## .. code-block:: Nim
|
## .. code-block:: Nim
|
||||||
## import json
|
## import json
|
||||||
|
## import options
|
||||||
##
|
##
|
||||||
## type
|
## type
|
||||||
## User = object
|
## User = object
|
||||||
## name: string
|
## name: string
|
||||||
## age: int
|
## age: int
|
||||||
|
## `type`: Option[string]
|
||||||
##
|
##
|
||||||
## let userJson = parseJson("""{ "name": "Nim", "age": 12 }""")
|
## let userJson = parseJson("""{ "name": "Nim", "age": 12 }""")
|
||||||
## let user = to(userJson, User)
|
## let user = to(userJson, User)
|
||||||
|
## if user.`type`.isSome():
|
||||||
|
## assert user.`type`.get() != "robot"
|
||||||
##
|
##
|
||||||
## Creating JSON
|
## Creating JSON
|
||||||
## =============
|
## =============
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue