Repository navigation
Expand file tree
/
Copy pathjdreplace.m
More file actions
219 lines (201 loc) · 7.31 KB
/
Copy pathjdreplace.m
File metadata and controls
219 lines (201 loc) · 7.31 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
function data = jdreplace(data, varargin)
%
% data = jdreplace(roots)
% or
% data = jdreplace(roots, 'SimplifyCell', 1, 'SimplifyCellArray', 0, ...)
%
% Put back the values that in-place updates moved out of a JData file: a value (or an array
% chunk) that no longer fitted its place was appended after the document as a replacement record,
%
% {"_DataInfo_": {"_DataReplaces_": "$.path.of.its.place"}, "_DataRecord_": value}
%
% and its place holds a stub, {"_DataReplace_": "$k"}, naming the k-th root after the document.
% Each stub is replaced by its record's value, after checking that the record names the stub's
% own path back (an error otherwise), and the records are removed from the roots: they are not
% documents.
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% roots: the cell array of roots that loadjson/loadbj return for a file holding several
% (the JSON-Mmap table, the document, then any records); any other input is
% returned unchanged
%
% options: SimplifyCell, SimplifyCellArray (as for loadjson/loadbj, which pass theirs):
% a list that held a stub is simplified once its stub is replaced, as the loader
% would have simplified it had the value been in place
%
% output:
% data: the roots with the stubs in the document replaced and the records removed (a single
% remaining root is returned by itself, as loadjson/loadbj do)
%
% examples:
% data = jdreplace(loadbj('volume.pmat', 'JDataDecode', 0));
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%
if (~iscell(data) || numel(data) < 2)
return
end
isrec = cellfun(@isrecord, data);
if (~any(isrec))
return
end
% the document: the first root that is neither the JSON-Mmap table nor a record; "$k" is the
% k-th root after it
doc = find(~isrec & ~cellfun(@istable, data), 1);
if (isempty(doc))
return
end
opt = varargin2struct(varargin{:});
if (isfield(opt, 'simplifycell'))
simp = [opt.simplifycell, opt.simplifycellarray];
else
simp = [jsonopt('SimplifyCell', 1, opt), jsonopt('SimplifyCellArray', 0, opt)];
end
data{doc} = walk(data{doc}, '$', data, doc, simp);
data = data(~isrec);
if (numel(data) == 1)
data = data{1};
end
%% -------------------------------------------------------------------------
function v = walk(v, path, roots, doc, simp)
% the stubs inside v (whose path is path) replaced
if (isstruct(v))
if (numel(v) == 1 && isstub(v))
v = resolve(v, path, roots, doc);
return
end
names = fieldnames(v);
if (numel(v) > 1)
% a list of objects read as a struct array: element by element (as a cell when a stub
% becomes a value of another kind)
c = num2cell(v);
changed = false;
for j = 1:numel(c)
[c{j}, d] = walkone(c{j}, sprintf('%s[%d]', path, j - 1), roots, doc, simp);
changed = changed || d;
end
if (changed)
try
v = reshape([c{:}], size(v));
catch
v = c;
end
end
return
end
for k = 1:numel(names)
sub = v.(names{k});
if (isstruct(sub) || iscell(sub) || isa(sub, 'containers.Map'))
v.(names{k}) = walk(sub, member(path, decodevarname(names{k})), roots, doc, simp);
end
end
elseif (iscell(v))
stubbed = false;
for j = 1:numel(v)
if (isstruct(v{j}) || iscell(v{j}) || isa(v{j}, 'containers.Map'))
stubbed = stubbed || (isstruct(v{j}) && numel(v{j}) == 1 && isstub(v{j}));
v{j} = walk(v{j}, sprintf('%s[%d]', path, j - 1), roots, doc, simp);
end
end
if (stubbed)
v = simplify(v, simp);
end
elseif (isa(v, 'containers.Map'))
ks = keys(v);
for k = 1:numel(ks)
sub = v(ks{k});
if (isstruct(sub) || iscell(sub) || isa(sub, 'containers.Map'))
v(ks{k}) = walk(sub, member(path, char(ks{k})), roots, doc, simp);
end
end
end
function [v, changed] = walkone(v, path, roots, doc, simp)
before = v;
v = walk(v, path, roots, doc, simp);
changed = ~isequal(before, v);
function v = resolve(stub, path, roots, doc)
ref = field(stub, '_DataReplace_');
if (~ischar(ref) || isempty(regexp(ref, '^\$[0-9]+$', 'once')))
error('jsonlab:jdreplace', '_DataReplace_ at %s must hold "$k" (the k-th root after the document)', path);
end
k = doc + str2double(ref(2:end));
if (k > numel(roots) || ~isrecord(roots{k}))
error('jsonlab:jdreplace', 'the _DataReplace_ at %s names no record', path);
end
rec = roots{k};
of = field(field(rec, '_DataInfo_'), '_DataReplaces_');
if (~strcmp(of, path))
error('jsonlab:jdreplace', 'the _DataReplace_ at %s names a record that replaces %s', path, of);
end
v = field(rec, '_DataRecord_');
if (isstruct(v) && numel(v) == 1 && isstub(v))
error('jsonlab:jdreplace', 'the record replacing %s is itself a stub', path);
end
function object = simplify(object, simp)
% a list as loadjson/loadbj simplify one when it is read (simp: SimplifyCell, SimplifyCellArray)
if (~simp(1) || isempty(object))
return
end
if (all(cellfun(@(e) isnumeric(e) && isscalar(e) && isa(e, 'double'), object)))
object = [object{:}]; % a list of numbers: a row, as the parser reads one
return
end
if (all(cellfun('isclass', object, 'double')) || all(cellfun('isclass', object, 'struct')))
if (all(cellfun(@(e) isequal(size(object{1}), size(e)), object(2:end))))
try
oldobj = object;
if (length(object) > 1 && ndims(object{1}) >= 2)
catdim = size(object{1});
catdim = ndims(object{1}) - (catdim(end) == 1) + 1;
object = cat(catdim, object{:});
object = permute(object, ndims(object):-1:1);
else
object = cell2mat(object.')';
end
if (isstruct(object) && numel(object) > 1 && simp(2) == 0)
object = oldobj;
end
catch
end
end
end
function s = member(base, key)
% a member's path, as JSON-Mmap writes it: $.name, or $['name'] for names with . [ ] ' \
if (~isempty(key) && isempty(regexp(key, '[.\[\]''\\]', 'once')))
s = [base '.' key];
else
s = [base '[''' regexprep(key, '([''\\])', '\\$1') ''']'];
end
function tf = isstub(v)
names = fieldnames(v);
tf = numel(names) == 1 && strcmp(decodevarname(names{1}), '_DataReplace_');
function tf = isrecord(v)
tf = false;
if (~isstruct(v) || numel(v) ~= 1 || numel(fieldnames(v)) ~= 2)
return
end
info = field(v, '_DataInfo_');
tf = isstruct(info) && ischar(field(info, '_DataReplaces_')) && hasfield(v, '_DataRecord_');
function tf = istable(v)
% a JSON-Mmap table: a list of [name, value] records, the first ["MmapVersion", ...]
tf = iscell(v) && ~isempty(v) && iscell(v{1}) && numel(v{1}) == 2 && ischar(v{1}{1}) && strcmp(v{1}{1}, 'MmapVersion');
function tf = hasfield(s, name)
tf = isstruct(s) && any(strcmp(cellfun(@decodevarname, fieldnames(s), 'UniformOutput', false), name));
function v = field(s, name)
% a JData keyword's member, however the loader spelled its name (_x_, x0x5F_x_, ...)
v = [];
if (~isstruct(s))
return
end
names = fieldnames(s);
for k = 1:numel(names)
if (strcmp(decodevarname(names{k}), name))
v = s.(names{k});
return
end
end