|
17 | 17 | #include <my_sys.h>
|
18 | 18 | #include <json_lib.h>
|
19 | 19 |
|
20 |
| -int json_locate_key(const char *js, const char *js_end, const char *kname, |
21 |
| - const char **key_start, const char **key_end, |
22 |
| - int *comma_pos); |
23 |
| -int main() |
| 20 | +const char *json="{\"int\":1,\"str\":\"foo bar\"," |
| 21 | + "\"array\":[10,20,{\"c\":\"d\"}],\"bool\":false}"; |
| 22 | + |
| 23 | +const char *json_ar="[1,\"foo bar\",[10,20,{\"c\":\"d\"}],false]"; |
| 24 | + |
| 25 | +const char *json_w="{\"int\" : 1 , " |
| 26 | + "\"array\" : [10,20,{\"c\":\"d\"}] , \"bool\" : false }"; |
| 27 | +const char *json_1="{ \"str\" : \"foo bar\" }"; |
| 28 | + |
| 29 | +void do_json(const char *key, int type, const char *value) |
| 30 | +{ |
| 31 | + enum json_types value_type; |
| 32 | + const char *value_start; |
| 33 | + int value_len; |
| 34 | + |
| 35 | + value_type= json_get_object_key(json, json + strlen(json), |
| 36 | + key, &value_start, &value_len); |
| 37 | + if (type) |
| 38 | + ok(value_type == type && value_len == (int)strlen(value) && !memcmp(value_start, value, value_len), |
| 39 | + "%s: type=%u, value(%d)=\"%.*s\"", key, value_type, value_len, value_len, value_start); |
| 40 | + else |
| 41 | + ok(value_type == type && value_len == (int)strlen(value), |
| 42 | + "%s: type=%u keys=%u end=\"%s\"", key, value_type, value_len, value_start); |
| 43 | +} |
| 44 | + |
| 45 | +void do_json_ar(int n, int type, const char *value) |
24 | 46 | {
|
25 |
| - const char *json="{\"int\":1, \"str\":\"foo bar\", " |
26 |
| - "\"array\":[10,20,{\"c\":\"d\"}],\"bool\":false}"; |
27 |
| - const char *json_ar="[1, \"foo bar\", " "[10,20,{\"c\":\"d\"}], false]"; |
28 |
| - const char *json_w="{\"int\" : 1 , \"str\" : \"foo bar\" , " |
29 |
| - "\"array\" : [10,20,{\"c\":\"d\"}] , \"bool\" : false }"; |
30 |
| - const char *json_1="{ \"str\" : \"foo bar\" }"; |
31 | 47 | enum json_types value_type;
|
32 | 48 | const char *value_start;
|
33 | 49 | int value_len;
|
| 50 | + |
| 51 | + value_type= json_get_array_item(json_ar, json_ar + strlen(json_ar), |
| 52 | + n, &value_start, &value_len); |
| 53 | + if (type) |
| 54 | + ok(value_type == type && value_len == (int)strlen(value) && !memcmp(value_start, value, value_len), |
| 55 | + "%i: type=%u, value(%d)=\"%.*s\"", n, value_type, value_len, value_len, value_start); |
| 56 | + else |
| 57 | + ok(value_type == type && value_len == (int)strlen(value), |
| 58 | + "%i: type=%u keys=%u end=\"%s\"", n, value_type, value_len, value_start); |
| 59 | +} |
| 60 | + |
| 61 | +void do_json_locate(const char *json, const char *key, int from, int to, int cp) |
| 62 | +{ |
34 | 63 | const char *key_start, *key_end;
|
35 |
| - int result, comma_pos; |
36 |
| - |
37 |
| - plan(15); |
38 |
| - |
39 |
| -#define do_json(V) \ |
40 |
| - do { \ |
41 |
| - value_type= json_get_object_key(json, json+strlen(json), \ |
42 |
| - V, &value_start, &value_len); \ |
43 |
| - ok(value_type != JSV_BAD_JSON, V); \ |
44 |
| - diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \ |
45 |
| - } while(0) |
46 |
| -#define do_json_ar(N) \ |
47 |
| - do { \ |
48 |
| - value_type= json_get_array_item(json_ar, json_ar+strlen(json_ar), \ |
49 |
| - N, &value_start, &value_len); \ |
50 |
| - ok(value_type != JSV_BAD_JSON, #N); \ |
51 |
| - diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \ |
52 |
| - } while(0) |
53 |
| -#define do_json_locate(J, V) \ |
54 |
| - do { \ |
55 |
| - result= json_locate_key(J, J+strlen(J), \ |
56 |
| - V, &key_start, &key_end, &comma_pos); \ |
57 |
| - ok(result == 0, V); \ |
58 |
| - if (key_start) \ |
59 |
| - diag("key_str=\"%.*s\" comma_pos= %d", (int)(key_end - key_start), key_start, comma_pos); \ |
60 |
| - else \ |
61 |
| - diag("no key found"); \ |
62 |
| - } while(0) |
63 |
| - |
64 |
| - do_json("int"); |
65 |
| - do_json("str"); |
66 |
| - do_json("bool"); |
67 |
| - do_json("c"); |
68 |
| - do_json("array"); |
69 |
| - |
70 |
| - do_json_ar(0); |
71 |
| - do_json_ar(1); |
72 |
| - do_json_ar(2); |
73 |
| - do_json_ar(3); |
74 |
| - do_json_ar(4); |
75 |
| - |
76 |
| - do_json_locate(json_w, "bool"); |
77 |
| - do_json_locate(json_w, "int"); |
78 |
| - do_json_locate(json_w, "array"); |
79 |
| - do_json_locate(json_1, "str"); |
80 |
| - do_json_locate(json_w, "c"); |
| 64 | + int res, comma_pos; |
| 65 | + |
| 66 | + res= json_locate_key(json, json + strlen(json), |
| 67 | + key, &key_start, &key_end, &comma_pos); |
| 68 | + if (key_start) |
| 69 | + ok(res == 0 && key_start - json == from && key_end - json == to && |
| 70 | + comma_pos == cp, "%s: [%d,%d,%d] %.*s%s", key, (int)(key_start-json), |
| 71 | + (int)(key_end-json), comma_pos, (int)(key_start - json), json, key_end); |
| 72 | + else |
| 73 | + ok(res == 0 && from == -1, "%s: key not found", key); |
| 74 | +} |
| 75 | + |
| 76 | +int main() |
| 77 | +{ |
| 78 | + plan(18); |
| 79 | + |
| 80 | + diag("%s", json); |
| 81 | + do_json("int", 4, "1"); |
| 82 | + do_json("str", 3, "foo bar"); |
| 83 | + do_json("bool", 6, "false"); |
| 84 | + do_json("c", 0, "1234"); |
| 85 | + do_json("array", 2, "[10,20,{\"c\":\"d\"}]"); |
| 86 | + diag("%s", json_ar); |
| 87 | + do_json_ar(0, 4, "1"); |
| 88 | + do_json_ar(1, 3, "foo bar"); |
| 89 | + do_json_ar(2, 2, "[10,20,{\"c\":\"d\"}]"); |
| 90 | + do_json_ar(3, 6, "false"); |
| 91 | + do_json_ar(4, 0, "1234"); |
| 92 | + |
| 93 | + do_json_locate(json, "bool", 50, 63, 1); |
| 94 | + do_json_locate(json, "int", 1, 9, 2); |
| 95 | + do_json_locate(json, "array", 24, 50, 1); |
| 96 | + do_json_locate(json_w, "bool", 43, 61, 1); |
| 97 | + do_json_locate(json_w, "int", 1, 12, 2); |
| 98 | + do_json_locate(json_w, "array", 11, 43, 1); |
| 99 | + do_json_locate(json_w, "c", -1, -1, -1); |
| 100 | + do_json_locate(json_1, "str", 1, 22, 0); |
| 101 | + |
81 | 102 | return exit_status();
|
82 | 103 | }
|
0 commit comments