This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
fromjson.c
217 lines (155 loc) · 4.49 KB
/
fromjson.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifdef JSON_C_DIR_PREFIXED
#include <json-c/json.h>
#else
#include <json.h>
#endif
#include <stdio.h>
#include <string.h>
#include "mex.h"
void object(json_object * jo, mxArray ** mxa);
void parse(json_object * jo, mxArray ** mxa);
void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
char *buf;
struct json_object *jo;
if (nrhs != 1) {
mexErrMsgTxt("One input argument required.");
}
if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
}
if (!mxIsChar(prhs[0]) || (mxGetM(prhs[0]) != 1 ) ) {
mexErrMsgTxt("Input argument must be a string.");
}
buf = mxArrayToString(prhs[0]);
jo = json_tokener_parse(buf);
if(jo == NULL)
mexErrMsgTxt("error parsing json.");
else
parse(jo, &plhs[0]);
/* free memory */
json_object_put(jo);
mxFree(buf);
}
void value(json_object *jo, mxArray ** mxa){
enum json_type type = json_object_get_type(jo);
mxArray *ma;
switch (type) {
case json_type_boolean:
ma = mxCreateLogicalScalar(json_object_get_boolean(jo));
break;
case json_type_int:
ma = mxCreateNumericMatrix(1, 1, mxINT64_CLASS, mxREAL);
*((int64_t *)mxGetData(ma)) = json_object_get_int64(jo);
break;
case json_type_double:
ma = mxCreateDoubleScalar(json_object_get_double(jo));
break;
case json_type_string:
ma = mxCreateString(json_object_get_string(jo));
break;
}
*mxa = ma;
}
void array( json_object *jo, char *key, mxArray ** mxa) {
enum json_type type;
int i;
int len;
struct json_object *jv;
struct json_object *ja = jo;
mxArray *ma;
if(key){
ja = json_object_object_get(jo, key);
}
len = json_object_array_length(ja);
*mxa = mxCreateCellMatrix(len, 1);
for (i=0; i< len; i++){
jv = json_object_array_get_idx(ja, i);
if(jv){
type = json_object_get_type(jv);
if (type == json_type_array) {
array(jv, NULL, &ma);
}
else if (type != json_type_object) {
value(jv, &ma);
}
else {
object(jv, &ma);
}
}
else{
ma = mxCreateDoubleScalar(mxGetNaN());
}
mxSetCell(*mxa, i, ma);
}
}
int keys_count(json_object * jo){
int count = 0;
json_object_object_foreach(jo, key, val)
count++;
return count;
}
void keys_fill(json_object * jo, char *** keys, int count){
int i = 0;
struct json_object_iter it;
*keys = mxMalloc(count * sizeof(*keys));
json_object_object_foreachC(jo, it){
(*keys)[i] = mxMalloc(sizeof(char)*(strlen(it.key)+1));
strcpy((*keys)[i], it.key);
i++;
}
}
void object(json_object * jo, mxArray ** mxa) {
enum json_type type;
struct json_object_iter it;
char ** keys;
mxArray *ma;
int count = keys_count(jo);
keys_fill(jo, &keys, count);
*mxa = mxCreateStructMatrix(1, 1, count, (const char**) keys);
json_object_object_foreachC(jo, it) {
if(it.val){
type = json_object_get_type(it.val);
switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string:
value(it.val, &ma);
break;
case json_type_object:
object(json_object_object_get(jo, it.key), &ma);
break;
case json_type_array:
array(jo, it.key, &ma);
break;
}
}
else{
ma = mxCreateDoubleScalar(mxGetNaN());
}
mxSetField(*mxa, 0, it.key, ma);
}
}
void parse(json_object * jo, mxArray ** ma) {
enum json_type type;
if(jo){
type = json_object_get_type(jo);
switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string:
value(jo, ma);
break;
case json_type_object:
object(jo, ma);
break;
case json_type_array:
array(jo, NULL, ma);
break;
}
}else{
*ma = mxCreateDoubleScalar(mxGetNaN());
}
}