|
12 | 12 | % |
13 | 13 | % input: |
14 | 14 | % data: a structure (array) or cell (array) to be encoded. |
15 | | -% options: (optional) a struct or Param/value pairs for user specified options |
| 15 | +% options: (optional) a struct or Param/value pairs for user |
| 16 | +% specified options (first in [.|.] is the default) |
| 17 | +% Base64: [0|1] if set to 1, _ArrayZipData_ is assumed to |
| 18 | +% be encoded with base64 format and need to be |
| 19 | +% decoded first. This is needed for JSON but not |
| 20 | +% UBJSON data |
| 21 | +% Prefix: [x0x5F|x] for JData files loaded via loadjson/loadubjson, the |
| 22 | +% default JData keyword prefix is 'x0x5F'(default); |
| 23 | +% if the json file is loaded using matlab2018's |
| 24 | +% jsondecode(), the prefix is 'x'. |
| 25 | +% UseArrayZipSize: [1|0] if set to 1, _ArrayZipSize_ will be added to |
| 26 | +% store the "pre-processed" data dimensions, i.e. |
| 27 | +% the original data stored in _ArrayData_, and then flaten |
| 28 | +% _ArrayData_ into a row vector using row-major |
| 29 | +% order; if set to 0, a 2D _ArrayData_ will be used |
| 30 | +% MapAsStruct: [0|1] if set to 1, convert containers.Map into |
| 31 | +% struct; otherwise, keep it as map |
16 | 32 | % Compression: ['zlib'|'gzip','lzma','lz4','lz4hc'] - use zlib method |
17 | 33 | % to compress data array |
18 | 34 | % CompressArraySize: [100|int]: only to compress an array if the |
|
39 | 55 | jdata=obj2jd(data,opt); |
40 | 56 |
|
41 | 57 | %%------------------------------------------------------------------------- |
42 | | -function newitem=obj2jd( item,varargin) |
| 58 | +function newitem=obj2jd(item,varargin) |
43 | 59 |
|
44 | 60 | if(iscell(item)) |
45 | 61 | newitem=cell2jd(item,varargin{:}); |
|
51 | 67 | newitem=map2jd(item,varargin{:}); |
52 | 68 | elseif(isa(item,'categorical')) |
53 | 69 | newitem=cell2jd(cellstr(item),varargin{:}); |
| 70 | +elseif(isa(item,'function_handle')) |
| 71 | + newitem=struct2jd(functions(item),varargin{:}); |
54 | 72 | elseif(islogical(item) || isnumeric(item)) |
55 | 73 | newitem=mat2jd(item,varargin{:}); |
56 | 74 | elseif(isa(item,'table')) |
|
88 | 106 | function newitem=map2jd(item,varargin) |
89 | 107 |
|
90 | 108 | names=item.keys; |
91 | | -newitem=containers.Map; |
92 | | -for i=1:length(names) |
93 | | - newitem(names{i})=obj2jd(item(names{i}),varargin{:}); |
| 109 | +if(jsonopt('MapAsStruct',0,varargin{:})) |
| 110 | + newitem=struct; |
| 111 | + for i=1:length(names) |
| 112 | + newitem(N_(names{i},varargin{:}))=obj2jd(item(names{i}),varargin{:}); |
| 113 | + end |
| 114 | +else |
| 115 | + newitem=containers.Map; |
| 116 | + for i=1:length(names) |
| 117 | + newitem(names{i})=obj2jd(item(names{i}),varargin{:}); |
| 118 | + end |
94 | 119 | end |
95 | | - |
96 | 120 | %%------------------------------------------------------------------------- |
97 | 121 | function newitem=mat2jd(item,varargin) |
98 | 122 | if(isempty(item) || isa(item,'string') || ischar(item) || (isvector(item) && isreal(item) && ~issparse(item))) |
|
107 | 131 | item=uint8(item); |
108 | 132 | end |
109 | 133 |
|
110 | | -newitem=struct(N_('_ArraySize_'),size(item),N_('_ArrayType_'),class(item)); |
| 134 | +N=@(x) N_(x,varargin{:}); |
| 135 | + |
| 136 | +newitem=struct(N('_ArraySize_'),size(item),N('_ArrayType_'),class(item)); |
111 | 137 |
|
112 | 138 | if(isreal(item)) |
113 | 139 | if(issparse(item)) |
114 | 140 | fulldata=full(item(find(item))); |
115 | | - newitem.(N_('_ArrayIsSparse_'))=true; |
116 | | - newitem.(N_('_ArrayZipSize_'))=[2+(~isvector(item)),length(fulldata)]; |
| 141 | + newitem.(N('_ArrayIsSparse_'))=true; |
| 142 | + newitem.(N('_ArrayZipSize_'))=[2+(~isvector(item)),length(fulldata)]; |
117 | 143 | if(isvector(item)) |
118 | | - newitem.(N_('_ArrayData_'))=[find(item)', fulldata(:)']; |
| 144 | + newitem.(N('_ArrayData_'))=[find(item)', fulldata(:)']; |
119 | 145 | else |
120 | 146 | [ix,iy]=find(item); |
121 | | - newitem.(N_('_ArrayData_'))=[ix(:)' , iy(:)', fulldata(:)']; |
| 147 | + newitem.(N('_ArrayData_'))=[ix(:)' , iy(:)', fulldata(:)']; |
122 | 148 | end |
123 | 149 | else |
124 | 150 | item=permute(item,ndims(item):-1:1); |
125 | | - newitem.(N_('_ArrayData_'))=item(:)'; |
| 151 | + newitem.(N('_ArrayData_'))=item(:)'; |
126 | 152 | end |
127 | 153 | else |
128 | | - newitem.(N_('_ArrayIsComplex_'))=true; |
| 154 | + newitem.(N('_ArrayIsComplex_'))=true; |
129 | 155 | if(issparse(item)) |
130 | 156 | fulldata=full(item(find(item))); |
131 | | - newitem.(N_('_ArrayIsSparse_'))=true; |
132 | | - newitem.(N_('_ArrayZipSize_'))=[3+(~isvector(item)),length(fulldata)]; |
| 157 | + newitem.(N('_ArrayIsSparse_'))=true; |
| 158 | + newitem.(N('_ArrayZipSize_'))=[3+(~isvector(item)),length(fulldata)]; |
133 | 159 | if(isvector(item)) |
134 | | - newitem.(N_('_ArrayData_'))=[find(item)', real(fulldata(:)'), imag(fulldata(:)')]; |
| 160 | + newitem.(N('_ArrayData_'))=[find(item)', real(fulldata(:)'), imag(fulldata(:)')]; |
135 | 161 | else |
136 | 162 | [ix,iy]=find(item); |
137 | | - newitem.(N_('_ArrayData_'))=[ix(:)' , iy(:)' , real(fulldata(:)'), imag(fulldata(:)')]; |
| 163 | + newitem.(N('_ArrayData_'))=[ix(:)' , iy(:)' , real(fulldata(:)'), imag(fulldata(:)')]; |
138 | 164 | end |
139 | 165 | else |
140 | | - newitem.(N_('_ArrayZipSize_'))=[2,numel(item)]; |
141 | | - newitem.(N_('_ArrayData_'))=[real(item(:)'), imag(item(:)')]; |
| 166 | + newitem.(N('_ArrayZipSize_'))=[2,numel(item)]; |
| 167 | + newitem.(N('_ArrayData_'))=[real(item(:)'), imag(item(:)')]; |
142 | 168 | end |
143 | 169 | end |
144 | 170 |
|
| 171 | +if(jsonopt('UseArrayZipSize',1,varargin{:})==0) |
| 172 | + data=newitem.(N('_ArrayData_')); |
| 173 | + data=reshape(data,fliplr(newitem.(N('_ArrayZipSize_')))); |
| 174 | + newitem.(N('_ArrayData_'))=permute(data,ndims(data):-1:1); |
| 175 | +end |
145 | 176 | if(~isempty(zipmethod) && numel(item)>minsize) |
146 | 177 | compfun=str2func([zipmethod 'encode']); |
147 | | - newitem.(N_('_ArrayZipType_'))=lower(zipmethod); |
148 | | - newitem.(N_('_ArrayZipSize_'))=size(newitem.(N_('_ArrayData_'))); |
149 | | - newitem.(N_('_ArrayZipData_'))=compfun(typecast(newitem.(N_('_ArrayData_')),'uint8')); |
| 178 | + newitem.(N('_ArrayZipType_'))=lower(zipmethod); |
| 179 | + newitem.(N('_ArrayZipSize_'))=size(newitem.(N('_ArrayData_'))); |
| 180 | + newitem.(N('_ArrayZipData_'))=compfun(typecast(newitem.(N('_ArrayData_')),'uint8')); |
150 | 181 | newitem=rmfield(newitem,N_('_ArrayData_')); |
151 | | - if(jsonopt('Base64',1,varargin{:})) |
152 | | - newitem.(N_('_ArrayZipData_'))=char(base64encode(newitem.(N_('_ArrayZipData_')))); |
| 182 | + if(jsonopt('Base64',0,varargin{:})) |
| 183 | + newitem.(N('_ArrayZipData_'))=char(base64encode(newitem.(N('_ArrayZipData_')))); |
153 | 184 | end |
154 | 185 | end |
155 | 186 |
|
156 | 187 | %%------------------------------------------------------------------------- |
157 | 188 | function newitem=table2jd(item,varargin) |
158 | 189 | newitem=struct; |
159 | | -newitem(N_('_TableRows_'))=item.Properties.RowNames'; |
160 | | -newitem(N_('_TableCols_'))=item.Properties.VariableNames; |
161 | | -newitem(N_('_TableRecords_'))=table2cell(item); |
| 190 | +newitem(N('_TableRows_',varargin{:}))=item.Properties.RowNames'; |
| 191 | +newitem(N('_TableCols_',varargin{:}))=item.Properties.VariableNames; |
| 192 | +newitem(N('_TableRecords_',varargin{:}))=table2cell(item); |
162 | 193 |
|
163 | 194 | %%------------------------------------------------------------------------- |
164 | 195 | function newitem=graph2jd(item,varargin) |
165 | 196 | newitem=struct; |
166 | 197 | nodedata=table2struct(item.Nodes); |
167 | 198 | if(isfield(nodedata,'Name')) |
168 | 199 | nodedata=rmfield(nodedata,'Name'); |
169 | | - newitem.(N_('_GraphNodes_'))=containers.Map(item.Nodes.Name,num2cell(nodedata),'uniformValues',false); |
| 200 | + newitem.(N_('_GraphNodes_',varargin{:}))=containers.Map(item.Nodes.Name,num2cell(nodedata),'uniformValues',false); |
170 | 201 | else |
171 | | - newitem.(N_('_GraphNodes_'))=containers.Map(1:max(item.Edges.EndNodes(:)),num2cell(nodedata),'uniformValues',false); |
| 202 | + newitem.(N_('_GraphNodes_',varargin{:}))=containers.Map(1:max(item.Edges.EndNodes(:)),num2cell(nodedata),'uniformValues',false); |
172 | 203 | end |
173 | 204 | edgenodes=item.Edges.EndNodes; |
174 | 205 | edgedata=table2struct(item.Edges); |
175 | 206 | if(isfield(edgedata,'EndNodes')) |
176 | 207 | edgedata=rmfield(edgedata,'EndNodes'); |
177 | 208 | end |
178 | 209 | edgenodes(:,3)=num2cell(edgedata); |
179 | | -newitem.(N_('_GraphEdges_'))=edgenodes; |
| 210 | +newitem.(N_('_GraphEdges_',varargin{:}))=edgenodes; |
| 211 | + |
| 212 | +%%------------------------------------------------------------------------- |
| 213 | +function newitem=any2jd(item,varargin) |
| 214 | +newitem.(N_('_DataInfo_',varargin{:}))=struct('MATLABObjectClass',class(item),'MATLABObjectSize',size(item)); |
| 215 | +newitem.(N_('_ByteStream_',varargin{:}))=getByteStreamFromArray(item); % use undocumented matlab function |
180 | 216 |
|
181 | 217 | %%------------------------------------------------------------------------- |
182 | 218 | function newname=N_(name,varargin) |
|
0 commit comments