Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cb-tools/obj2struct.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
end
s = obj2struct(m);
else % Normal object
s.ClassContructor = class(obj); % Supply class name for loading object
names = fieldnames(obj); % Get list of public properties
for i = 1:length(names)
if isobject(obj.(names{i})) % Property contains an object
if isempty(obj) % Object and therefore all properties are empty
s.(names{i}) = [];
elseif isobject(obj.(names{i})) % Property contains an object
if startsWith(class(obj.(names{i})),'daq.ni.')
% Do not attempt to save ni daq sessions of channels
s.(names{i}) = [];
s.(names{i}) = [];
else % Recurse
s.(names{i}) = obj2struct(obj.(names{i}));
end
Expand All @@ -55,7 +58,6 @@
s.(names{i}) = obj.(names{i});
end
end
s.ClassContructor = class(obj); % Supply class name for loading object
end
elseif iscell(obj)
% If dealing with cell array, recurse through elements
Expand Down
23 changes: 23 additions & 0 deletions tests/obj2json_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%% Test obj2struct with given data
data = struct;
data.A = struct(... % Scalar struct
'field1', zeros(10), ...
'field2', true(10), ...
'field3', pi, ...
'field4', single(10), ...
'field5', '10');
data.B = hw.DaqController(); % Obj containing empty obj
v = daq.getVendors();
if v(strcmp({v.ID},'ni')).IsOperational
data.B.createDaqChannels(); % Add daq.ni obj
end
data.C = struct; % Non-scalar struct
data.C(1,1).a = 1;
data.C(2,1).a = 2;
data.C(1,2).a = 3;
data.C(2,2).a = 4;
data.D = @(a,b,c)zeros(c,b,a); % Function handle

json = obj2json(data);
out = '{"A":{"field1":[[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]],"field2":[[true,true,true,true,true,true,true,true,true,true],[true,true,true,true,true,true,true,true,true,true],[true,true,true,true,true,true,true,true,true,true],[true,true,true,true,true,true,true,true,true,true],[true,true,true,true,true,true,true,true,true,true],[true,true,true,true,true,true,true,true,true,true],[true,true,true,true,true,true,true,true,true,true],[true,true,true,true,true,true,true,true,true,true],[true,true,true,true,true,true,true,true,true,true],[true,true,true,true,true,true,true,true,true,true]],"field3":3.1415926535897931,"field4":10,"field5":"10"},"B":{"ClassContructor":"hw.DaqController","ChannelNames":[],"SignalGenerators":{"ClassContructor":"hw.PulseSwitcher","OpenValue":[],"ClosedValue":[],"ParamsFun":[],"DefaultCommand":[],"DefaultValue":[]},"DaqIds":"Dev1","DaqChannelIds":[],"SampleRate":1000,"DaqSession":[],"DigitalDaqSession":[],"Value":[],"NumChannels":0,"AnalogueChannelsIdx":[]},"C":[[{"a":1},{"a":3}],[{"a":2},{"a":4}]],"D":"@(a,b,c)zeros(c,b,a)"}';
assert(strcmp(json,out), 'Test failed')