1+ function s = obj2json(rig )
2+ % OBJ2JSON Converts input into JSON
3+ s = obj2struct(rig );
4+ if verLessThan(' matlab' ,' 9.1' )
5+ s = savejson(' ' , s );
6+ elseif verLessThan(' matlab' ,' 9.3' )
7+ s = jsonencode(s );
8+ else
9+ s = jsonencode(s , ' ConvertInfAndNaN' , true );
10+ end
11+ end
12+
13+ function s = obj2struct(obj )
14+ % OBJ2STRUCT Converts input object into a struct
15+ % Returns the input but with any non-fundamental object converted to a
16+ % structure. If the input does not contain an object, the resulting
17+ % output will remain unchanged.
18+ %
19+ % NB: Does not convert National Instruments object or objects within
20+ % non-scalar structures. Cannot currently deal with Java, COM or certain
21+ % graphics objects. Function handles are converted to strings.
22+ %
23+ % 2018-05-03 MW created
24+
25+ if isobject(obj )
26+ if length(obj ) > 1
27+ % If dealing with heterogeneous array of objects, recurse through array
28+ s = arrayfun(@obj2struct , obj , ' uni' , 0 );
29+ elseif isa(obj , ' containers.Map' )
30+ % Convert to scalar struct
31+ keySet = keys(obj );
32+ valueSet = values(obj );
33+ for j = 1 : length(keySet )
34+ m.(keySet{j }) = valueSet{j };
35+ end
36+ s = obj2struct(m );
37+ else % Normal object
38+ names = fieldnames(obj ); % Get list of public properties
39+ for i = 1 : length(names )
40+ if isobject(obj.(names{i })) % Property contains an object
41+ if startsWith(class(obj.(names{i })),' daq.ni.' )
42+ % Do not attempt to save ni daq sessions of channels
43+ s.(names{i }) = [];
44+ else % Recurse
45+ s.(names{i }) = obj2struct(obj.(names{i }));
46+ end
47+ elseif iscell(obj.(names{i }))
48+ % If property contains cell array, run through each element in case
49+ % any contain an object
50+ s.(names{i }) = cellfun(@obj2struct , obj.(names{i }), ' uni' , 0 );
51+ elseif isstruct(obj.(names{i })) && isscalar(obj.(names{i }))
52+ % If property contains struct, run through each field in case any
53+ % contain an object
54+ s.(names{i }) = structfun(@obj2struct , obj.(names{i }), ' uni' , 0 );
55+ elseif isa(obj.(names{i }), ' function_handle' )
56+ % Convert function to string
57+ s.(names{i }) = func2str(obj.(names{i }));
58+ elseif isa(obj.(names{i }), ' containers.Map' )
59+ % Convert to scalar struct
60+ keySet = keys(obj.(names{i }));
61+ valueSet = values(obj.(names{i }));
62+ for j = 1 : length(keySet )
63+ m.(keySet{j }) = valueSet{j };
64+ end
65+ s.(names{i }) = obj2struct(m );
66+ else % Property is fundamental object
67+ s.(names{i }) = obj.(names{i });
68+ end
69+ end
70+ s.ClassContructor = class(obj ); % Supply class name for loading object
71+ end
72+ elseif iscell(obj )
73+ % If dealing with cell array, recurse through elements
74+ s = cellfun(@obj2struct , obj , ' uni' , 0 );
75+ elseif isstruct(obj ) && isscalar(obj )
76+ % If dealing with structure, recurse through fields
77+ s = structfun(@obj2struct , obj , ' uni' , 0 );
78+ elseif isa(obj , ' function_handle' )
79+ % Convert function to string
80+ s = func2str(obj );
81+ else % Fundamental object, return unchanged
82+ s = obj ;
83+ end
84+ end
0 commit comments