|
1 | 1 | function json = jsonget(fname, mmap, varargin) |
2 | 2 | % |
3 | | -% json=jsonget(fname,mmap,'$.jsonpath1','$.jsonpath2',...) |
| 3 | +% json=jsonget(data,'$.jsonpath1','$.jsonpath2',...) |
| 4 | +% or |
| 5 | +% json=jsonget(filename,mmap,'$.jsonpath1','$.jsonpath2',...) |
4 | 6 | % |
5 | 7 | % Fast reading of JSON data records using memory-map (mmap) returned by |
6 | 8 | % loadjson and JSONPath-like keys |
|
9 | 11 | % initially created on 2022/02/02 |
10 | 12 | % |
11 | 13 | % input: |
12 | | -% fname: a JSON/BJData/UBJSON string or stream, or a file name |
| 14 | +% data: a struct, cell, or any other matlab variable |
| 15 | +% filename: a JSON/BJData/UBJSON string or stream, or a file name |
13 | 16 | % mmap: memory-map returned by loadjson/loadbj of the same data |
14 | 17 | % important: mmap must be produced from the same file/string, |
15 | 18 | % otherwise calling this function may cause data corruption |
|
46 | 49 | end |
47 | 50 | end |
48 | 51 |
|
49 | | -mmap = [mmap{:}]; |
50 | | -keylist = mmap(1:2:end); |
| 52 | +if (iscell(mmap)) |
| 53 | + mmap = [mmap{:}]; |
| 54 | + keylist = mmap(1:2:end); |
51 | 55 |
|
52 | | -loc = 1:length(keylist); |
53 | | -if (length(varargin) >= 1) |
54 | | - [tf, loc] = ismember(varargin, keylist); |
55 | | - if (any(tf)) |
56 | | - keylist = keylist(loc); |
57 | | - else |
58 | | - keylist = {}; |
| 56 | + loc = 1:length(keylist); |
| 57 | + if (length(varargin) >= 1) |
| 58 | + [tf, loc] = ismember(varargin, keylist); |
| 59 | + if (any(tf)) |
| 60 | + keylist = keylist(loc); |
| 61 | + else |
| 62 | + keylist = {}; |
| 63 | + end |
59 | 64 | end |
| 65 | +else |
| 66 | + keylist = [{mmap}, varargin{:}]; |
60 | 67 | end |
61 | 68 |
|
62 | | -json = {}; |
| 69 | +json = cell(1, length(keylist)); |
63 | 70 |
|
64 | | -if (isstruct(fname) || iscell(fname) || isa(fname, 'table') || isa(fname, 'containers.Map')) |
| 71 | +if (isstruct(fname) || iscell(fname) || isa(fname, 'table') || isa(fname, 'containers.Map') || ... |
| 72 | + isa(fname, 'containers.Map') || isa(input, 'jdict')) |
65 | 73 | for i = 1:length(keylist) |
66 | | - json{end + 1} = jsonpath(fname, keylist{i}); |
| 74 | + json{i} = jsonpath(fname, keylist{i}); |
67 | 75 | end |
68 | 76 | if (length(json) == 1) |
69 | 77 | json = json{1}; |
|
75 | 83 | bmap = mmap{loc(i) * 2}; |
76 | 84 | rec = {'uint8', [1, bmap(2)], 'x'}; |
77 | 85 | if (exist('inputstr', 'var')) |
78 | | - json{end + 1} = {keylist{i}, inputstr(bmap(1):bmap(1) + bmap(2) - 1)}; |
| 86 | + json{i} = {keylist{i}, inputstr(bmap(1):bmap(1) + bmap(2) - 1)}; |
79 | 87 | else |
80 | 88 | if (exist('fid', 'var') && fid >= 0) |
81 | 89 | fseek(fid, bmap(1), -1); |
82 | | - json{end + 1} = {keylist{i}, fread(fid, bmap(1), 'uint8=>char')}; |
| 90 | + json{i} = {keylist{i}, fread(fid, bmap(1), 'uint8=>char')}; |
83 | 91 | else |
84 | 92 | fmap = memmapfile(fname, 'writable', false, 'offset', bmap(1), 'format', rec); |
85 | | - json{end + 1} = {keylist{i}, char(fmap.Data(1).x)}; |
| 93 | + json{i} = {keylist{i}, char(fmap.Data(1).x)}; |
86 | 94 | end |
87 | 95 | end |
88 | 96 | end |
|
0 commit comments