summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorSteve Kellock <steve@kellock.ca>2018-10-02 23:47:21 -0400
committerDominik Picheta <dominikpicheta@googlemail.com>2018-10-02 20:47:21 -0700
commit91c13a0163c692dd709eed1b0cf7eee0c19d0f96 (patch)
tree5c0310fe1fdf3e86a0de13d6bbe7ade63cffa96a /lib
parentafd5b003105cadda78e5a4a8bd9b91ca95ac0972 (diff)
downloadNim-91c13a0163c692dd709eed1b0cf7eee0c19d0f96.tar.gz
[docs/json]: expands existing examples + shows {} operator in action (#9157)
* [docs/json]: expands existing examples + shows {} operator in action

* adds import stmt and fixes butchered syntax in json examples
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/json.nim102
1 files changed, 74 insertions, 28 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index 81d5702c9..389df4087 100644
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -14,28 +14,38 @@
 ## JSON is based on a subset of the JavaScript Programming Language,
 ## Standard ECMA-262 3rd Edition - December 1999.
 ##
-## Dynamically retrieving fields from JSON
-## =======================================
+## Overview
+## ========
 ##
-## This module allows you to access fields in a parsed JSON object in two
-## different ways, one of them is described in this section.
+## Parsing JSON
+## ------------
+##
+## JSON often arrives into your program (via an API or a file) as a ``string``.
+## The first step is to change it from its serialized form into a nested object
+## structure called a ``JsonNode``.
 ##
 ## The ``parseJson`` procedure takes a string containing JSON and returns a
 ## ``JsonNode`` object. This is an object variant and it is either a
 ## ``JObject``, ``JArray``, ``JString``, ``JInt``, ``JFloat``, ``JBool`` or
-## ``JNull``. You
-## check the kind of this object variant by using the ``kind`` accessor.
+## ``JNull``. You check the kind of this object variant by using the ``kind``
+## accessor.
 ##
 ## For a ``JsonNode`` who's kind is ``JObject``, you can acess its fields using
 ## the ``[]`` operator. The following example shows how to do this:
 ##
 ## .. code-block:: Nim
+##   import json
+##
 ##   let jsonNode = parseJson("""{"key": 3.14}""")
+##
 ##   doAssert jsonNode.kind == JObject
 ##   doAssert jsonNode["key"].kind == JFloat
 ##
-## Retrieving the value of a JSON node can then be achieved using one of the
-## helper procedures, which include:
+## Reading values
+## --------------
+##
+## Once you have a ``JsonNode``, retrieving the values can then be achieved
+## by using one of the helper procedures, which include:
 ##
 ## * ``getInt``
 ## * ``getFloat``
@@ -45,43 +55,79 @@
 ## To retrieve the value of ``"key"`` you can do the following:
 ##
 ## .. code-block:: Nim
+##   import json
+##
+##   let jsonNode = parseJson("""{"key": 3.14}""")
+##
 ##   doAssert jsonNode["key"].getFloat() == 3.14
 ##
-## The ``[]`` operator will raise an exception when the specified field does
-## not exist. If you wish to avoid this behaviour you can use the ``{}``
-## operator instead, it will simply return ``nil`` when the field is not found.
-## The ``get``-family of procedures will return a default value when called on
-## ``nil``.
+## **Important:** The ``[]`` operator will raise an exception when the
+## specified field does not exist.
+##
+## Handling optional keys
+## ----------------------
+##
+## By using the ``{}`` operator instead of ``[]``, it will return ``nil``
+## when the field is not found. The ``get``-family of procedures will return a
+## type's default value when called on ``nil``.
+##
+## .. code-block:: Nim
+##   import json
+##
+##   let jsonNode = parseJson("{}")
+##
+##   doAssert jsonNode{"nope"}.getInt() == 0
+##   doAssert jsonNode{"nope"}.getFloat() == 0
+##   doAssert jsonNode{"nope"}.getStr() == ""
+##   doAssert jsonNode{"nope"}.getBool() == false
+##
+## Using default values
+## --------------------
+##
+## The ``get``-family helpers also accept an additional parameter which allow
+## you to fallback to a default value should the key's values be ``null``:
+##
+## .. code-block:: Nim
+##   import json
 ##
-## Unmarshalling JSON into a type
-## ==============================
+##   let jsonNode = parseJson("""{"key": 3.14, "key2": null}""")
+##
+##   doAssert jsonNode["key"].getFloat(6.28) == 3.14
+##   doAssert jsonNode["key2"].getFloat(3.14) == 3.14
+##   doAssert jsonNode{"nope"}.getFloat(3.14) == 3.14 # note the {}
+##
+## Unmarshalling
+## -------------
+##
+## In addition to reading dynamic data, Nim can also unmarshall JSON directly
+## into a type with the ``to`` macro.
+##
+## .. code-block:: Nim
+##   import json
 ##
-## This module allows you to access fields in a parsed JSON object in two
-## different ways, one of them is described in this section.
+##   type
+##     User = object
+##       name: string
+##       age: int
 ##
-## This is done using the ``to`` macro. Take a look at
-## `its documentation <#to.m,JsonNode,typedesc>`_ to see an example of its use.
+##   let userJson = parseJson("""{ "name": "Nim", "age": 12 }""")
+##   let user = to(userJson, User)
 ##
 ## Creating JSON
 ## =============
 ##
-## This module can also be used to comfortably create JSON using the `%*`
+## This module can also be used to comfortably create JSON using the ``%*``
 ## operator:
 ##
 ## .. code-block:: nim
+##   import json
 ##
 ##   var hisName = "John"
 ##   let herAge = 31
 ##   var j = %*
 ##     [
-##       {
-##         "name": hisName,
-##         "age": 30
-##       },
-##       {
-##         "name": "Susan",
-##         "age": herAge
-##       }
+##       { "name": hisName, "age": 30 },
+##       { "name": "Susan", "age": herAge }
 ##     ]
 ##
 ##    var j2 = %* {"name": "Isaac", "books": ["Robot Dreams"]}
m> 2015-02-19 16:46:47 -0800 788 - reorg, showing off how tangle makes it' href='/akkartik/mu/commit/cpp/013arithmetic?h=main&id=8d7839b99723553c08b33c30980a001e7a3557b6'>8d7839b9 ^
5497090a ^


8d7839b9 ^
6f5d7864 ^
8d7839b9 ^



0487a30e ^
57699011 ^
8d7839b9 ^
0487a30e ^

5497090a ^
0487a30e ^


1848b18f ^

8d7839b9 ^
42b31beb ^
8d7839b9 ^
42b31beb ^
0487a30e ^
fca0ebbe ^
0487a30e ^

827898fc ^
0487a30e ^

827898fc ^
42b31beb ^

8d7839b9 ^
88be3dbc ^
8d7839b9 ^
5497090a ^
8d7839b9 ^
6f5d7864 ^
8d7839b9 ^

0487a30e ^
57699011 ^
8d7839b9 ^
88be3dbc ^
8d7839b9 ^
5497090a ^


8d7839b9 ^
6f5d7864 ^
8d7839b9 ^



0487a30e ^
57699011 ^
8d7839b9 ^
0487a30e ^

5497090a ^
0487a30e ^


1848b18f ^

8d7839b9 ^
42b31beb ^
8d7839b9 ^
42b31beb ^
fca0ebbe ^
0487a30e ^

827898fc ^
0487a30e ^

827898fc ^
42b31beb ^

8d7839b9 ^
88be3dbc ^
8d7839b9 ^
5497090a ^
8d7839b9 ^
6f5d7864 ^
8d7839b9 ^

0487a30e ^
57699011 ^
8d7839b9 ^
88be3dbc ^
8d7839b9 ^
5497090a ^


8d7839b9 ^
6f5d7864 ^
8d7839b9 ^



0487a30e ^
57699011 ^
8d7839b9 ^
0487a30e ^

5497090a ^
0487a30e ^


1848b18f ^

8d7839b9 ^
42b31beb ^
8d7839b9 ^
42b31beb ^
0487a30e ^
fca0ebbe ^
0487a30e ^

827898fc ^
0487a30e ^

827898fc ^
42b31beb ^

8d7839b9 ^
88be3dbc ^
8d7839b9 ^
5497090a ^
8d7839b9 ^
6f5d7864 ^
8d7839b9 ^

0487a30e ^
57699011 ^
8d7839b9 ^
88be3dbc ^
8d7839b9 ^
5497090a ^


8d7839b9 ^
6f5d7864 ^
8d7839b9 ^



0487a30e ^
57699011 ^
8d7839b9 ^
0487a30e ^

5497090a ^
0487a30e ^


5497090a ^

1848b18f ^

8d7839b9 ^
0b82eef7 ^
8d7839b9 ^
42b31beb ^
827898fc ^
fca0ebbe ^
0487a30e ^
5497090a ^
827898fc ^

42b31beb ^

8d7839b9 ^
88be3dbc ^
8d7839b9 ^
5497090a ^
8d7839b9 ^
6f5d7864 ^
8d7839b9 ^

0487a30e ^
57699011 ^
0487a30e ^
57699011 ^
8d7839b9 ^
88be3dbc ^
8d7839b9 ^
5497090a ^


8d7839b9 ^
6f5d7864 ^
8d7839b9 ^



0487a30e ^
57699011 ^
0487a30e ^
57699011 ^
fca0ebbe ^



5497090a ^
fca0ebbe ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239