Skip to content

Commit 5034423

Browse files
committed
[feat] extend jsonget and jsonset to use jsonpath based getter/setting
1 parent 17d99d2 commit 5034423

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

jsonget.m

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
function json = jsonget(fname, mmap, varargin)
22
%
3-
% json=jsonget(fname,mmap,'$.jsonpath1','$.jsonpath2',...)
3+
% json=jsonget(data,'$.jsonpath1','$.jsonpath2',...)
4+
% or
5+
% json=jsonget(filename,mmap,'$.jsonpath1','$.jsonpath2',...)
46
%
57
% Fast reading of JSON data records using memory-map (mmap) returned by
68
% loadjson and JSONPath-like keys
@@ -9,7 +11,8 @@
911
% initially created on 2022/02/02
1012
%
1113
% 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
1316
% mmap: memory-map returned by loadjson/loadbj of the same data
1417
% important: mmap must be produced from the same file/string,
1518
% otherwise calling this function may cause data corruption
@@ -46,24 +49,29 @@
4649
end
4750
end
4851

49-
mmap = [mmap{:}];
50-
keylist = mmap(1:2:end);
52+
if (iscell(mmap))
53+
mmap = [mmap{:}];
54+
keylist = mmap(1:2:end);
5155

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
5964
end
65+
else
66+
keylist = [{mmap}, varargin{:}];
6067
end
6168

62-
json = {};
69+
json = cell(1, length(keylist));
6370

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'))
6573
for i = 1:length(keylist)
66-
json{end + 1} = jsonpath(fname, keylist{i});
74+
json{i} = jsonpath(fname, keylist{i});
6775
end
6876
if (length(json) == 1)
6977
json = json{1};
@@ -75,14 +83,14 @@
7583
bmap = mmap{loc(i) * 2};
7684
rec = {'uint8', [1, bmap(2)], 'x'};
7785
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)};
7987
else
8088
if (exist('fid', 'var') && fid >= 0)
8189
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')};
8391
else
8492
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)};
8694
end
8795
end
8896
end

jsonset.m

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function json = jsonset(fname, mmap, varargin)
22
%
3+
% newdata=jsonset(data,'$.jsonpath1',newval1,'$.jsonpath2','newval2',...)
4+
% or
35
% json=jsonset(fname,mmap,'$.jsonpath1',newval1,'$.jsonpath2','newval2',...)
46
%
57
% Fast writing of JSON data records to stream or disk using memory-map
@@ -9,6 +11,7 @@
911
% initially created on 2022/02/02
1012
%
1113
% input:
14+
% data: a struct, cell, or any other matlab variable
1215
% fname: a JSON/BJData/UBJSON string or stream, or a file name
1316
% mmap: memory-map returned by loadjson/loadbj of the same data
1417
% important: mmap must be produced from the same file/string,
@@ -43,12 +46,21 @@
4346
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
4447
%
4548

46-
if (regexp(fname, '^\s*(?:\[.*\])|(?:\{.*\})\s*$', 'once'))
47-
inputstr = fname;
49+
if (ischar(fname) || isa(fname, 'string'))
50+
if (regexp(fname, '^\s*(?:\[.*\])|(?:\{.*\})\s*$', 'once'))
51+
inputstr = fname;
52+
else
53+
if (~exist('memmapfile', 'file'))
54+
fid = fopen(fname, 'r+b');
55+
end
56+
end
4857
else
49-
if (~exist('memmapfile', 'file'))
50-
fid = fopen(fname, 'r+b');
58+
keylist = [{mmap}, varargin{:}];
59+
for i = 1:2:length(keylist)
60+
fname = jsonpath(fname, keylist{i}, keylist{i + 1});
5161
end
62+
json = fname;
63+
return
5264
end
5365

5466
mmap = [mmap{:}];
@@ -61,7 +73,8 @@
6173
end
6274
end
6375

64-
json = {};
76+
json = cell(1, floor(length(varargin) / 2));
77+
6578
for i = 1:2:length(varargin)
6679
if (regexp(varargin{i}, '^\$'))
6780
[tf, loc] = ismember(varargin{i}, keylist);
@@ -85,7 +98,7 @@
8598
fseek(fid, bmap(1) - 1, 'bof');
8699
fwrite(fid, val);
87100
end
88-
json{end + 1} = {varargin{i}, val};
101+
json{(i + 1) / 2} = {varargin{i}, val};
89102
end
90103
end
91104
end

0 commit comments

Comments
 (0)