Debian Codegraph @

src/modules/jansson/README

raw
1 JANSSON Module
2
3 Joe Hillenbrand
4
5 <[email protected]>
6
7 Edited by
8
9 Matthew Williams
10
11 <[email protected]>
12
13 Carsten Bock
14
15 <[email protected]>
16
17 Copyright © 2013 Flowroute LLC (flowroute.com)
18 __________________________________________________________________
19
20 Table of Contents
21
22 1. Admin Guide
23
24 1. Overview
25 2. Dependencies
26
27 2.1. Kamailio Modules
28 2.2. External Libraries or Applications
29
30 3. Functions
31
32 3.1. jansson_get(path, src, dst)
33 3.2. jansson_pv_get(key/path, srcvar, dst)
34 3.3. jansson_set(type, key/path, value, result)
35 3.4. jansson_append(type, key/path, value, result)
36 3.5. jansson_array_size(key/path, src, dst)
37 3.6. jansson_xdecode(json, xavp)
38 3.7. jansson_xencode(xavp, pv)
39 3.8. jansson_get_field(field_name, src, dst)
40
41 List of Examples
42
43 1.1. jansson_get usage
44 1.2. jansson_pv_get usage
45 1.3. jansson_set usage
46 1.4. jansson_append usage
47 1.5. jansson_array_size usage
48 1.6. array concatenation
49 1.7. jansson_xdecode usage
50 1.8. jansson_xencode usage
51 1.9. jansson_get_field usage
52
53 Chapter 1. Admin Guide
54
55 Table of Contents
56
57 1. Overview
58 2. Dependencies
59
60 2.1. Kamailio Modules
61 2.2. External Libraries or Applications
62
63 3. Functions
64
65 3.1. jansson_get(path, src, dst)
66 3.2. jansson_pv_get(key/path, srcvar, dst)
67 3.3. jansson_set(type, key/path, value, result)
68 3.4. jansson_append(type, key/path, value, result)
69 3.5. jansson_array_size(key/path, src, dst)
70 3.6. jansson_xdecode(json, xavp)
71 3.7. jansson_xencode(xavp, pv)
72 3.8. jansson_get_field(field_name, src, dst)
73
74 1. Overview
75
76 This module provides operations on JSON strings using JANSSON library.
77 It has support for JSON-PATH operations.
78
79 2. Dependencies
80
81 2.1. Kamailio Modules
82 2.2. External Libraries or Applications
83
84 2.1. Kamailio Modules
85
86 The following modules must be loaded before this module:
87 * None
88
89 2.2. External Libraries or Applications
90
91 The following libraries or applications must be installed before
92 running Kamailio with this module loaded:
93 * jansson (http://www.digip.org/jansson/), tested with: 2.2+
94
95 3. Functions
96
97 3.1. jansson_get(path, src, dst)
98 3.2. jansson_pv_get(key/path, srcvar, dst)
99 3.3. jansson_set(type, key/path, value, result)
100 3.4. jansson_append(type, key/path, value, result)
101 3.5. jansson_array_size(key/path, src, dst)
102 3.6. jansson_xdecode(json, xavp)
103 3.7. jansson_xencode(xavp, pv)
104 3.8. jansson_get_field(field_name, src, dst)
105
106 3.1. jansson_get(path, src, dst)
107
108 Copy the value at the location 'path' from the json object 'src' and
109 store it in variable 'dst'. The path can also be a simple field name (a
110 key), if it does not include any path separator. To retrieve the value
111 of a field that includes path separators in the name, use
112 jansson_get_field().
113
114 The 'src' can be a static string or a dynamic string with variables.
115
116 The path string supports dot delimited notation (e.g. foo.bar.baz),
117 array notation (e.g. [0]), or a combination of the two (e.g.
118 foo.bar[0][1].baz).
119
120 Returns FALSE if the data can not be parsed, TRUE otherwise.
121
122 The function can put a string, integer, null, or new json string into
123 destination. If the path can't be found in the JSON data structure, the
124 pvar is not changed. If it had a previous value, that value remains
125 unchanged.
126
127 Note: For JSON-Integer values exceeding the C-Integer boundaries, a
128 String representing the number is returned.
129
130 Example 1.1. jansson_get usage
131 ...
132 if(!jansson_get("inner.deep.list[3]", "$var(myjson)", "$var(n)")) {
133 xlog("L_ERR", "Can't parse json data");
134 }
135 xlog("L_INFO", "foo is $var(n)");
136 ...
137 jansson_get("test", "{\"test\":\"abc\",\"idx\":20}", "$var(n)");
138 ...
139
140 3.2. jansson_pv_get(key/path, srcvar, dst)
141
142 Similar to jansson_get(), but the 'srcvar' parameter can be only a
143 variable name.
144
145 Example 1.2. jansson_pv_get usage
146 ...
147 if(!jansson_pv_get("inner.deep.list[3]", "$var(myjson)", "$var(n)")) {
148 xlog("L_ERR", "Can't parse json data");
149 }
150 xlog("L_INFO", "foo is $var(n)");
151 ...
152
153 3.3. jansson_set(type, key/path, value, result)
154
155 Insert 'value' as 'type' at location 'path' into 'result'.
156
157 The path string works the same as in jansson_get.
158
159 Valid 'type' parameters are 'integer', 'real', 'string', 'object',
160 'array', 'true', 'false', and 'null' as well as abbreviated names such
161 as 'int', 'str', and 'obj'. 'value' is ignored when type is 'true',
162 'false', or 'null'.
163
164 Note: If you want to insert a JSON-Integer value exceeding the
165 C-Integer boundaries (e.g. C-type long), then the number can be
166 provided as a string.
167
168 Example 1.3. jansson_set usage
169 ...
170 # create a new json object and put a string in it at key "mystr"
171 jansson_set("string", "mystr", "my input string", "$var(myjson)");
172 # $var(myjson) =='{"mystr":"my input string"}'
173
174 # add other values
175 jansson_set("integer", "count", 9000, "$var(myjson)");
176 jansson_set("true", "mybool", 0, "$var(myjson)");
177 jansson_set("real", "pi", "3.14159", "$var(myjson)");
178 # $var(myjson) == '{"mystr":"my input string", "count":9000, "mybool":true, "pi"
179 :3.14159}'
180
181 # add a nested object
182 jansson_set("obj", "myobj", '{"foo":"bar"}', "$var(myjson)");
183 # $var(myjson) =='{"mystr":"my input string", "count":9000, "mybool":true, "pi":
184 3.14159, "myobj":{"foo":"bar"}}'
185
186 # change the nested object
187 jansson_set("str", "myobj.foo", "baz", "$var(myjson)");
188 # $var(myjson) == '{"mystr":"my input string", "count":9000, "mybool":true, "pi"
189 :3.14159, "myobj":{"foo":"baz"}}'
190 ...
191
192 3.4. jansson_append(type, key/path, value, result)
193
194 Like jansson_set but can be used to append to arrays. It can also be
195 used to combine two json objects.
196
197 Note that when appending a json object to another json object, if there
198 is a key that is shared between the two objects, that value will be
199 overwritten by the new object.
200
201 Example 1.4. jansson_append usage
202 ...
203 # create a new json array and append values to it
204 $var(myarray) = '[]';
205 jansson_append("int", "", 0, "$var(myarray)");
206 jansson_append("int", "", 1, "$var(myarray)");
207 jansson_append("int", "", 2, "$var(myarray)");
208 jansson_append("int", "", 3, "$var(myarray)");
209 jansson_append("int", "", 4, "$var(myarray)");
210 # $var(myarray) == '[0,1,2,3,4]'
211
212 # add that array to an object
213 jansson_set("array", "list", $var(myarray), "$var(myjson)");
214 # $var(myjson) == '{"list":[0,1,2,3,4]}'
215
216 # append another value to the list
217 jansson_append("int", "list", 5, "$var(myjson)");
218 # $var(myjson) == '{"list":[0,1,2,3,4,5]}'
219
220 # combining two json objects
221 $var(newobj) = '{"b":2, "c":3}';
222 jansson_append('obj', "", '{"a":1, "b":100}', "$var(newobj)");
223 # $var(newobj) == '{"a":1,"b":100","c":3}';
224 ...
225
226 3.5. jansson_array_size(key/path, src, dst)
227
228 Puts the size of the array in 'src' at location 'path' into the pvar
229 'dst'.
230
231 This is particularly useful for looping through an array. See example.
232
233 Example 1.5. jansson_array_size usage
234 ...
235 $var(array) = "{\"loopme\":[0,1,2,3,4,5]}";
236 $var(count) = 0;
237 jansson_array_size("loopme", $var(array), "$var(size)");
238 while($var(count) < $var(size)) {
239 jansson_get("loopme[$var(count)]", $var(array), "$var(v)");
240 xlog("loopme[$var(count)] == $var(v)\n");
241 $var(count) = $var(count) + 1;
242 }
243 ...
244
245 Example 1.6. array concatenation
246 ...
247 $var(count) = 0;
248 $var(appendme) = '[0,1]';
249 $var(mylist) = '[2,3,4,5]';
250 jansson_array_size("", $var(mylist), "$var(appendme_size)");
251 while($var(count) < $var(appendme_size)) {
252 jansson_get("[$var(count)]", $var(mylist), "$var(tmp)");
253 jansson_append('int', "", $var(tmp), "$var(appendme)");
254 $var(count) = $var(count) + 1;
255 }
256 ...
257
258 3.6. jansson_xdecode(json, xavp)
259
260 Parse a JSON string in 'json' and store the elements in xapv 'xavp'.
261 Top-level JSON must be an object or an array of objects. Nested arrays
262 and objects are not decoded but stored as string.
263
264 Example 1.7. jansson_xdecode usage
265 ...
266 jansson_xdecode('{"foo":"bar"}', "js");
267 xlog("foo is $xavp(js=>foo)");
268 ...
269
270 3.7. jansson_xencode(xavp, pv)
271
272 Encode the items in the xavp 'xavp' as JSON and store the result in a
273 pv. Nested xavp's are not supported.
274
275 Example 1.8. jansson_xencode usage
276 ...
277 $xavp(a=>foo) = "bar";
278 jansson_xencode("a", "$var(js)");
279 # $var(js) = '{"foo":"bar"}'
280 ...
281
282 3.8. jansson_get_field(field_name, src, dst)
283
284 Copy the value of the field 'field_name' from json object 'src' and
285 store it in pvar 'dst'. The field name is not evaluated as JSON path,
286 therefore it has a different behaviour than jansson_get() and can be
287 used when the field name contains path delimiters.
288
289 Note that till version 5.7.x, this function was similar to
290 jansson_get(), after that its behaviour changed to work as described
291 above. Also, the order of parameters changed.
292
293 Example 1.9. jansson_get_field usage
294 ...
295 jansson_get_field("foo", "{'foo':'bar'}", "$var(foo)");
296 xlog("foo is $var(foo)");
297 jansson_get_field("foo.foz", "{'foo.foz':'bar.buz'}", "$var(foofoz)");
298 xlog("foo.foz is $var(foofoz)");
299 ...