|
17 | 17 | % rootname: the name of the root-object, when set to '', the root name |
18 | 18 | % is ignored, however, when opt.ForceRootName is set to 1 (see below), |
19 | 19 | % the MATLAB variable name will be used as the root name. |
20 | | -% obj: a MATLAB object (array, cell, cell array, struct, struct array) |
| 20 | +% obj: a MATLAB object (array, cell, cell array, struct, struct array, |
| 21 | +% class instance) |
21 | 22 | % filename: a string for the file name to save the output UBJSON data |
22 | 23 | % opt: a struct for additional options, ignore to use default values. |
23 | 24 | % opt can have the following fields (first in [.|.] is the default) |
|
92 | 93 | rootisarray=0; |
93 | 94 | rootlevel=1; |
94 | 95 | forceroot=jsonopt('ForceRootName',0,opt); |
95 | | -if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) |
| 96 | +if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || ... |
| 97 | + iscell(obj) || isobject(obj)) && isempty(rootname) && forceroot==0) |
96 | 98 | rootisarray=1; |
97 | 99 | rootlevel=0; |
98 | 100 | else |
|
129 | 131 | txt=struct2ubjson(name,item,level,varargin{:}); |
130 | 132 | elseif(ischar(item)) |
131 | 133 | txt=str2ubjson(name,item,level,varargin{:}); |
| 134 | +elseif(isobject(item)) |
| 135 | + txt=matlabobject2ubjson(name,item,level,varargin{:}); |
132 | 136 | else |
133 | 137 | txt=mat2ubjson(name,item,level,varargin{:}); |
134 | 138 | end |
|
328 | 332 | end |
329 | 333 | txt=[txt,'}']; |
330 | 334 |
|
| 335 | +%%------------------------------------------------------------------------- |
| 336 | +function txt=matlabobject2ubjson(name,item,level,varargin) |
| 337 | +if numel(item) == 0 %empty object |
| 338 | + st = struct(); |
| 339 | +else |
| 340 | + % "st = struct(item);" would produce an inmutable warning, because it |
| 341 | + % make the protected and private properties visible. Instead we get the |
| 342 | + % visible properties |
| 343 | + propertynames = properties(item); |
| 344 | + for p = 1:numel(propertynames) |
| 345 | + for o = numel(item):-1:1 % aray of objects |
| 346 | + st(o).(propertynames{p}) = item(o).(propertynames{p}); |
| 347 | + end |
| 348 | + end |
| 349 | +end |
| 350 | +txt=struct2ubjson(name,st,level,varargin{:}); |
| 351 | + |
331 | 352 | %%------------------------------------------------------------------------- |
332 | 353 | function txt=matdata2ubjson(mat,level,varargin) |
333 | 354 | if(isempty(mat)) |
|
0 commit comments