-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathload_json.cc
337 lines (276 loc) · 7.19 KB
/
load_json.cc
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright (C) 2017 Andreas Weber <andy@josoansi.de>
//
// GNU Octave wrapper around RapidJSON (http://rapidjson.org/)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see
// <http://www.gnu.org/licenses/>.
#include <vector>
#include "dynContainer.h"
#include "rapidjson/reader.h"
#include "rapidjson/error/en.h"
//#define DEBUG
#include "debug.h"
class parse_state
{
public:
parse_state (int depth)
: _depth (depth),
is_json_array (0),
array (NULL)
{
DBG_MSG2(_depth, "constructor, _depth = ", _depth);
}
~parse_state ()
{
DBG_MSG2(_depth, "destructor, _depth = ", _depth);
delete (array);
}
octave_scalar_map result;
void push_back (octave_value v)
{
DBG_MSG2(_depth, "is_json_array=", is_json_array);
if (is_json_array)
{
array->append_value (v);
}
else
{
result.contents (_key) = v;
}
}
void key (const char* str)
{
DBG_MSG1(_depth, str);
_key = str;
}
void start_array ()
{
DBG_MSG1(_depth, "");
is_json_array = true;
if (! array)
array = new dynContainer;
array->ob ();
}
void end_array ()
{
DBG_MSG1(_depth, "");
is_json_array = false;
array->cb ();
if (array->get_depth () == 0)
{
result.contents (_key) = array->get_array ();
delete (array);
array = NULL;
}
}
private:
int _depth; // only for debug output
std::string _key;
bool is_json_array; // we are between start_array() and end_array()
dynContainer *array;
};
class JSON_Handler : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, JSON_Handler>
{
private:
std::vector <parse_state*> ps;
public:
JSON_Handler ()
{
DBG_MSG1 (0, "constructor");
ps.push_back (new parse_state(0));
ps.back()->key("root");
}
~JSON_Handler ()
{
DBG_MSG1 (0, "destructor");
for (unsigned k = 0; k < ps.size(); ++k)
delete (ps[k]);
}
octave_value result ()
{
int n = ps.size ();
if (n != 1)
warning ("result () called too early, parsing is still in progress");
//return ps[0]->result; // FIXME: eigentlich wollte ich hier nur "root" zurück geben
//.contents ("root").scalar_map_value();
octave_value tmp = ps[0]->result.contents ("root");
return tmp;
}
bool Null()
{
DBG_MSG1 (0, "");
ps.back()->push_back (lo_ieee_na_value ());
return true;
}
bool Bool(bool b)
{
DBG_MSG1 (0, b);
ps.back()->push_back ((b)? true: false);
return true;
}
bool Uint(unsigned u)
{
DBG_MSG1 (0, u);
ps.back()->push_back (octave_uint32 (u));
return true;
}
bool Int(signed i)
{
DBG_MSG1 (0, i);
ps.back()->push_back (octave_int32 (i));
return true;
}
bool Int64(int64_t i)
{
DBG_MSG1 (0, i);
ps.back()->push_back (octave_int64 (i));
return true;
}
bool Uint64(uint64_t u)
{
DBG_MSG1 (0, u);
ps.back()->push_back (octave_uint64(u));
return true;
}
bool Double(double d)
{
DBG_MSG1 (0, d);
ps.back()->push_back (d);
return true;
}
bool String(const char* str, rapidjson::SizeType length, bool copy)
{
(void) length;
(void) copy;
DBG_MSG2 (0, str, length);
ps.back()->push_back (str);
return true;
}
bool StartObject()
{
DBG_MSG1 (0, "");
int d = ps.size () + 1;
ps.push_back (new parse_state(d));
return true;
}
bool Key(const char* str, rapidjson::SizeType length, bool copy)
{
(void) copy;
if (strlen (str) != length)
error ("octave-rapidjson can't handle strings with zeros");
ps.back()->key(str);
return true;
}
bool EndObject(rapidjson::SizeType memberCount)
{
(void) memberCount;
int n = ps.size ();
DBG_MSG2 (0, "ps.size() = ", n);
if (n > 1)
ps[n-2]->push_back (ps[n-1]->result);
//else
// result = ps[n-1]->result;
delete ps.back (); // this IS ps[n-1]
ps.pop_back ();
return true;
}
bool StartArray()
{
// Sollte JSON mit '[' anfangen, z.B.
// '[{"a":5}]'
// oder
// '[2,3]'
//if (! ps.size())
// ps.push_back (new parse_state(ps.size () + 1));
ps.back()->start_array ();
return true;
}
bool EndArray(rapidjson::SizeType elementCount)
{
(void) elementCount;
ps.back()->end_array ();
return true;
}
};
DEFUN_DLD (load_json, args,, "load_json (json_str)")
{
if (args.length () != 1)
print_usage ();
JSON_Handler handler;
rapidjson::Reader reader;
std::string json = args(0).string_value ();
DBG_MSG2(0, "json = ", json);
rapidjson::StringStream ss(json.c_str());
rapidjson::ParseResult ok = reader.Parse<rapidjson::kParseNanAndInfFlag>(ss, handler);
//rapidjson::Document d;
//rapidjson::ParseResult ok = d.ParseStream(ss);
if (! ok)
{
// show substring with error
#ifdef DEBUG
#define HALF_DBG_OUT 10
int left = HALF_DBG_OUT;
if (int(ok.Offset()) - left < 0)
left = ok.Offset();
// FIXME: this writes to stdout even when running "test load_json"
// -> find a way to handle this better
std::cout << "JSON Debug:" << json.substr (ok.Offset() - left, left + HALF_DBG_OUT + 1) << endl;
std::cout << "JSON Debug:";
for (int k = 0; k < left; ++k)
std::cout << " ";
std::cout << "^" << endl;;
#endif
error ("JSON parse error: '%s' at offset %zu",
rapidjson::GetParseError_En (ok.Code()),
ok.Offset());
}
return ovl (handler.result ());
}
/*
%!test
%! json = '{ "hello" : "world", "t" : true , "f" : false, "n": null, "i":-123, "u":456, "pi": 3.1416, "li": -765432986, "a":[1, 2, 3, 4], "b": ["foo", 4] } ';
%! r = load_json (json);
%! assert (r.hello, "world")
%! assert (r.t, true)
%! assert (r.f, false)
%! assert (r.n, NA)
%! assert (r.i, int32 (-123))
%! assert (r.u, uint32 (456))
%! assert (r.pi, pi, 1e-5)
%! assert (r.a, [1 2 3 4])
%! assert (r.b, {"foo", 4})
%!test
%! json = '{ "a": [[1,2],[3,4]]}';
%! r = load_json (json);
%! assert (r.a, [1 2; 3 4], eps);
%!test
%! json = '{ "a" : [[[1,2],[3,4]],[[5,6],[7,8.1]]], "b" : [10,20], "c": [100,200] }';
%! r = load_json (json);
%! assert (r.a, cat (3, [1 2; 3 4], [5 6; 7 8.1]), eps);
%! assert (r.b, [10 20], eps);
%! assert (r.c, [100 200], eps);
%!test
%! json = '[2,3,4]';
%! r = load_json (json);
%! assert (r, 2:4, eps)
%!test
%! json = '[[2.1],[3.4],[1.6]]';
%! r = load_json (json);
%! assert (r, [2.1, 3.4, 1.6].', eps)
# NaN/Inf is a JSON extension (not in the standard)
%!test
%! assert (load_json ("[NaN, Inf, -Inf]"), [NaN, Inf, -Inf])
%!error <'Invalid value.' at offset 5> load_json ('[1,2,,3]');
%!error <'Missing a colon after a name of object member.' at offset 5> load_json ('{"a" 5}');
*/