comparison xml/en/docs/njs/njs_api.xml @ 2176:95b406f1f347

Added njs JSON API.
author Yaroslav Zhuravlev <yar@nginx.com>
date Tue, 05 Jun 2018 18:23:19 +0300
parents cd4889fdcfa4
children 79297494d291
comparison
equal deleted inserted replaced
2175:cd4889fdcfa4 2176:95b406f1f347
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> 7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
8 8
9 <article name="njs API" 9 <article name="njs API"
10 link="/en/docs/njs/njs_api.html" 10 link="/en/docs/njs/njs_api.html"
11 lang="en" 11 lang="en"
12 rev="1"> 12 rev="2">
13 13
14 <section id="summary"> 14 <section id="summary">
15 15
16 <para> 16 <para>
17 <link doc="../njs_about.xml">njs</link> provides objects, methods and properties 17 <link doc="../njs_about.xml">njs</link> provides objects, methods and properties
18 for extending nginx functionality. 18 for extending nginx functionality.
19 </para> 19 </para>
20
21 </section>
22
23
24 <section id="core" name="Core">
25
26
27 <section id="core_json" name="JSON">
28
29 <para>
30 The <literal>JSON</literal> object (ES 5.1) provides functions
31 to convert njs values to and from JSON format.
32 <list type="tag">
33
34 <tag-name><literal>JSON.parse(<value>string</value>[,
35 <value>reviver</value>])</literal></tag-name>
36 <tag-desc>
37 Converts a <literal>string</literal> that represents JSON data
38 into an njs object (<literal>{...}</literal>) or
39 array (<literal>[...]</literal>).
40 The optional <literal>reviver</literal> parameter is a function (key, value)
41 that will be called for each (key,value) pair and can transform the value.
42 </tag-desc>
43
44 <tag-name><literal>JSON.stringify(<value>value</value>[,
45 <value>replacer</value>] [, <value>space</value>])</literal></tag-name>
46 <tag-desc>
47 Converts an njs object back to JSON.
48 The obligatory <literal>value</literal> parameter is generally a JSON
49 <literal>object</literal> or <literal>array</literal> that will be converted.
50 If the value has a <literal>toJSON()</literal> method,
51 it defines how the object will be serialized.
52 The optional <literal>replacer</literal> parameter is
53 a <literal>function</literal> or <literal>array</literal>
54 that transforms results.
55 The optional <literal>space</literal> parameter is
56 a <literal>string</literal> or <literal>number</literal>.
57 If it is a <literal>number</literal>,
58 it indicates the number of white spaces placed before a result
59 (no more than 10).
60 If it is a <literal>string</literal>,
61 it is used as a white space (or first 10 characters of it).
62 If omitted or is <literal>null</literal>, no white space is used.
63 </tag-desc>
64 </list>
65 </para>
66
67 <para>
68 <example>
69 >> var json = JSON.parse('{"a":1, "b":true}')
70 >> json.a
71 1
72
73 >> JSON.stringify(json)
74 {"a":1,"b":true}
75
76 >> JSON.stringify(json, undefined, 1)
77 {
78 "a": 1,
79 "b": true
80 }
81
82 >> JSON.stringify({ x: [10, undefined, function(){}] })
83 {"x":[10,null,null]}
84
85 >> JSON.stringify({"a":1, "toJSON": function() {return "xxx"}})
86 "xxx"
87
88 # Example with function replacer
89
90 >> function replacer(key, value) {return (typeof value === 'string') ? undefined : value}
91 >>JSON.stringify({a:1, b:"b", c:true}, replacer)
92 {"a":1,"c":true}
93 </example>
94 </para>
95
96 </section>
20 97
21 </section> 98 </section>
22 99
23 100
24 <section id="http" name="HTTP"> 101 <section id="http" name="HTTP">