Debian Codegraph @

Xpjproject/pjsip/src/pjsua2/json.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pjsua2/json.hpp>
19 #include <pjlib-util/errno.h>
20 #include <pj/file_io.h>
21 #include "util.hpp"
22
23 #define THIS_FILE "json.cpp"
24
25 using namespace pj;
26 using namespace std;
27
28 /* Json node operations */
29 static bool jsonNode_hasUnread(const ContainerNode*);
30 static string jsonNode_unreadName(const ContainerNode*n)
31 PJSUA2_THROW(Error);
32 static float jsonNode_readNumber(const ContainerNode*,
33 const string&)
34 PJSUA2_THROW(Error);
35 static bool jsonNode_readBool(const ContainerNode*,
36 const string&)
37 PJSUA2_THROW(Error);
38 static string jsonNode_readString(const ContainerNode*,
39 const string&)
40 PJSUA2_THROW(Error);
41 static StringVector jsonNode_readStringVector(const ContainerNode*,
42 const string&)
43 PJSUA2_THROW(Error);
44 static ContainerNode jsonNode_readContainer(const ContainerNode*,
45 const string &)
46 PJSUA2_THROW(Error);
47 static ContainerNode jsonNode_readArray(const ContainerNode*,
48 const string &)
49 PJSUA2_THROW(Error);
50 static void jsonNode_writeNumber(ContainerNode*,
51 const string &name,
52 float num)
53 PJSUA2_THROW(Error);
54 static void jsonNode_writeBool(ContainerNode*,
55 const string &name,
56 bool value)
57 PJSUA2_THROW(Error);
58 static void jsonNode_writeString(ContainerNode*,
59 const string &name,
60 const string &value)
61 PJSUA2_THROW(Error);
62 static void jsonNode_writeStringVector(ContainerNode*,
63 const string &name,
64 const StringVector &value)
65 PJSUA2_THROW(Error);
66 static ContainerNode jsonNode_writeNewContainer(ContainerNode*,
67 const string &name)
68 PJSUA2_THROW(Error);
69 static ContainerNode jsonNode_writeNewArray(ContainerNode*,
70 const string &name)
71 PJSUA2_THROW(Error);
72
73 static container_node_op json_op =
74 {
75 &jsonNode_hasUnread,
76 &jsonNode_unreadName,
77 &jsonNode_readNumber,
78 &jsonNode_readBool,
79 &jsonNode_readString,
80 &jsonNode_readStringVector,
81 &jsonNode_readContainer,
82 &jsonNode_readArray,
83 &jsonNode_writeNumber,
84 &jsonNode_writeBool,
85 &jsonNode_writeString,
86 &jsonNode_writeStringVector,
87 &jsonNode_writeNewContainer,
88 &jsonNode_writeNewArray
89 };
90
91 ///////////////////////////////////////////////////////////////////////////////
92 JsonDocument::JsonDocument()
93 : root(NULL)
94 {
95 pj_caching_pool_init(&cp, NULL, 0);
96 pool = pj_pool_create(&cp.factory, "jsondoc", 512, 512, NULL);
97 if (!pool)
98 PJSUA2_RAISE_ERROR(PJ_ENOMEM);
99 }
100
101 JsonDocument::~JsonDocument()
102 {
103 if (pool)
104 pj_pool_release(pool);
105 pj_caching_pool_destroy(&cp);
106 }
107
108 void JsonDocument::initRoot() const
109 {
110 rootNode.op = &json_op;
111 rootNode.data.doc = (void*)this;
112 rootNode.data.data1 = (void*)root;
113 rootNode.data.data2 = root->value.children.next;
114 }
115
116 void JsonDocument::loadFile(const string &filename) PJSUA2_THROW(Error)
117 {
118 if (root)
119 PJSUA2_RAISE_ERROR3(PJ_EINVALIDOP, "JsonDocument.loadString()",
120 "Document already initialized");
121
122 if (!pj_file_exists(filename.c_str()))
123 PJSUA2_RAISE_ERROR(PJ_ENOTFOUND);
124
125 pj_ssize_t size = (pj_ssize_t)pj_file_size(filename.c_str());
126 if (size <= 0)
127 PJSUA2_RAISE_ERROR(PJ_ETOOSMALL);
128 pj_status_t status;
129
130 char *buffer = (char*)pj_pool_alloc(pool, size+1);
131 pj_oshandle_t fd = 0;
132 unsigned parse_size;
133 char err_msg[120];
134 pj_json_err_info err_info;
135
136 err_msg[0] = '\0';
137
138 status = pj_file_open(pool, filename.c_str(), PJ_O_RDONLY, &fd);
139 if (status != PJ_SUCCESS)
140 goto on_error;
141
142 status = pj_file_read(fd, buffer, &size);
143 if (status != PJ_SUCCESS)
144 goto on_error;
145
146 pj_file_close(fd);
147 fd = NULL;
148
149 if (size <= 0) {
150 status = PJ_EEOF;
151 goto on_error;
152 }
153
154 parse_size = (unsigned)size;
155 root = pj_json_parse(pool, buffer, &parse_size, &err_info);
156 if (root == NULL) {
157 pj_ansi_snprintf(err_msg, sizeof(err_msg),
158 "JSON parsing failed: syntax error in file '%s' at "
159 "line %d column %d",
160 filename.c_str(), err_info.line, err_info.col);
161 PJ_LOG(1,(THIS_FILE, "%s", err_msg));
162 status = PJLIB_UTIL_EINJSON;
163 goto on_error;
164 }
165
166 initRoot();
167 return;
168
169 on_error:
170 if (fd)
171 pj_file_close(fd);
172 if (err_msg[0])
173 PJSUA2_RAISE_ERROR3(status, "loadFile()", err_msg);
174 else
175 PJSUA2_RAISE_ERROR(status);
176 }
177
178 void JsonDocument::loadString(const string &input) PJSUA2_THROW(Error)
179 {
180 if (root)
181 PJSUA2_RAISE_ERROR3(PJ_EINVALIDOP, "JsonDocument.loadString()",
182 "Document already initialized");
183
184 unsigned size = (unsigned)input.size();
185 char *buffer = (char*)pj_pool_alloc(pool, size+1);
186 unsigned parse_size = (unsigned)size;
187 pj_json_err_info err_info;
188
189 pj_memcpy(buffer, input.c_str(), size);
190
191 root = pj_json_parse(pool, buffer, &parse_size, &err_info);
192 if (root == NULL) {
193 char err_msg[80];
194
195 pj_ansi_snprintf(err_msg, sizeof(err_msg),
196 "JSON parsing failed at line %d column %d",
197 err_info.line, err_info.col);
198 PJ_LOG(1,(THIS_FILE, "%s", err_msg));
199 PJSUA2_RAISE_ERROR3(PJLIB_UTIL_EINJSON, "loadString()", err_msg);
200 }
201 initRoot();
202 }
203
204 struct save_file_data
205 {
206 pj_oshandle_t fd;
207 };
208
209 static pj_status_t json_file_writer(const char *s,
210 unsigned size,
211 void *user_data)
212 {
213 save_file_data *sd = (save_file_data*)user_data;
214 pj_ssize_t ssize = (pj_ssize_t)size;
215 return pj_file_write(sd->fd, s, &ssize);
216 }
217
218 void JsonDocument::saveFile(const string &filename) PJSUA2_THROW(Error)
219 {
220 struct save_file_data sd;
221 pj_status_t status;
222
223 /* Make sure root container has been created */
224 getRootContainer();
225
226 status = pj_file_open(pool, filename.c_str(), PJ_O_WRONLY, &sd.fd);
227 if (status != PJ_SUCCESS)
228 PJSUA2_RAISE_ERROR(status);
229
230 status = pj_json_writef(root, &json_file_writer, &sd.fd);
231 pj_file_close(sd.fd);
232
233 if (status != PJ_SUCCESS)
234 PJSUA2_RAISE_ERROR(status);
235 }
236
237 struct save_string_data
238 {
239 string output;
240 };
241
242 static pj_status_t json_string_writer(const char *s,
243 unsigned size,
244 void *user_data)
245 {
246 save_string_data *sd = (save_string_data*)user_data;
247 sd->output.append(s, size);
248 return PJ_SUCCESS;
249 }
250
251 string JsonDocument::saveString() PJSUA2_THROW(Error)
252 {
253 struct save_string_data sd;
254 pj_status_t status;
255
256 /* Make sure root container has been created */
257 getRootContainer();
258
259 status = pj_json_writef(root, &json_string_writer, &sd);
260 if (status != PJ_SUCCESS)
261 PJSUA2_RAISE_ERROR(status);
262
263 return sd.output;
264 }
265
266 ContainerNode & JsonDocument::getRootContainer() const
267 {
268 if (!root) {
269 root = allocElement();
270 pj_json_elem_obj(root, NULL);
271 initRoot();
272 }
273
274 return rootNode;
275 }
276
277 pj_json_elem* JsonDocument::allocElement() const
278 {
279 return (pj_json_elem*)pj_pool_alloc(pool, sizeof(pj_json_elem));
280 }
281
282 pj_pool_t *JsonDocument::getPool()
283 {
284 return pool;
285 }
286
287 ///////////////////////////////////////////////////////////////////////////////
288 struct json_node_data
289 {
290 JsonDocument *doc;
291 pj_json_elem *jnode;
292 pj_json_elem *childPtr;
293 };
294
295 static bool jsonNode_hasUnread(const ContainerNode *node)
296 {
297 json_node_data *jdat = (json_node_data*)&node->data;
298 return jdat->childPtr != (pj_json_elem*)&jdat->jnode->value.children;
299 }
300
301 static void json_verify(struct json_node_data *jdat,
302 const char *op,
303 const string &name,
304 pj_json_val_type type)
305 {
306 if (jdat->childPtr == (pj_json_elem*)&jdat->jnode->value.children)
307 PJSUA2_RAISE_ERROR3(PJ_EEOF, op, "No unread element");
308
309 /* If name is specified, then check if the names match, except
310 * when the node name itself is empty and the parent node is
311 * an array, then ignore the checking (JSON doesn't allow array
312 * elements to have name).
313 */
314 if (jdat->jnode->type != PJ_JSON_VAL_ARRAY &&
315 name.size() && jdat->childPtr->name.slen &&
316 name.compare(0, name.size(), jdat->childPtr->name.ptr,
317 jdat->childPtr->name.slen))
318 {
319 char err_msg[80];
320 pj_ansi_snprintf(err_msg, sizeof(err_msg),
321 "Name mismatch: expecting '%s' got '%.*s'",
322 name.c_str(), (int)jdat->childPtr->name.slen,
323 jdat->childPtr->name.ptr);
324 PJSUA2_RAISE_ERROR3(PJLIB_UTIL_EINJSON, op, err_msg);
325 }
326
327 if (type != PJ_JSON_VAL_NULL && jdat->childPtr->type != type) {
328 char err_msg[80];
329 pj_ansi_snprintf(err_msg, sizeof(err_msg),
330 "Type mismatch: expecting %d got %d",
331 type, jdat->childPtr->type);
332 PJSUA2_RAISE_ERROR3(PJLIB_UTIL_EINJSON, op, err_msg);
333 }
334 }
335
336 static string jsonNode_unreadName(const ContainerNode *node)
337 PJSUA2_THROW(Error)
338 {
339 json_node_data *jdat = (json_node_data*)&node->data;
340 json_verify(jdat, "unreadName()", "", PJ_JSON_VAL_NULL);
341 return pj2Str(jdat->childPtr->name);
342 }
343
344 static float jsonNode_readNumber(const ContainerNode *node,
345 const string &name)
346 PJSUA2_THROW(Error)
347 {
348 json_node_data *jdat = (json_node_data*)&node->data;
349 json_verify(jdat, "readNumber()", name, PJ_JSON_VAL_NUMBER);
350 jdat->childPtr = jdat->childPtr->next;
351 return jdat->childPtr->prev->value.num;
352 }
353
354 static bool jsonNode_readBool(const ContainerNode *node,
355 const string &name)
356 PJSUA2_THROW(Error)
357 {
358 json_node_data *jdat = (json_node_data*)&node->data;
359 json_verify(jdat, "readBool()", name, PJ_JSON_VAL_BOOL);
360 jdat->childPtr = jdat->childPtr->next;
361 return PJ2BOOL(jdat->childPtr->prev->value.is_true);
362 }
363
364 static string jsonNode_readString(const ContainerNode *node,
365 const string &name)
366 PJSUA2_THROW(Error)
367 {
368 json_node_data *jdat = (json_node_data*)&node->data;
369 json_verify(jdat, "readString()", name, PJ_JSON_VAL_STRING);
370 jdat->childPtr = jdat->childPtr->next;
371 return pj2Str(jdat->childPtr->prev->value.str);
372 }
373
374 static StringVector jsonNode_readStringVector(const ContainerNode *node,
375 const string &name)
376 PJSUA2_THROW(Error)
377 {
378 json_node_data *jdat = (json_node_data*)&node->data;
379 json_verify(jdat, "readStringVector()", name, PJ_JSON_VAL_ARRAY);
380
381 StringVector result;
382 pj_json_elem *child = jdat->childPtr->value.children.next;
383 while (child != (pj_json_elem*)&jdat->childPtr->value.children) {
384 if (child->type != PJ_JSON_VAL_STRING) {
385 char err_msg[80];
386 pj_ansi_snprintf(err_msg, sizeof(err_msg),
387 "Elements not string but type %d",
388 child->type);
389 PJSUA2_RAISE_ERROR3(PJLIB_UTIL_EINJSON, "readStringVector()",
390 err_msg);
391 }
392 result.push_back(pj2Str(child->value.str));
393 child = child->next;
394 }
395
396 jdat->childPtr = jdat->childPtr->next;
397 return result;
398 }
399
400 static ContainerNode jsonNode_readContainer(const ContainerNode *node,
401 const string &name)
402 PJSUA2_THROW(Error)
403 {
404 json_node_data *jdat = (json_node_data*)&node->data;
405 json_verify(jdat, "readContainer()", name, PJ_JSON_VAL_OBJ);
406
407 ContainerNode json_node;
408
409 json_node.op = &json_op;
410 json_node.data.doc = (void*)jdat->doc;
411 json_node.data.data1 = (void*)jdat->childPtr;
412 json_node.data.data2 = (void*)jdat->childPtr->value.children.next;
413
414 jdat->childPtr = jdat->childPtr->next;
415 return json_node;
416 }
417
418 static ContainerNode jsonNode_readArray(const ContainerNode *node,
419 const string &name)
420 PJSUA2_THROW(Error)
421 {
422 json_node_data *jdat = (json_node_data*)&node->data;
423 json_verify(jdat, "readArray()", name, PJ_JSON_VAL_ARRAY);
424
425 ContainerNode json_node;
426
427 json_node.op = &json_op;
428 json_node.data.doc = (void*)jdat->doc;
429 json_node.data.data1 = (void*)jdat->childPtr;
430 json_node.data.data2 = (void*)jdat->childPtr->value.children.next;
431
432 jdat->childPtr = jdat->childPtr->next;
433 return json_node;
434 }
435
436 static pj_str_t alloc_name(JsonDocument *doc, const string &name)
437 {
438 pj_str_t new_name;
439 pj_strdup2(doc->getPool(), &new_name, name.c_str());
440 return new_name;
441 }
442
443 static void jsonNode_writeNumber(ContainerNode *node,
444 const string &name,
445 float num)
446 PJSUA2_THROW(Error)
447 {
448 json_node_data *jdat = (json_node_data*)&node->data;
449 pj_json_elem *el = jdat->doc->allocElement();
450 pj_str_t nm = alloc_name(jdat->doc, name);
451 pj_json_elem_number(el, &nm, num);
452 pj_json_elem_add(jdat->jnode, el);
453 }
454
455 static void jsonNode_writeBool(ContainerNode *node,
456 const string &name,
457 bool value)
458 PJSUA2_THROW(Error)
459 {
460 json_node_data *jdat = (json_node_data*)&node->data;
461 pj_json_elem *el = jdat->doc->allocElement();
462 pj_str_t nm = alloc_name(jdat->doc, name);
463 pj_json_elem_bool(el, &nm, value);
464 pj_json_elem_add(jdat->jnode, el);
465 }
466
467 static void jsonNode_writeString(ContainerNode *node,
468 const string &name,
469 const string &value)
470 PJSUA2_THROW(Error)
471 {
472 json_node_data *jdat = (json_node_data*)&node->data;
473 pj_json_elem *el = jdat->doc->allocElement();
474 pj_str_t nm = alloc_name(jdat->doc, name);
475 pj_str_t new_val;
476 pj_str_t str_val;
477 pj_strset(&str_val, (char*)value.data(), value.size());
478 pj_strdup(jdat->doc->getPool(), &new_val, &str_val);
479 pj_json_elem_string(el, &nm, &new_val);
480
481 pj_json_elem_add(jdat->jnode, el);
482 }
483
484 static void jsonNode_writeStringVector(ContainerNode *node,
485 const string &name,
486 const StringVector &value)
487 PJSUA2_THROW(Error)
488 {
489 json_node_data *jdat = (json_node_data*)&node->data;
490 pj_json_elem *el = jdat->doc->allocElement();
491 pj_str_t nm = alloc_name(jdat->doc, name);
492
493 pj_json_elem_array(el, &nm);
494 for (unsigned i=0; i<value.size(); ++i) {
495 pj_str_t new_val;
496 pj_str_t str_val;
497
498 pj_strset(&str_val, (char*)value[i].data(), value[i].size());
499 pj_strdup(jdat->doc->getPool(), &new_val, &str_val);
500 pj_json_elem *child = jdat->doc->allocElement();
501 pj_json_elem_string(child, NULL, &new_val);
502 pj_json_elem_add(el, child);
503 }
504
505 pj_json_elem_add(jdat->jnode, el);
506 }
507
508 static ContainerNode jsonNode_writeNewContainer(ContainerNode *node,
509 const string &name)
510 PJSUA2_THROW(Error)
511 {
512 json_node_data *jdat = (json_node_data*)&node->data;
513 pj_json_elem *el = jdat->doc->allocElement();
514 pj_str_t nm = alloc_name(jdat->doc, name);
515
516 pj_json_elem_obj(el, &nm);
517 pj_json_elem_add(jdat->jnode, el);
518
519 ContainerNode json_node;
520
521 json_node.op = &json_op;
522 json_node.data.doc = (void*)jdat->doc;
523 json_node.data.data1 = (void*)el;
524 json_node.data.data2 = (void*)el->value.children.next;
525
526 return json_node;
527 }
528
529 static ContainerNode jsonNode_writeNewArray(ContainerNode *node,
530 const string &name)
531 PJSUA2_THROW(Error)
532 {
533 json_node_data *jdat = (json_node_data*)&node->data;
534 pj_json_elem *el = jdat->doc->allocElement();
535 pj_str_t nm = alloc_name(jdat->doc, name);
536
537 pj_json_elem_array(el, &nm);
538 pj_json_elem_add(jdat->jnode, el);
539
540 ContainerNode json_node;
541
542 json_node.op = &json_op;
543 json_node.data.doc = (void*)jdat->doc;
544 json_node.data.data1 = (void*)el;
545 json_node.data.data2 = (void*)el->value.children.next;
546
547 return json_node;
548 }