|
18 | 18 | % "_ArrayZipType_", "_ArrayZipSize", "_ArrayZipData_" |
19 | 19 | % opt: (optional) a list of 'Param',value pairs for additional options |
20 | 20 | % The supported options include |
21 | | -% 'Recursive', if set to 1, will apply the conversion to |
| 21 | +% Recursive: [1|0] if set to 1, will apply the conversion to |
22 | 22 | % every child; 0 to disable |
23 | | -% 'Base64'. if set to 1, _ArrayZipData_ is assumed to |
| 23 | +% Base64: [0|1] if set to 1, _ArrayZipData_ is assumed to |
24 | 24 | % be encoded with base64 format and need to be |
25 | 25 | % decoded first. This is needed for JSON but not |
26 | 26 | % UBJSON data |
27 | | -% 'Prefix', for JData files loaded via loadjson/loadubjson, the |
| 27 | +% Prefix: ['x0x5F'|'x'] for JData files loaded via loadjson/loadubjson, the |
28 | 28 | % default JData keyword prefix is 'x0x5F'; if the |
29 | 29 | % json file is loaded using matlab2018's |
30 | 30 | % jsondecode(), the prefix is 'x'; this function |
31 | 31 | % attempts to automatically determine the prefix. |
32 | | -% 'FormatVersion' [2|float]: set the JSONLab output version; |
| 32 | +% FormatVersion: [2|float]: set the JSONLab output version; |
33 | 33 | % since v2.0, JSONLab uses JData specification Draft 1 |
34 | 34 | % for output format, it is incompatible with all |
35 | 35 | % previous releases; if old output is desired, |
|
51 | 51 | % |
52 | 52 |
|
53 | 53 | newdata=data; |
| 54 | + opt=varargin2struct(varargin{:}); |
| 55 | + |
| 56 | + %% process non-structure inputs |
54 | 57 | if(~isstruct(data)) |
55 | 58 | if(iscell(data)) |
56 | | - newdata=cellfun(@(x) jdatadecode(x),data,'UniformOutput',false); |
| 59 | + newdata=cellfun(@(x) jdatadecode(x,opt),data,'UniformOutput',false); |
| 60 | + elseif(isa(data,'containers.Map')) |
| 61 | + newdata=containers.Map('KeyType',data.KeyType,'ValueType','any'); |
| 62 | + names=data.keys; |
| 63 | + for i=1:length(names) |
| 64 | + newdata(names{i})=jdatadecode(data(names{i}),opt); |
| 65 | + end |
57 | 66 | end |
58 | 67 | return; |
59 | 68 | end |
| 69 | + |
| 70 | + %% assume the input is a struct below |
60 | 71 | fn=fieldnames(data); |
61 | 72 | len=length(data); |
62 | | - opt=varargin2struct(varargin{:}); |
63 | | - needbase64=jsonopt('Base64',1,opt); |
| 73 | + needbase64=jsonopt('Base64',0,opt); |
64 | 74 | format=jsonopt('FormatVersion',2,opt); |
65 | 75 | prefix=jsonopt('Prefix',sprintf('x0x%X','_'+0),opt); |
66 | | - if(isempty(strmatch(N_('_ArrayType_'),fn)) && ~isempty(strmatch('x_ArrayType_',fn))) |
| 76 | + if(~any(ismember(N_('_ArrayType_'),fn)) && any(ismember('x_ArrayType_',fn))) |
67 | 77 | prefix='x'; |
68 | 78 | opt.prefix='x'; |
69 | 79 | end |
70 | 80 |
|
| 81 | + %% recursively process subfields |
71 | 82 | if(jsonopt('Recursive',1,opt)==1) |
72 | 83 | for i=1:length(fn) % depth-first |
73 | 84 | for j=1:len |
74 | | - if(isstruct(data(j).(fn{i}))) |
| 85 | + if(isstruct(data(j).(fn{i})) || isa(data(j).(fn{i}),'containers.Map')) |
75 | 86 | newdata(j).(fn{i})=jdatadecode(data(j).(fn{i}),opt); |
76 | 87 | elseif(iscell(data(j).(fn{i}))) |
77 | 88 | newdata(j).(fn{i})=cellfun(@(x) jdatadecode(x,opt),newdata(j).(fn{i}),'UniformOutput',false); |
|
81 | 92 | end |
82 | 93 |
|
83 | 94 | %% handle array data |
84 | | - if(~isempty(strmatch(N_('_ArrayType_'),fn)) && (~isempty(strmatch(N_('_ArrayData_'),fn)) || ~isempty(strmatch(N_('_ArrayZipData_'),fn)))) |
| 95 | + if(any(ismember(N_('_ArrayType_'),fn)) && (any(ismember(N_('_ArrayData_'),fn)) || any(ismember(N_('_ArrayZipData_'),fn)))) |
85 | 96 | newdata=cell(len,1); |
86 | 97 | for j=1:len |
87 | | - if(~isempty(strmatch(N_('_ArrayZipSize_'),fn)) && ~isempty(strmatch(N_('_ArrayZipData_'),fn))) |
| 98 | + if(any(ismember(N_('_ArrayZipSize_'),fn)) && any(ismember(N_('_ArrayZipData_'),fn))) |
88 | 99 | zipmethod='zip'; |
89 | | - if(~isempty(strmatch(N_('_ArrayZipType_'),fn))) |
| 100 | + if(any(ismember(N_('_ArrayZipType_'),fn))) |
90 | 101 | zipmethod=data(j).(N_('_ArrayZipType_')); |
91 | 102 | end |
92 | | - if(~isempty(strmatch(zipmethod,{'zlib','gzip','lzma','lzip','lz4','lz4hc'}))) |
| 103 | + if(any(ismember(zipmethod,{'zlib','gzip','lzma','lzip','lz4','lz4hc'}))) |
93 | 104 | decompfun=str2func([zipmethod 'decode']); |
94 | 105 | if(needbase64) |
95 | 106 | ndata=reshape(typecast(decompfun(base64decode(data(j).(N_('_ArrayZipData_')))),data(j).(N_('_ArrayType_'))),data(j).(N_('_ArrayZipSize_'))(:)'); |
|
105 | 116 | end |
106 | 117 | ndata=cast(data(j).(N_('_ArrayData_')),char(data(j).(N_('_ArrayType_')))); |
107 | 118 | end |
108 | | - if(~isempty(strmatch(N_('_ArrayZipSize_'),fn))) |
| 119 | + if(any(ismember(N_('_ArrayZipSize_'),fn))) |
109 | 120 | ndata=reshape(ndata(:),fliplr(data(j).(N_('_ArrayZipSize_'))(:)')); |
110 | 121 | ndata=permute(ndata,ndims(ndata):-1:1); |
111 | 122 | end |
112 | 123 | iscpx=0; |
113 | | - if(~isempty(strmatch(N_('_ArrayIsComplex_'),fn))) |
| 124 | + if(any(ismember(N_('_ArrayIsComplex_'),fn))) |
114 | 125 | if(data(j).(N_('_ArrayIsComplex_'))) |
115 | 126 | iscpx=1; |
116 | 127 | end |
117 | 128 | end |
118 | | - if(~isempty(strmatch(N_('_ArrayIsSparse_'),fn)) && data(j).(N_('_ArrayIsSparse_'))) |
119 | | - if(~isempty(strmatch(N_('_ArraySize_'),fn))) |
| 129 | + if(any(ismember(N_('_ArrayIsSparse_'),fn)) && data(j).(N_('_ArrayIsSparse_'))) |
| 130 | + if(any(ismember(N_('_ArraySize_'),fn))) |
120 | 131 | dim=double(data(j).(N_('_ArraySize_'))(:)'); |
121 | 132 | if(iscpx) |
122 | 133 | ndata(end-1,:)=complex(ndata(end-1,:),ndata(end,:)); |
|
140 | 151 | end |
141 | 152 | ndata=sparse(ndata(1,:),ndata(2,:),ndata(3,:)); |
142 | 153 | end |
143 | | - elseif(~isempty(strmatch(N_('_ArraySize_'),fn))) |
| 154 | + elseif(any(ismember(N_('_ArraySize_'),fn))) |
144 | 155 | if(iscpx) |
145 | 156 | ndata=complex(ndata(1,:),ndata(2,:)); |
146 | 157 | end |
|
161 | 172 | end |
162 | 173 |
|
163 | 174 | %% handle table data |
164 | | - if(~isempty(strmatch(N_('_TableRecords_'),fn))) |
| 175 | + if(any(ismember(N_('_TableRecords_'),fn))) |
165 | 176 | newdata=cell(len,1); |
166 | 177 | for j=1:len |
167 | 178 | ndata=data(j).(N_('_TableRecords_')); |
|
191 | 202 | end |
192 | 203 |
|
193 | 204 | %% handle map data |
194 | | - if(~isempty(strmatch(N_('_MapData_'),fn))) |
| 205 | + if(any(ismember(N_('_MapData_'),fn))) |
195 | 206 | newdata=cell(len,1); |
196 | 207 | for j=1:len |
197 | | - key={}; |
198 | | - val={}; |
| 208 | + key=cell(1,length(data(j).(N_('_MapData_')))); |
| 209 | + val=cell(size(key)); |
199 | 210 | for k=1:length(data(j).(N_('_MapData_'))) |
200 | 211 | key{k}=data(j).(N_('_MapData_')){k}{1}; |
201 | | - val{k}=data(j).(N_('_MapData_')){k}{2}; |
| 212 | + val{k}=jdatadecode(data(j).(N_('_MapData_')){k}{2},opt); |
202 | 213 | end |
203 | 214 | ndata=containers.Map(key,val); |
204 | 215 | newdata{j}=ndata; |
|
207 | 218 | newdata=newdata{1}; |
208 | 219 | end |
209 | 220 | end |
| 221 | + |
| 222 | + %% handle graph data |
| 223 | + if(any(ismember(N_('_GraphNodes_'),fn)) && exist('graph','file') && exist('digraph','file')) |
| 224 | + newdata=cell(len,1); |
| 225 | + isdirected=1; |
| 226 | + for j=1:len |
| 227 | + nodedata=data(j).(N_('_GraphNodes_')); |
| 228 | + if(isstruct(nodedata)) |
| 229 | + nodetable=struct2table(nodedata); |
| 230 | + elseif(isa(nodedata,'containers.Map')) |
| 231 | + nodetable=[keys(nodedata);values(nodedata)]; |
| 232 | + if(strcmp(nodedata.KeyType,'char')) |
| 233 | + nodetable=table(nodetable(1,:)',nodetable(2,:)','VariableNames',{'Name','Data'}); |
| 234 | + else |
| 235 | + nodetable=table(nodetable(2,:)','VariableNames',{'Data'}); |
| 236 | + end |
| 237 | + else |
| 238 | + nodetable=table; |
| 239 | + end |
| 240 | + |
| 241 | + if(any(ismember(N_('_GraphEdges_'),fn))) |
| 242 | + edgedata=data(j).(N_('_GraphEdges_')); |
| 243 | + elseif(any(ismember(N_('_GraphEdges0_'),fn))) |
| 244 | + edgedata=data(j).(N_('_GraphEdges0_')); |
| 245 | + isdirected=0; |
| 246 | + elseif(any(ismember(N_('_GraphMatrix_'),fn))) |
| 247 | + edgedata=jdatadecode(data(j).(N_('_GraphMatrix_')),varargin{:}); |
| 248 | + end |
| 249 | + |
| 250 | + if(exist('edgedata','var')) |
| 251 | + if(iscell(edgedata)) |
| 252 | + endnodes=edgedata(:,1:2); |
| 253 | + endnodes=reshape([endnodes{:}],size(edgedata,1),2); |
| 254 | + weight=cell2mat(edgedata(:,3:end)); |
| 255 | + edgetable=table(endnodes,[weight.Weight]','VariableNames',{'EndNodes','Weight'}); |
| 256 | + |
| 257 | + if(isdirected) |
| 258 | + newdata{j}=digraph(edgetable,nodetable); |
| 259 | + else |
| 260 | + newdata{j}=graph(edgetable,nodetable); |
| 261 | + end |
| 262 | + elseif(ismatrix(edgedata) && isstruct(nodetable)) |
| 263 | + newdata{j}=digraph(edgedata,fieldnames(nodetable)); |
| 264 | + end |
| 265 | + end |
| 266 | + end |
| 267 | + if(len==1) |
| 268 | + newdata=newdata{1}; |
| 269 | + end |
| 270 | + end |
| 271 | + |
| 272 | + %% handle bytestream and arbitrary matlab objects |
| 273 | + if(sum(ismember({N_('_ByteStream_'),N_('_DataInfo_')},fn))==2) |
| 274 | + newdata=cell(len,1); |
| 275 | + for j=1:len |
| 276 | + if(isfield(data(j).(N_('_DataInfo_')),'MATLABObjectClass')) |
| 277 | + if(needbase64) |
| 278 | + newdata{j}=getArrayFromByteStream(base64decode(data(j).(N_('_ByteStream_')))); |
| 279 | + else |
| 280 | + newdata{j}=getArrayFromByteStream(data(j).(N_('_ByteStream_'))); |
| 281 | + end |
| 282 | + end |
| 283 | + end |
| 284 | + if(len==1) |
| 285 | + newdata=newdata{1}; |
| 286 | + end |
| 287 | + end |
| 288 | + |
210 | 289 | %% subfunctions |
211 | 290 | function escaped=N_(str) |
212 | 291 | escaped=[prefix str]; |
|
0 commit comments