Skip to content

Commit 07c58f3

Browse files
committed
initial support for _DataLink_ of online/local file with JSONPath ref
1 parent 2936461 commit 07c58f3

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

jdatadecode.m

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -461,28 +461,30 @@
461461
end
462462

463463
%% handle data link
464-
if(isfield(data,N_('_DataLink_')))
465-
if(ischar(data.N('_DataLink_')))
466-
datalink=data.N('_DataLink_');
467-
[pat, ref]=regexp(datalink, '(^.+)(:($\d*\..*)*', 'match', 'tokens');
468-
if(~isempty(pat) && ~isempty(ref))
469-
[fpath, fname, fext]=fileparts(ref{1});
464+
if(opt.maxlinklevel>0 && isfield(data,N_('_DataLink_')))
465+
if(ischar(data.(N_('_DataLink_'))))
466+
datalink=data.(N_('_DataLink_'));
467+
ref=regexp(datalink, '^(?<proto>[a-zA-Z]+://)*(?<path>[^:]+)(?<delim>\:)*(?<jsonpath>\$\d*\..*)*', 'names');
468+
if(~isempty(ref.path))
469+
uripath=[ref.proto ref.path];
470+
[fpath, fname, fext]=fileparts(uripath);
471+
opt.maxlinklevel=opt.maxlinklevel-1;
470472
switch(lower(fext))
471473
case {'.json','.jnii','.jdt','.jdat','.jmsh','.jnirs'}
472-
newdata=loadjson([fpath, filesep, fname], varargin{:});
474+
newdata=loadjson(uripath, opt);
473475
case {'.bjd' ,'.bnii','.jdb','.jbat','.bmsh','.bnirs', '.jamm'}
474-
newdata=loadbj([fpath, filesep, fname], varargin{:});
476+
newdata=loadbj(uripath, opt);
475477
case {'.ubj'}
476-
newdata=loadubjson([fpath, filesep, fname], varargin{:});
478+
newdata=loadubjson(uripath, opt);
477479
case {'.msgpack'}
478-
newdata=loadmsgpack([fpath, filesep, fname], varargin{:});
480+
newdata=loadmsgpack(uripath, opt);
479481
case {'.h5','.hdf5','.snirf'} % this requires EasyH5 toolbox
480-
newdata=loadh5([fpath, filesep, fname], varargin{:});
482+
newdata=loadh5(uripath, opt);
481483
otherwise
482-
warning('datalink file is not supported');
484+
warning('_DataLink_ file type is not supported');
483485
end
484-
if(length(ref)>=2)
485-
newdata=getfromjsonpath(newdata,ref{2}{1});
486+
if(~isempty(ref.jsonpath))
487+
newdata=getfromjsonpath(newdata,ref.jsonpath);
486488
end
487489
end
488490
end

loadjson.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,13 @@
162162
end
163163
catch
164164
try
165-
string = urlread(['file://',fname]);
165+
string = urlread(fname);
166166
catch
167167
string = urlread(['file://',fullfile(pwd,fname)]);
168168
end
169169
end
170+
elseif(regexpi(fname,'^\s*(http|https|ftp|file)://'))
171+
string = urlread(fname);
170172
else
171173
error_pos('input file does not exist');
172174
end

test/run_jsonlab_test.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ function run_jsonlab_test(tests)
152152
test_jsonlab('output float format',@savejson,pi,'[3.142]','FloatFormat','%5.3f');
153153
test_jsonlab('remove singlet array',@savejson,{struct('a',1),5},'[{"a":1},5]','compact',1,'SingletArray',0);
154154
test_jsonlab('keep singlet array',@savejson,{struct('a',1),5},'[[{"a":[1]}],[5]]','compact',1,'SingletArray',1);
155+
test_jsonlab('test no datalink',@savejson,loadjson(savejson('a',struct(encodevarname('_DataLink_'),...
156+
'../examples/example2.json:$.glossary.title'))),'{"a":[{"_DataLink_":"..\/examples\/example2.json:$.glossary.title"}]}','compact',1,'SingletArray',1);
157+
test_jsonlab('test maxlinklevel',@savejson,loadjson(savejson('a',struct(encodevarname('_DataLink_'),...
158+
'../examples/example2.json:$.glossary.title')),'maxlinklevel',1),'{"a":"example glossary"}','compact',1,'SingletArray',1);
155159
end
156160

157161

0 commit comments

Comments
 (0)