-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathjsonpath.m
More file actions
225 lines (216 loc) · 7.62 KB
/
jsonpath.m
File metadata and controls
225 lines (216 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
function obj = jsonpath(root, jpath, varargin)
%
% obj=jsonpath(root, jpath)
% obj=jsonpath(root, jpath, newvalue)
%
% Getting or setting data elements from matlab data structures using
% JSONPath
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% root: a matlab data structure like an array, cell, struct, etc
% jpath: a string in the format of JSONPath, see loadjson help
% newvalue: if present, the data specified at the path jpath will be
% replaced by newvalue
%
% output:
% obj: if the specified element exist, obj returns the result
%
% example:
% jsonpath(struct('a',[1,2,3]), '$.a[1]') % returns 2
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://neurojson.org/jsonlab)
%
obj = root;
jpath = regexprep(jpath, '([^.\]])(\[[-0-9:\*]+\])', '$1.$2');
jpath = regexprep(jpath, '\[[''"]*([^\]''"]+)[''"]*\]', '.[$1]');
jpath = regexprep(jpath, '\\\.', '_0x2E_');
while (regexp(jpath, '(\[[''"]*[^\]''"]+)\.(?=[^\]''"]+[''"]*\])'))
jpath = regexprep(jpath, '(\[[''"]*[^\]''"]+)\.(?=[^\]''"]+[''"]*\])', '$1_0x2E_');
end
[pat, paths] = regexp(jpath, '(\.{0,2}[^\.]+)', 'match', 'tokens');
if (~isempty(pat) && ~isempty(paths))
if (strcmp(paths{1}, '$'))
paths(1) = [];
end
if (isempty(varargin))
for i = 1:length(paths)
[obj, isfound] = getonelevel(obj, paths, i, varargin{:});
if (~isfound)
return
end
end
else
datastack = cell(1, length(paths) + 1);
datastack{1} = obj;
for i = 1:length(paths)
[datastack{i + 1}, isfound] = getonelevel(datastack{i}, paths, i, varargin{:});
if (~isfound)
error(['data with a jsonpath ' jpath ' does not exist']);
end
end
fieldname = paths{end}{1}(2:end);
datastack{end - 1} = setfield_safe(datastack{end - 1}, fieldname, varargin{1});
for i = length(paths) - 1:-1:1
fieldname = paths{i}{1}(2:end);
datastack{i} = setfield_safe(datastack{i}, fieldname, datastack{i + 1});
end
obj = datastack{1};
end
end
%% scan function
function [obj, isfound] = getonelevel(input, paths, pathid, varargin)
pathname = paths{pathid};
if (iscell(pathname))
pathname = pathname{1};
end
deepscan = ~isempty(regexp(pathname, '^\.\.', 'once'));
origpath = pathname;
pathname = regexprep(pathname, '^\.+', '');
if (strcmp(pathname, '$'))
obj = input;
elseif (regexp(pathname, '$\d+'))
obj = input(str2double(pathname(2:end)) + 1);
elseif (~isempty(regexp(pathname, '^\[[-0-9\*:]+\]$', 'once')) || iscell(input))
arraystr = pathname(2:end - 1);
if (find(arraystr == ':'))
arrayrange = regexp(arraystr, '(?<start>-*\d*):(?<end>-*\d*)', 'names');
if (~isempty(arrayrange.start))
arrayrange.start = str2double(arrayrange.start);
arrayrange.start = (arrayrange.start < 0) * length(input) + arrayrange.start + 1;
else
arrayrange.start = 1;
end
if (~isempty(arrayrange.end))
arrayrange.end = str2double(arrayrange.end);
arrayrange.end = (arrayrange.end < 0) * length(input) + arrayrange.end + 1;
else
arrayrange.end = length(input);
end
elseif (regexp(arraystr, '^[-0-9:]+', 'once'))
firstidx = str2double(arraystr);
if (firstidx < 0)
firstidx = length(input) + firstidx + 1;
else
firstidx = firstidx + 1;
end
arrayrange.start = firstidx;
arrayrange.end = firstidx;
elseif (~isempty(regexp(arraystr, '^\*', 'once')))
% do nothing
end
if (exist('arrayrange', 'var'))
obj = {input(arrayrange.start:arrayrange.end)};
else
arrayrange = struct('start', 1, 'end', length(input));
end
if (~exist('obj', 'var') && iscell(input))
input = {input{arrayrange.start:arrayrange.end}};
if (deepscan)
searchkey = ['..' pathname];
else
searchkey = origpath;
end
for idx = 1:length(input)
[val, isfound] = getonelevel(input{idx}, [paths{1:pathid - 1} {searchkey}], pathid);
if (isfound)
if (~exist('newobj', 'var'))
newobj = {};
end
newobj = [newobj(:)', val(:)'];
end
end
if (exist('newobj', 'var'))
obj = newobj;
end
if (exist('obj', 'var') && iscell(obj) && length(obj) == 1)
obj = obj{1};
end
else
obj = input(arrayrange.start:arrayrange.end);
end
elseif (isstruct(input) || isa(input, 'containers.Map') || isa(input, 'table') || isa(input, 'jdict'))
pathname = regexprep(pathname, '^\[(.*)\]$', '$1');
stpath = encodevarname(pathname);
if (isstruct(input))
if (isfield(input, stpath))
obj = {input.(stpath)};
end
elseif (isa(input, 'table'))
if (any(ismember(input.Properties.VariableNames, stpath)))
obj = {input.(stpath)};
end
else
if (isKey(input, pathname))
obj = {input(pathname)};
elseif (isKey(input, decodevarname(pathname)))
obj = {input(decodevarname(pathname))};
end
end
if (~exist('obj', 'var') || deepscan)
if (isa(input, 'containers.Map'))
items = keys(input);
else
items = fieldnames(input);
end
for idx = 1:length(items)
if (isa(input, 'containers.Map'))
[val, isfound] = getonelevel(input(items{idx}), [paths{1:pathid - 1} {['..' pathname]}], pathid);
elseif (isa(input, 'table'))
[val, isfound] = getonelevel({input.(items{idx})}, [paths{1:pathid - 1} {['..' pathname]}], pathid);
elseif (isstruct(input) && length(input) > 1) % struct array
[val, isfound] = getonelevel({input.(items{idx})}, [paths{1:pathid - 1} {['..' pathname]}], pathid);
else
[val, isfound] = getonelevel(input.(items{idx}), [paths{1:pathid - 1} {['..' pathname]}], pathid);
end
if (isfound)
if (~exist('obj', 'var'))
obj = {};
end
if (iscell(val))
obj = [obj(:)', val(:)'];
else
obj = [obj(:)', {val}];
end
end
end
if (exist('obj', 'var') && length(obj) == 1)
obj = obj{1};
end
end
if (exist('obj', 'var') && iscell(obj) && length(obj) == 1)
obj = obj{1};
end
elseif (~deepscan)
error('json path segment "%s" can not be found in the input object\n', pathname);
end
if (~exist('obj', 'var'))
isfound = false;
obj = [];
elseif (nargout > 1)
isfound = true;
end
function data = setfield_safe(data, fieldname, value)
if (isa(value, 'jdict') && ~isempty(value{'kind'}) && strcmp(value{'kind'}, '_deleted_'))
if isstruct(data) && isfield(data, fieldname)
data = rmfield(data, fieldname);
elseif (isa(data, 'containers.Map') || isa(data, 'dictionary')) && isKey(data, fieldname)
data = remove(data, fieldname);
else
idx = struct('type', '.', 'subs', fieldname);
data = subsasgn(data, idx, []);
end
else
if isstruct(data)
data.(fieldname) = value;
elseif isa(data, 'containers.Map') || isa(data, 'dictionary')
data(fieldname) = value;
else
idx = struct('type', '.', 'subs', fieldname);
data = subsasgn(data, idx, value);
end
end