|
278 | 278 | end |
279 | 279 | end |
280 | 280 | if (~fastok) |
281 | | -inputlen = length(jsonstring); |
282 | | -inputstr = jsonstring; |
283 | | - |
284 | | -% OPTIMIZATION: Precompute next non-whitespace position lookup table |
285 | | -ws = (inputstr == ' ' | inputstr == char(9) | inputstr == char(10) | inputstr == char(13)); |
286 | | -nonws_idx = find(~ws); |
287 | | -if ~isempty(nonws_idx) |
288 | | - marker = repmat(inputlen + 1, 1, inputlen); |
289 | | - marker(nonws_idx) = nonws_idx; |
290 | | - if (exist('cummin', 'builtin')) |
291 | | - cummin_ = @cummin; |
| 281 | + inputlen = length(jsonstring); |
| 282 | + inputstr = jsonstring; |
| 283 | + |
| 284 | + % OPTIMIZATION: Precompute next non-whitespace position lookup table |
| 285 | + ws = (inputstr == ' ' | inputstr == char(9) | inputstr == char(10) | inputstr == char(13)); |
| 286 | + nonws_idx = find(~ws); |
| 287 | + if ~isempty(nonws_idx) |
| 288 | + marker = repmat(inputlen + 1, 1, inputlen); |
| 289 | + marker(nonws_idx) = nonws_idx; |
| 290 | + if (exist('cummin', 'builtin')) |
| 291 | + cummin_ = @cummin; |
| 292 | + else |
| 293 | + cummin_ = @(x) arrayfun(@(i) min(x(1:i)), 1:length(x)); |
| 294 | + end |
| 295 | + opt.next_nonws_ = fliplr(cummin_(fliplr(marker))); |
292 | 296 | else |
293 | | - cummin_ = @(x) arrayfun(@(i) min(x(1:i)), 1:length(x)); |
| 297 | + opt.next_nonws_ = repmat(inputlen + 1, 1, inputlen); |
| 298 | + end |
| 299 | + opt.inputlen_ = inputlen; |
| 300 | + opt.utf8bytes_ = exist('OCTAVE_VERSION', 'builtin') ~= 0; % Octave's char holds UTF-8 bytes |
| 301 | + |
| 302 | + % Precompute array tokens for fast array parsing |
| 303 | + arraytokenidx = find(inputstr == '[' | inputstr == ']'); |
| 304 | + arraytoken = inputstr(arraytokenidx); |
| 305 | + |
| 306 | + % String delimiters and escape chars identified to improve speed: |
| 307 | + esc = find(inputstr == '"' | inputstr == '\'); |
| 308 | + |
| 309 | + opt.arraytoken_ = arraytoken; |
| 310 | + opt.arraytokenidx_ = arraytokenidx; |
| 311 | + opt.simplifycell = jsonopt('SimplifyCell', 1, opt); |
| 312 | + opt.simplifycellarray = jsonopt('SimplifyCellArray', opt.simplifycell, opt); |
| 313 | + opt.formatversion = jsonopt('FormatVersion', 3, opt); |
| 314 | + opt.fastarrayparser = jsonopt('FastArrayParser', 1, opt); |
| 315 | + opt.parsestringarray = jsonopt('ParseStringArray', 0, opt); |
| 316 | + opt.usemap = jsonopt('UseMap', 0, opt); |
| 317 | + opt.arraydepth_ = 1; |
| 318 | + opt.mmaponly = jsonopt('MmapOnly', 0, opt); |
| 319 | + |
| 320 | + if (jsonopt('ShowProgress', 0, opt) == 1) |
| 321 | + opt.progressbar_ = waitbar(0, 'loading ...'); |
294 | 322 | end |
295 | | - opt.next_nonws_ = fliplr(cummin_(fliplr(marker))); |
296 | | -else |
297 | | - opt.next_nonws_ = repmat(inputlen + 1, 1, inputlen); |
298 | | -end |
299 | | -opt.inputlen_ = inputlen; |
300 | | -opt.utf8bytes_ = exist('OCTAVE_VERSION', 'builtin') ~= 0; % Octave's char holds UTF-8 bytes |
301 | | - |
302 | | -% Precompute array tokens for fast array parsing |
303 | | -arraytokenidx = find(inputstr == '[' | inputstr == ']'); |
304 | | -arraytoken = inputstr(arraytokenidx); |
305 | | - |
306 | | -% String delimiters and escape chars identified to improve speed: |
307 | | -esc = find(inputstr == '"' | inputstr == '\'); |
308 | | - |
309 | | -opt.arraytoken_ = arraytoken; |
310 | | -opt.arraytokenidx_ = arraytokenidx; |
311 | | -opt.simplifycell = jsonopt('SimplifyCell', 1, opt); |
312 | | -opt.simplifycellarray = jsonopt('SimplifyCellArray', opt.simplifycell, opt); |
313 | | -opt.formatversion = jsonopt('FormatVersion', 3, opt); |
314 | | -opt.fastarrayparser = jsonopt('FastArrayParser', 1, opt); |
315 | | -opt.parsestringarray = jsonopt('ParseStringArray', 0, opt); |
316 | | -opt.usemap = jsonopt('UseMap', 0, opt); |
317 | | -opt.arraydepth_ = 1; |
318 | | -opt.mmaponly = jsonopt('MmapOnly', 0, opt); |
319 | | - |
320 | | -if (jsonopt('ShowProgress', 0, opt) == 1) |
321 | | - opt.progressbar_ = waitbar(0, 'loading ...'); |
322 | | -end |
323 | 323 |
|
324 | | -objid = jsonopt('ObjectID', 0, opt); |
325 | | -maxobjid = max(objid); |
326 | | -if (maxobjid == 0) |
327 | | - maxobjid = inf; |
328 | | -end |
329 | | -opt.jsonpath_ = '$'; |
330 | | -needmmap = (nargout > 1 || opt.mmaponly); |
331 | | -if needmmap |
332 | | - mmap = {}; |
333 | | -end |
334 | | -if isempty(nonws_idx) |
335 | | - data = []; |
336 | | - inputlen = 0; |
337 | | -end |
| 324 | + objid = jsonopt('ObjectID', 0, opt); |
| 325 | + maxobjid = max(objid); |
| 326 | + if (maxobjid == 0) |
| 327 | + maxobjid = inf; |
| 328 | + end |
| 329 | + opt.jsonpath_ = '$'; |
| 330 | + needmmap = (nargout > 1 || opt.mmaponly); |
| 331 | + if needmmap |
| 332 | + mmap = {}; |
| 333 | + end |
| 334 | + if isempty(nonws_idx) |
| 335 | + data = []; |
| 336 | + inputlen = 0; |
| 337 | + end |
338 | 338 |
|
339 | | -jsoncount = 1; |
340 | | -pos = 1; |
341 | | -index_esc = 1; |
342 | | -next_nonws = opt.next_nonws_; |
| 339 | + jsoncount = 1; |
| 340 | + pos = 1; |
| 341 | + index_esc = 1; |
| 342 | + next_nonws = opt.next_nonws_; |
343 | 343 |
|
344 | | -while pos <= inputlen |
345 | | - % Inline next_char |
346 | | - pos = next_nonws(pos); |
347 | | - cc = inputstr(pos); |
| 344 | + while pos <= inputlen |
| 345 | + % Inline next_char |
| 346 | + pos = next_nonws(pos); |
| 347 | + cc = inputstr(pos); |
348 | 348 |
|
349 | | - switch cc |
350 | | - case '{' |
351 | | - if needmmap |
352 | | - mmap{end + 1} = {opt.jsonpath_, [pos, 0, mmap_before(inputstr, pos)]}; |
353 | | - [data{jsoncount}, pos, index_esc, newmmap] = parse_object(inputstr, pos, esc, index_esc, opt); |
354 | | - if (pos < 0) |
355 | | - opt.usemap = 1; |
356 | | - [data{jsoncount}, pos, index_esc, newmmap] = parse_object(inputstr, -pos, esc, index_esc, opt); |
| 349 | + switch cc |
| 350 | + case '{' |
| 351 | + if needmmap |
| 352 | + mmap{end + 1} = {opt.jsonpath_, [pos, 0, mmap_before(inputstr, pos)]}; |
| 353 | + [data{jsoncount}, pos, index_esc, newmmap] = parse_object(inputstr, pos, esc, index_esc, opt); |
| 354 | + if (pos < 0) |
| 355 | + opt.usemap = 1; |
| 356 | + [data{jsoncount}, pos, index_esc, newmmap] = parse_object(inputstr, -pos, esc, index_esc, opt); |
| 357 | + end |
| 358 | + mmap{end}{2}(2) = mmap_length(inputstr, mmap{end}{2}(1), pos); |
| 359 | + mmap = [mmap(:); newmmap(:)]; |
| 360 | + else |
| 361 | + [data{jsoncount}, pos, index_esc] = parse_object(inputstr, pos, esc, index_esc, opt); |
| 362 | + if (pos < 0) |
| 363 | + opt.usemap = 1; |
| 364 | + [data{jsoncount}, pos, index_esc] = parse_object(inputstr, -pos, esc, index_esc, opt); |
| 365 | + end |
357 | 366 | end |
358 | | - mmap{end}{2}(2) = mmap_length(inputstr, mmap{end}{2}(1), pos); |
359 | | - mmap = [mmap(:); newmmap(:)]; |
360 | | - else |
361 | | - [data{jsoncount}, pos, index_esc] = parse_object(inputstr, pos, esc, index_esc, opt); |
362 | | - if (pos < 0) |
363 | | - opt.usemap = 1; |
364 | | - [data{jsoncount}, pos, index_esc] = parse_object(inputstr, -pos, esc, index_esc, opt); |
| 367 | + case '[' |
| 368 | + if needmmap |
| 369 | + mmap{end + 1} = {opt.jsonpath_, [pos, 0, mmap_before(inputstr, pos)]}; |
| 370 | + [data{jsoncount}, pos, index_esc, newmmap] = parse_array(inputstr, pos, esc, index_esc, opt); |
| 371 | + mmap{end}{2}(2) = mmap_length(inputstr, mmap{end}{2}(1), pos); |
| 372 | + mmap = [mmap(:); newmmap(:)]; |
| 373 | + else |
| 374 | + [data{jsoncount}, pos, index_esc] = parse_array(inputstr, pos, esc, index_esc, opt); |
365 | 375 | end |
366 | | - end |
367 | | - case '[' |
368 | | - if needmmap |
369 | | - mmap{end + 1} = {opt.jsonpath_, [pos, 0, mmap_before(inputstr, pos)]}; |
370 | | - [data{jsoncount}, pos, index_esc, newmmap] = parse_array(inputstr, pos, esc, index_esc, opt); |
371 | | - mmap{end}{2}(2) = mmap_length(inputstr, mmap{end}{2}(1), pos); |
372 | | - mmap = [mmap(:); newmmap(:)]; |
373 | | - else |
374 | | - [data{jsoncount}, pos, index_esc] = parse_array(inputstr, pos, esc, index_esc, opt); |
375 | | - end |
376 | | - otherwise |
377 | | - error_pos('Outer level structure must be an object or an array', inputstr, pos); |
378 | | - end |
379 | | - if jsoncount >= maxobjid |
380 | | - break |
| 376 | + otherwise |
| 377 | + error_pos('Outer level structure must be an object or an array', inputstr, pos); |
| 378 | + end |
| 379 | + if jsoncount >= maxobjid |
| 380 | + break |
| 381 | + end |
| 382 | + opt.jsonpath_ = sprintf('$%d', jsoncount); |
| 383 | + jsoncount = jsoncount + 1; |
381 | 384 | end |
382 | | - opt.jsonpath_ = sprintf('$%d', jsoncount); |
383 | | - jsoncount = jsoncount + 1; |
384 | | -end |
385 | 385 |
|
386 | 386 | end |
387 | 387 |
|
|
1546 | 1546 | dopt = jdopt_(opt, tt, parent, vt, off, nchild, fnt, usemap, isopen); |
1547 | 1547 | end |
1548 | 1548 | try |
1549 | | - V{c} = jdatadecode(V{c}, dopt); |
1550 | | - catch |
1551 | | - return % the element-by-element parser, and its warning |
1552 | | - end |
| 1549 | + V{c} = jdatadecode(V{c}, dopt); |
| 1550 | + catch |
| 1551 | + return % the element-by-element parser, and its warning |
| 1552 | + end |
1553 | 1553 | end |
1554 | 1554 | continue |
1555 | 1555 | end |
|
0 commit comments