forked from NeuroJSON/jsonlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdatadecode.m
More file actions
230 lines (225 loc) · 9.17 KB
/
jdatadecode.m
File metadata and controls
230 lines (225 loc) · 9.17 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
226
227
228
229
230
function newdata=jdatadecode(data,varargin)
%
% newdata=jdatadecode(data,opt,...)
%
% Convert all JData object (in the form of a struct array) into an array
% (accepts JData objects loaded from either loadjson/loadubjson or
% jsondecode for MATLAB R2018a or later)
%
% authors:Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% data: a struct array. If data contains JData keywords in the first
% level children, these fields are parsed and regrouped into a
% data object (arrays, trees, graphs etc) based on JData
% specification. The JData keywords are
% "_ArrayType_", "_ArraySize_", "_ArrayData_"
% "_ArrayIsSparse_", "_ArrayIsComplex_",
% "_ArrayZipType_", "_ArrayZipSize", "_ArrayZipData_"
% opt: (optional) a list of 'Param',value pairs for additional options
% The supported options include
% 'Recursive', if set to 1, will apply the conversion to
% every child; 0 to disable
% 'Base64'. if set to 1, _ArrayZipData_ is assumed to
% be encoded with base64 format and need to be
% decoded first. This is needed for JSON but not
% UBJSON data
% 'Prefix', for JData files loaded via loadjson/loadubjson, the
% default JData keyword prefix is 'x0x5F'; if the
% json file is loaded using matlab2018's
% jsondecode(), the prefix is 'x'; this function
% attempts to automatically determine the prefix.
% 'FormatVersion' [2|float]: set the JSONLab output version;
% since v2.0, JSONLab uses JData specification Draft 1
% for output format, it is incompatible with all
% previous releases; if old output is desired,
% please set FormatVersion to 1
%
% output:
% newdata: the covnerted data if the input data does contain a JData
% structure; otherwise, the same as the input.
%
% examples:
% obj=struct('_ArrayType_','double','_ArraySize_',[2 3],
% '_ArrayIsSparse_',1 ,'_ArrayData_',null);
% jdata=jdatadecode(obj);
%
% 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)
%
newdata=data;
if(~isstruct(data))
if(iscell(data))
newdata=cellfun(@(x) jdatadecode(x),data,'UniformOutput',false);
end
return;
end
fn=fieldnames(data);
len=length(data);
opt=varargin2struct(varargin{:});
needbase64=jsonopt('Base64',1,opt);
format=jsonopt('FormatVersion',2,opt);
prefix=jsonopt('Prefix',sprintf('x0x%X','_'+0),opt);
if(isempty(strmatch(N_('_ArrayType_'),fn)) && ~isempty(strmatch('x_ArrayType_',fn)))
prefix='x';
opt.prefix='x';
end
if(jsonopt('Recursive',1,opt)==1)
for i=1:length(fn) % depth-first
for j=1:len
if(isstruct(data(j).(fn{i})))
newdata(j).(fn{i})=jdatadecode(data(j).(fn{i}),opt);
elseif(iscell(data(j).(fn{i})))
newdata(j).(fn{i})=cellfun(@(x) jdatadecode(x,opt),newdata(j).(fn{i}),'UniformOutput',false);
end
end
end
end
%% handle array data
if(~isempty(strmatch(N_('_ArrayType_'),fn)) && (~isempty(strmatch(N_('_ArrayData_'),fn)) || ~isempty(strmatch(N_('_ArrayZipData_'),fn))))
newdata=cell(len,1);
for j=1:len
if(~isempty(strmatch(N_('_ArrayZipSize_'),fn)) && ~isempty(strmatch(N_('_ArrayZipData_'),fn)))
zipmethod='zip';
if(~isempty(strmatch(N_('_ArrayZipType_'),fn)))
zipmethod=data(j).(N_('_ArrayZipType_'));
end
if(~isempty(strmatch(zipmethod,{'zlib','gzip','lzma','lzip'})))
decompfun=str2func([zipmethod 'decode']);
if(needbase64)
ndata=reshape(typecast(decompfun(base64decode(data(j).(N_('_ArrayZipData_')))),data(j).(N_('_ArrayType_'))),data(j).(N_('_ArrayZipSize_'))(:)');
else
ndata=reshape(typecast(decompfun(data(j).(N_('_ArrayZipData_'))),data(j).(N_('_ArrayType_'))),data(j).(N_('_ArrayZipSize_'))(:)');
end
else
error('compression method is not supported');
end
else
if(iscell(data(j).(N_('_ArrayData_'))))
data(j).(N_('_ArrayData_'))=cell2mat(cellfun(@(x) double(x(:)),data(j).(N_('_ArrayData_')),'uniformoutput',0));
end
ndata=cast(data(j).(N_('_ArrayData_')),data(j).(N_('_ArrayType_')));
end
iscpx=0;
needtranspose=0;
if(~isempty(strmatch(N_('_ArrayIsComplex_'),fn)))
if(data(j).(N_('_ArrayIsComplex_')))
iscpx=1;
needtranspose=islogical(data(j).(N_('_ArrayIsComplex_')));
end
end
if(~isempty(strmatch(N_('_ArrayIsSparse_'),fn)) && data(j).(N_('_ArrayIsSparse_')))
if(islogical(data(j).(N_('_ArrayIsSparse_'))))
needtranspose=1;
end
if(~isempty(strmatch(N_('_ArraySize_'),fn)))
dim=double(data(j).(N_('_ArraySize_'))(:)');
if(iscpx && size(ndata,2)==4-any(dim==1))
ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));
end
if isempty(ndata)
% All-zeros sparse
ndata=sparse(dim(1),prod(dim(2:end)));
elseif dim(1)==1
% Sparse row vector
if(size(ndata,2)~=2 && size(ndata,1)==2)
ndata=ndata';
end
ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));
elseif dim(2)==1
% Sparse column vector
if(size(ndata,2)~=2 && size(ndata,1)==2)
ndata=ndata';
end
ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));
else
% Generic sparse array.
if(size(ndata,2)~=3 && size(ndata,1)==3)
ndata=ndata';
end
ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));
end
else
if(iscpx && size(ndata,2)==4)
ndata(:,3)=complex(ndata(:,3),ndata(:,4));
end
ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));
end
elseif(~isempty(strmatch(N_('_ArraySize_'),fn)))
if(needtranspose)
ndata=ndata';
end
if(iscpx)
if(size(ndata,2)~=2 && size(ndata,1)==2)
ndata=ndata';
end
ndata=complex(ndata(:,1),ndata(:,2));
end
if(format>1.9)
data(j).(N_('_ArraySize_'))=data(j).(N_('_ArraySize_'))(end:-1:1);
end
dims=data(j).(N_('_ArraySize_'))(:)';
ndata=reshape(ndata(:),dims(:)');
if(format>1.9)
ndata=permute(ndata,ndims(ndata):-1:1);
end
end
newdata{j}=ndata;
end
if(len==1)
newdata=newdata{1};
end
end
%% handle table data
if(~isempty(strmatch(N_('_TableRecords_'),fn)))
newdata=cell(len,1);
for j=1:len
ndata=data(j).(N_('_TableRecords_'));
if(iscell(ndata))
rownum=length(ndata);
colnum=length(ndata{1});
nd=cell(rownum, colnum);
for i1=1:rownum;
for i2=1:colnum
nd{i1,i2}=ndata{i1}{i2};
end
end
newdata{j}=cell2table(nd);
else
newdata{j}=array2table(ndata');
end
if(isfield(data(j),N_('_TableRows_'))&& ~isempty(data(j).(N_('_TableRows_'))))
newdata{j}.Properties.RowNames=data(j).(N_('_TableRows_'))(:);
end
if(isfield(data(j),N_('_TableCols_')) && ~isempty(data(j).(N_('_TableCols_'))))
newdata{j}.Properties.VariableNames=data(j).(N_('_TableCols_'));
end
end
if(len==1)
newdata=newdata{1};
end
end
%% handle map data
if(~isempty(strmatch(N_('_MapData_'),fn)))
newdata=cell(len,1);
for j=1:len
key={};
val={};
for k=1:length(data(j).(N_('_MapData_')))
key{k}=data(j).(N_('_MapData_')){k}{1};
val{k}=data(j).(N_('_MapData_')){k}{2};
end
ndata=containers.Map(key,val);
newdata{j}=ndata;
end
if(len==1)
newdata=newdata{1};
end
end
%% subfunctions
function escaped=N_(str)
escaped=[prefix str];
end
end