-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson2resultset.c
160 lines (132 loc) · 4.1 KB
/
json2resultset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* json2resultset
* create ResultSet from json string.
*
* @param js json string
* @param sil string item list
*/
#include <stdio.h>
#include <json/json.h>
#include "stringlist.h"
#include "resultset.h"
#include "json2resultset.h"
StringItemList* create_columnlist(json_object* root_obj)
{
json_object *columns_obj;
StringItemList* sil = NULL;
columns_obj = json_object_object_get(root_obj, "columns");
// printf("columns_obj=%s\n", json_object_to_json_string(columns_obj));
if (json_object_get_type(columns_obj) == json_type_array) {
// 配列
json_object* obj;
int i;
int array_num = json_object_array_length(columns_obj);
if ((sil = createStringItemList()) == NULL)
return NULL;
// array_list* list = json_object_get_array(columns_obj);
for (i = 0; i < array_num; i++) {
StringItem* si;
obj = json_object_array_get_idx(columns_obj, i);
if (json_object_get_type(obj) == json_type_string) {
// printf("column(%d):%s\n", i, json_object_get_string(obj));
if ((si = createStringItem(json_object_get_string(obj))) == NULL)
return NULL;
addStringItem(sil, si);
}
}
}
return sil;
}
ResultSet* json2resultset(char* js) {
ResultSet* rs;
json_object* json_obj;
json_object* data_obj;
StringItemList* cl; // column name list
/* create json object (JSON-C functoin) */
json_obj = json_tokener_parse(js);
if (json_obj == NULL) {
return NULL;
}
if ((long) json_obj < 0) {
return NULL;
}
/* create column name list */
cl = create_columnlist(json_obj);
/* create empty resultset */
rs = createResultSet(cl);
data_obj = json_object_object_get(json_obj, "data");
// printf("data_obj=%s\n", json_object_to_json_string(data_obj));
if (json_object_get_type(data_obj) == json_type_array) {
// type is array
int i, j; // i:record, j:column
int column_num = get_column_num(json_obj);
int array_num_i = json_object_array_length(data_obj);
// array_list* record_list = json_object_get_array(data_obj);
json_object* record; // 1 record data
for (i = 0; i < array_num_i; i++) {
record = json_object_array_get_idx(data_obj, i);
if (json_object_get_type(data_obj) == json_type_array) {
StringItemList* sil;
RowData* row_data;
sil = createStringItemList();
for (j = 0; j < column_num; j++) {
json_object* obj; // 1 column data
StringItem* si;
obj = json_object_array_get_idx(record, j);
if (obj == NULL) {
// null
si = createStringItem((char*) NULL);
addStringItem(sil, si);
} else if (json_object_get_type(obj) == json_type_string) {
/// printf("data(%d):%s\n", j, json_object_get_string(obj));
// create StringItem
si = createStringItem((char*) json_object_get_string(obj));
addStringItem(sil, si);
}
}
/* create RowData, and add ResultSet */
row_data = createRowData(sil);
addRowData(rs, row_data);
}
}
}
return rs;
}
int
get_column_num(json_object* root_obj) {
json_object *columns_obj;
columns_obj = json_object_object_get(root_obj, "columns");
// printf("columns_obj=%s\n", json_object_to_json_string(columns_obj));
if (json_object_get_type(columns_obj) == json_type_array) {
return json_object_array_length(columns_obj);
}
return -1; // not array
}
#ifdef JSON2RESULTSET
int
main(int argc, char** argv) {
char* default_string = "{ \"columns\" : [ \"name\", \"area\",\"description\" ], \"data\" : [ [ \"nuko_yokohama\", \"Yokohama\",\"abc\" ], [ \"foo\", \"Nagoya\",\"efg\" ], [ \"bar\", \"Tokyo\",\"xyz\" ] ] }";
char* json_string;
printf("argc=%d\n", argc);
if (argc != 2) {
json_string = default_string;
} else {
json_string = argv[1];
}
printf("main start, json_string=%s\n", json_string);
ResultSet* rs;
RowData* rd;
int i;
rs = json2resultset(json_string);
if (rs == NULL) {
fprintf(stderr, "json2resultset error, json_string=%s\n", json_string);
}
initResultSet(rs);
while ((rd = getNextResultSet(rs)) != NULL) {
for (i = 1; i <= rs->column_num; i++) {
printf("value[%d]=%s\n", i, getValueRowData(rd, i));
}
}
freeResultSet(rs);
}
#endif // JSON2RESULTSET