| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |