Skip to content

Commit 8e3a395

Browse files
committed
Merge pull request #11 from sertansenturk/obj2json
Obj2json for ubjson
2 parents 57d4523 + 7593376 commit 8e3a395

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

savejson.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
% rootname: the name of the root-object, when set to '', the root name
1818
% is ignored, however, when opt.ForceRootName is set to 1 (see below),
1919
% 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).
2122
% filename: a string for the file name to save the output JSON data.
2223
% opt: a struct for additional options, ignore to use default values.
2324
% opt can have the following fields (first in [.|.] is the default)
@@ -104,7 +105,8 @@
104105
rootisarray=0;
105106
rootlevel=1;
106107
forceroot=jsonopt('ForceRootName',0,opt);
107-
if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)
108+
if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || ...
109+
iscell(obj) || isobject(obj)) && isempty(rootname) && forceroot==0)
108110
rootisarray=1;
109111
rootlevel=0;
110112
else

saveubjson.m

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
% rootname: the name of the root-object, when set to '', the root name
1818
% is ignored, however, when opt.ForceRootName is set to 1 (see below),
1919
% 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)
2122
% filename: a string for the file name to save the output UBJSON data
2223
% opt: a struct for additional options, ignore to use default values.
2324
% opt can have the following fields (first in [.|.] is the default)
@@ -92,7 +93,8 @@
9293
rootisarray=0;
9394
rootlevel=1;
9495
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)
9698
rootisarray=1;
9799
rootlevel=0;
98100
else
@@ -129,6 +131,8 @@
129131
txt=struct2ubjson(name,item,level,varargin{:});
130132
elseif(ischar(item))
131133
txt=str2ubjson(name,item,level,varargin{:});
134+
elseif(isobject(item))
135+
txt=matlabobject2ubjson(name,item,level,varargin{:});
132136
else
133137
txt=mat2ubjson(name,item,level,varargin{:});
134138
end
@@ -328,6 +332,23 @@
328332
end
329333
txt=[txt,'}'];
330334

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+
331352
%%-------------------------------------------------------------------------
332353
function txt=matdata2ubjson(mat,level,varargin)
333354
if(isempty(mat))

0 commit comments

Comments
 (0)