Skip to content

Commit

Permalink
make datalink URL query more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Mar 30, 2022
1 parent dd9e9c6 commit 6368409
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions examples/random_json_joke.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function jokes=random_json_joke(num, url)
% this example shows how to use the _DataLink_ annotation defined in the
% JData specification
% (https://github.com/NeuroJSON/jdata/blob/master/JData_specification.md#data-referencing-and-links)
% to define linked JSON/binary JSON data using external files or URL on the
% web. In the below example, the jokeapi.dev feed returns a JSON record via
% RESTFul URL, the returned record contains a subfield called `joke`, which
% can be retrieved via the JSONPath $.joke attched after the URL, separated
% by a collon. The general _DataLink_ URL is in the form of "URL:$jsonpath"

if(nargin==0)
num=1;
end

if(nargin<2)
url='https://v2.jokeapi.dev/joke/Programming?type=single';
end

joke.(encodevarname('_DataLink_'))=[url ':$.joke'];
jurl=savejson('',joke);

jokes=cell(1,num);
for i=1:num
jokes{i}=loadjson(jurl, 'maxlinklevel',1);
end

if(num==1)
jokes=jokes{1};
end
2 changes: 1 addition & 1 deletion jdatadecode.m
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@
if(opt.maxlinklevel>0 && isfield(data,N_('_DataLink_')))
if(ischar(data.(N_('_DataLink_'))))
datalink=data.(N_('_DataLink_'));
ref=regexp(datalink, '^(?<proto>[a-zA-Z]+://)*(?<path>[^:]+)(?<delim>\:)*(?<jsonpath>\$\d*\..*)*', 'names');
ref=regexp(datalink, '^(?<proto>[a-zA-Z]+://)*(?<path>.+)(?<delim>\:)()*(?<jsonpath>(?<=:)\$\d*\..*)*', 'names');
if(~isempty(ref.path))
uripath=[ref.proto ref.path];
[fpath, fname, fext]=fileparts(uripath);
Expand Down

0 comments on commit 6368409

Please sign in to comment.