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
/
tojson.c
244 lines (187 loc) · 5.53 KB
/
tojson.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifdef JSON_C_DIR_PREFIXED
#include <json-c/json.h>
#else
#include <json.h>
#endif
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "mex.h"
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
void parse(mxArray *ma, json_object **jo);
void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
struct json_object *jo = NULL;
char* out;
if (nrhs != 1) {
mexErrMsgTxt("One input argument required.");
}
if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
}
parse((mxArray *) prhs[0], &jo);
if(jo){
out = (char*) json_object_to_json_string(jo);
}
else{
out = "null";
}
plhs[0] = mxCreateString(out);
if(jo) json_object_put(jo); /* free memory */
}
void numeric(double dbl, json_object **jo){
int intgr = round(dbl);
if(mxIsNaN(dbl) || mxIsInf(dbl)){
*jo = NULL;
}
else{
if(fabs(dbl-(double)intgr) > 1e-18){
*jo = json_object_new_double(dbl);
}
else{
*jo = json_object_new_int(intgr);
}
}
}
void object(mxArray *ma, int i, json_object **jo){
int j = 0;
int n = mxGetNumberOfFields(ma);
mxArray *tmpma;
json_object *tmpobj;
*jo = json_object_new_object();
for(; j < n; j++){
bool needDestroy = false;
tmpma = mxGetFieldByNumber(ma, i, j);
if (tmpma == NULL){
/* Object is NULL, create an empty matrix instead */
tmpma = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
needDestroy = true;
}
parse(tmpma, &tmpobj);
json_object_object_add(*jo, mxGetFieldNameByNumber(ma, j), tmpobj);
if (needDestroy)
mxDestroyArray(tmpma);
}
}
void parse(mxArray *ma, json_object **jo){
int i = 0;
size_t num_el = mxGetNumberOfElements(ma);
size_t m = mxGetM(ma);
size_t n = mxGetN(ma);
json_object *lobj;
json_object *tmpobj;
mxArray *tmpma;
/* was char - convert to string*/
if(mxIsChar(ma)){
char *str = mxArrayToString(ma);
*jo = json_object_new_string(str);
}
/* was struct - convert to object */
else if(mxIsStruct(ma)){
if(num_el == 1){
object(ma, 0, jo);
}
else{
/* it was a list of objects! */
*jo = json_object_new_array();
if(n == 1){
n = m;
m = 1;
}
for(i = 0; i < num_el; i++){
if(i % m == 0 && m > 1){
lobj = json_object_new_array();
json_object_array_add(*jo,lobj);
}
object(ma, i, &tmpobj);
if(m > 1)
json_object_array_add(lobj, tmpobj);
else
json_object_array_add(*jo, tmpobj);
}
}
}
/* was cell array - convert to list */
else if(mxIsCell(ma)){
*jo = json_object_new_array();
if(n == 1){
n = m;
m = 1;
}
for(i = 0; i < num_el; i++){
if(i % m == 0 && m > 1){
lobj = json_object_new_array();
json_object_array_add(*jo,lobj);
}
tmpma = mxGetCell(ma, i);
if(tmpma){
parse(tmpma, &tmpobj);
if(m > 1)
json_object_array_add(lobj, tmpobj);
else
json_object_array_add(*jo, tmpobj);
}
}
}
/* was numeric or bool - convert to number or list */
else{
/* single number or bool */
if(num_el == 1){
/* mxINT32_CLASS */
if(mxIsComplex(ma)){
mexErrMsgTxt("Data cannot be complex.");
}
if(mxIsNumeric(ma)){
double dbl = mxGetScalar(ma);
numeric(dbl, jo);
}
else if(mxIsLogical(ma)){
*jo = json_object_new_boolean(mxIsLogicalScalarTrue(ma));
}
else{
printf("Type is: %s\n", mxGetClassName(ma));
mexErrMsgTxt("Encountered an unknown type.");
}
}
else{
double *dbl;
int *intgr;
mxLogical *bl;
if(mxIsDouble(ma)){
dbl = mxGetData(ma);
}
else if(mxIsClass(ma, "mxINT32_CLASS")){
intgr = mxGetData(ma);
}
else if(mxIsLogical(ma)){
bl = mxGetData(ma);
}
else{
printf("Type is: %s\n", mxGetClassName(ma));
mexErrMsgTxt("Encountered an unsupported type.");
}
*jo = json_object_new_array();
/* vector - convert to list */
if(n == 1){
n = m;
m = 1;
}
for(i = 0; i < num_el; i++){
if(i % m == 0 && m > 1){
lobj = json_object_new_array();
json_object_array_add(*jo,lobj);
}
if(mxIsLogical(ma))
tmpobj = json_object_new_boolean(bl[i]);
else if(mxIsDouble(ma))
numeric(dbl[i], &tmpobj);
else
numeric((double)intgr[i], &tmpobj);
if(m > 1)
json_object_array_add(lobj, tmpobj);
else
json_object_array_add(*jo, tmpobj);
}
}
}
}