Debian Codegraph @

Xpjproject/pjlib-util/src/pjlib-util-test/json_test.c

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 "test.h"
19
20 #define THIS_FILE "json_test.c"
21
22 #if INCLUDE_JSON_TEST
23
24 #include <pjlib-util/json.h>
25 #include <pj/log.h>
26 #include <pj/string.h>
27
28 static char json_doc1[] =
29 "{\
30 \"Object\": {\
31 \"Integer\": 800,\
32 \"Negative\": -12,\
33 \"Float\": -7.2,\
34 \"String\": \"A\\tString with tab\",\
35 \"Object2\": {\
36 \"True\": true,\
37 \"False\": false,\
38 \"Null\": null\
39 },\
40 \"Array1\": [116, false, \"string\", {}],\
41 \"Array2\": [\
42 {\
43 \"Float\": 123.,\
44 },\
45 {\
46 \"Float\": 123.,\
47 }\
48 ]\
49 },\
50 \"Integer\": 800,\
51 \"Array1\": [116, false, \"string\"]\
52 }\
53 ";
54
55 static int json_verify_1()
56 {
57 pj_pool_t *pool;
58 pj_json_elem *elem;
59 char *out_buf;
60 unsigned size;
61 pj_json_err_info err;
62
63 pool = pj_pool_create(mem, "json", 1000, 1000, NULL);
64
65 size = (unsigned)strlen(json_doc1);
66 elem = pj_json_parse(pool, json_doc1, &size, &err);
67 if (!elem) {
68 PJ_LOG(1, (THIS_FILE, " Error: json_verify_1() parse error"));
69 goto on_error;
70 }
71
72 size = (unsigned)strlen(json_doc1) * 2;
73 out_buf = pj_pool_alloc(pool, size);
74
75 if (pj_json_write(elem, out_buf, &size)) {
76 PJ_LOG(1, (THIS_FILE, " Error: json_verify_1() write error"));
77 goto on_error;
78 }
79
80 PJ_LOG(3,(THIS_FILE, "Json document:\n%s", out_buf));
81 pj_pool_release(pool);
82 return 0;
83
84 on_error:
85 pj_pool_release(pool);
86 return 10;
87 }
88
89
90 int json_test(void)
91 {
92 int rc;
93
94 rc = json_verify_1();
95 if (rc)
96 return rc;
97
98 return 0;
99 }
100
101
102
103 #else
104 int json_dummy;
105 #endif