Merge pull request #1818 from dibdot/lxc_fix
[project/luci.git] / documentation / api / modules / luci.jsonc.parser.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html>
4 <head>
5     <title>Reference</title>
6     <link rel="stylesheet" href="../luadoc.css" type="text/css" />
7         <!--meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/-->
8 </head>
9
10 <body>
11 <div id="container">
12
13 <div id="product">
14         <div id="product_logo"></div>
15         <div id="product_name"><big><b></b></big></div>
16         <div id="product_description"></div>
17 </div> <!-- id="product" -->
18
19 <div id="main">
20
21 <div id="navigation">
22
23
24 <h1>LuaDoc</h1>
25 <ul>
26         
27         <li><a href="../index.html">Index</a></li>
28         
29 </ul>
30
31
32 <!-- Module list -->
33
34 <h1>Modules</h1>
35 <ul>
36
37         <li>
38                 <a href="../modules/luci.dispatcher.html">luci.dispatcher</a>
39         </li>
40
41         <li>
42                 <a href="../modules/luci.http.html">luci.http</a>
43         </li>
44
45         <li>
46                 <a href="../modules/luci.http.conditionals.html">luci.http.conditionals</a>
47         </li>
48
49         <li>
50                 <a href="../modules/luci.http.date.html">luci.http.date</a>
51         </li>
52
53         <li>
54                 <a href="../modules/luci.http.mime.html">luci.http.mime</a>
55         </li>
56
57         <li>
58                 <a href="../modules/luci.i18n.html">luci.i18n</a>
59         </li>
60
61         <li>
62                 <a href="../modules/luci.ip.html">luci.ip</a>
63         </li>
64
65         <li>
66                 <a href="../modules/luci.ip.cidr.html">luci.ip.cidr</a>
67         </li>
68
69         <li>
70                 <a href="../modules/luci.json.html">luci.json</a>
71         </li>
72
73         <li>
74                 <a href="../modules/luci.jsonc.html">luci.jsonc</a>
75         </li>
76
77         <li><strong>luci.jsonc.parser</strong></li>
78         
79         <li>
80                 <a href="../modules/luci.model.ipkg.html">luci.model.ipkg</a>
81         </li>
82
83         <li>
84                 <a href="../modules/luci.model.uci.html">luci.model.uci</a>
85         </li>
86
87         <li>
88                 <a href="../modules/luci.rpcc.html">luci.rpcc</a>
89         </li>
90
91         <li>
92                 <a href="../modules/luci.rpcc.ruci.html">luci.rpcc.ruci</a>
93         </li>
94
95         <li>
96                 <a href="../modules/luci.sys.html">luci.sys</a>
97         </li>
98
99         <li>
100                 <a href="../modules/luci.sys.init.html">luci.sys.init</a>
101         </li>
102
103         <li>
104                 <a href="../modules/luci.sys.iptparser.html">luci.sys.iptparser</a>
105         </li>
106
107         <li>
108                 <a href="../modules/luci.sys.net.html">luci.sys.net</a>
109         </li>
110
111         <li>
112                 <a href="../modules/luci.sys.process.html">luci.sys.process</a>
113         </li>
114
115         <li>
116                 <a href="../modules/luci.sys.user.html">luci.sys.user</a>
117         </li>
118
119         <li>
120                 <a href="../modules/luci.sys.wifi.html">luci.sys.wifi</a>
121         </li>
122
123         <li>
124                 <a href="../modules/luci.util.html">luci.util</a>
125         </li>
126
127         <li>
128                 <a href="../modules/nixio.html">nixio</a>
129         </li>
130
131         <li>
132                 <a href="../modules/nixio.CHANGELOG.html">nixio.CHANGELOG</a>
133         </li>
134
135         <li>
136                 <a href="../modules/nixio.CryptoHash.html">nixio.CryptoHash</a>
137         </li>
138
139         <li>
140                 <a href="../modules/nixio.File.html">nixio.File</a>
141         </li>
142
143         <li>
144                 <a href="../modules/nixio.README.html">nixio.README</a>
145         </li>
146
147         <li>
148                 <a href="../modules/nixio.Socket.html">nixio.Socket</a>
149         </li>
150
151         <li>
152                 <a href="../modules/nixio.TLSContext.html">nixio.TLSContext</a>
153         </li>
154
155         <li>
156                 <a href="../modules/nixio.TLSSocket.html">nixio.TLSSocket</a>
157         </li>
158
159         <li>
160                 <a href="../modules/nixio.UnifiedIO.html">nixio.UnifiedIO</a>
161         </li>
162
163         <li>
164                 <a href="../modules/nixio.bin.html">nixio.bin</a>
165         </li>
166
167         <li>
168                 <a href="../modules/nixio.bit.html">nixio.bit</a>
169         </li>
170
171         <li>
172                 <a href="../modules/nixio.crypto.html">nixio.crypto</a>
173         </li>
174
175         <li>
176                 <a href="../modules/nixio.fs.html">nixio.fs</a>
177         </li>
178
179 </ul>
180
181
182
183 <!-- File list -->
184
185
186
187
188
189
190
191 </div><!-- id="navigation" -->
192
193 <div id="content">
194
195 <h1>Object Instance <code>luci.jsonc.parser</code></h1>
196
197 <p>
198  LuCI JSON parser instance. 
199  A JSON parser instance is useful to parse JSON data chunk by chunk, without 
200  the need to assemble all data in advance.</p>
201
202
203
204
205
206
207
208 <h2>Functions</h2>
209 <table class="function_list">
210
211         <tr>
212         <td class="name" nowrap><a href="#parser.parse">parser:parse</a>&nbsp;(json)</td>
213         <td class="summary">
214  
215 Parses one chunk of JSON data.</td>
216         </tr>
217
218         <tr>
219         <td class="name" nowrap><a href="#parser.get">parser:get</a>&nbsp;()</td>
220         <td class="summary">
221  
222 Convert parsed JSON data into Lua table.</td>
223         </tr>
224
225         <tr>
226         <td class="name" nowrap><a href="#parser.set">parser:set</a>&nbsp;(data)</td>
227         <td class="summary">
228  
229 Put Lua data into the parser.</td>
230         </tr>
231
232         <tr>
233         <td class="name" nowrap><a href="#parser.sink">parser:sink</a>&nbsp;()</td>
234         <td class="summary">
235  
236 Generate an ltn12-compatible sink.</td>
237         </tr>
238
239         <tr>
240         <td class="name" nowrap><a href="#parser.stringify">parser:stringify</a>&nbsp;(pretty)</td>
241         <td class="summary">
242  
243 Serialize current parser state as JSON.</td>
244         </tr>
245
246 </table>
247
248
249
250
251
252
253 <br/>
254 <br/>
255
256
257 <h2><a name="functions"></a>Functions</h2>
258 <dl class="function">
259
260
261
262 <dt><a name="parser.parse"></a><strong>parser:parse</strong>&nbsp;(json)</dt>
263 <dd>
264
265  
266 Parses one chunk of JSON data. 
267
268
269
270 <h3>Parameters</h3>
271 <ul>
272         
273         <li>
274           json: String containing the JSON fragment to parse
275         </li>
276         
277 </ul>
278
279
280
281
282 <h3>Usage:</h3>
283 <pre>parser = luci.jsonc.new() 
284  
285 while true do 
286         chunk = ...  -- fetch a cunk of data, e.g. from a socket 
287         finish, errmsg = <b>parser.parse(chunk)</b> 
288  
289         if finish == nil then 
290                 error("Cannot parse JSON: " .. errmsg) 
291         end 
292  
293         if finish == true then 
294                 break 
295         end 
296 end</pre>
297
298
299
300 <h3>Return value:</h3>
301 <ul> 
302         <li><code>true</code> if a complete JSON object has been parsed and no further input is 
303             expected.</li> 
304         <li><code>false</code> if further input is required</li> 
305         <li><code>nil</code> if an error was encountered while parsing the current chunk. 
306             In this case a string describing the parse error is returned as second 
307             value.</li></ul>
308
309
310
311 <h3>See also:</h3>
312 <ul>
313         
314         <li><a href="#parser.get">
315                 parser:get
316         </a>
317         
318 </ul>
319
320 </dd>
321
322
323
324
325 <dt><a name="parser.get"></a><strong>parser:get</strong>&nbsp;()</dt>
326 <dd>
327
328  
329 Convert parsed JSON data into Lua table. 
330
331
332
333
334
335
336 <h3>Usage:</h3>
337 <pre>parser = luci.jsonc.new() 
338 parser:parse('{ "example": "test" }') 
339  
340 data = parser:get() 
341 print(data.example)  -- "test"</pre>
342
343
344
345 <h3>Return value:</h3>
346 Parsed JSON object converted into a Lua table or <code>nil</code> if the parser 
347         didn't finish or encountered an error.
348
349
350
351 <h3>See also:</h3>
352 <ul>
353         
354         <li><a href="#parser.parse">
355                 parser:parse
356         </a>
357         
358 </ul>
359
360 </dd>
361
362
363
364
365 <dt><a name="parser.set"></a><strong>parser:set</strong>&nbsp;(data)</dt>
366 <dd>
367
368  
369 Put Lua data into the parser. 
370
371
372
373 <h3>Parameters</h3>
374 <ul>
375         
376         <li>
377           data: Lua data to put into the parser object. The data is converted to an 
378         internal JSON representation that can be dumped with <code>stringify()</code>. 
379         The conversion follows the rules described in <code>luci.jsonc.stringify</code>.
380         </li>
381         
382 </ul>
383
384
385
386
387 <h3>Usage:</h3>
388 <pre>parser = luci.jsonc.new() 
389 parser:set({ "some", "data" })</pre>
390
391
392
393 <h3>Return value:</h3>
394 Nothing is returned.
395
396
397
398 <h3>See also:</h3>
399 <ul>
400         
401         <li><a href="#parser.stringify">
402                 parser:stringify
403         </a>
404         
405 </ul>
406
407 </dd>
408
409
410
411
412 <dt><a name="parser.sink"></a><strong>parser:sink</strong>&nbsp;()</dt>
413 <dd>
414
415  
416 Generate an ltn12-compatible sink. 
417
418
419
420
421
422
423 <h3>Usage:</h3>
424 <pre>parser = luci.jsonc.new() 
425 ltn12.pump.all(ltn12.source.file(io.input()), parser:sink()) 
426 print(parser:get())</pre>
427
428
429
430 <h3>Return value:</h3>
431 Returns a function that can be used as an ltn12 sink.
432
433
434
435 </dd>
436
437
438
439
440 <dt><a name="parser.stringify"></a><strong>parser:stringify</strong>&nbsp;(pretty)</dt>
441 <dd>
442
443  
444 Serialize current parser state as JSON. 
445
446
447
448 <h3>Parameters</h3>
449 <ul>
450         
451         <li>
452           pretty: A boolean value indicating whether the resulting JSON should be pretty printed.
453         </li>
454         
455 </ul>
456
457
458
459
460 <h3>Usage:</h3>
461 <pre>parser = luci.jsonc.new() 
462 parser:parse('{ "example": "test" }') 
463 print(parser:serialize())  -- '{"example":"test"}'</pre>
464
465
466
467 <h3>Return value:</h3>
468 Returns the serialized JSON data of this parser instance.
469
470
471
472 </dd>
473
474
475 </dl>
476
477
478
479
480
481 </div> <!-- id="content" -->
482
483 </div> <!-- id="main" -->
484
485 <div id="about">
486         <p><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /></a></p>
487 </div> <!-- id="about" -->
488
489 </div> <!-- id="container" -->
490 </body>
491 </html>