Skip to content

Commit 53bbb8c

Browse files
author
Jai Bhagat
authored
Merge pull request #106 from cortex-lab/obj2json_fix
Bug fix for turning empty objects into struct
2 parents ce9c36d + 64416dd commit 53bbb8c

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

cb-tools/obj2struct.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
end
2424
s = obj2struct(m);
2525
else % Normal object
26+
s.ClassContructor = class(obj); % Supply class name for loading object
2627
names = fieldnames(obj); % Get list of public properties
2728
for i = 1:length(names)
28-
if isobject(obj.(names{i})) % Property contains an object
29+
if isempty(obj) % Object and therefore all properties are empty
30+
s.(names{i}) = [];
31+
elseif isobject(obj.(names{i})) % Property contains an object
2932
if startsWith(class(obj.(names{i})),'daq.ni.')
3033
% Do not attempt to save ni daq sessions of channels
31-
s.(names{i}) = [];
34+
s.(names{i}) = [];
3235
else % Recurse
3336
s.(names{i}) = obj2struct(obj.(names{i}));
3437
end
@@ -55,7 +58,6 @@
5558
s.(names{i}) = obj.(names{i});
5659
end
5760
end
58-
s.ClassContructor = class(obj); % Supply class name for loading object
5961
end
6062
elseif iscell(obj)
6163
% If dealing with cell array, recurse through elements

tests/obj2json_test.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
%% Test obj2struct with given data
2+
data = struct;
3+
data.A = struct(... % Scalar struct
4+
'field1', zeros(10), ...
5+
'field2', true(10), ...
6+
'field3', pi, ...
7+
'field4', single(10), ...
8+
'field5', '10');
9+
data.B = hw.DaqController(); % Obj containing empty obj
10+
v = daq.getVendors();
11+
if v(strcmp({v.ID},'ni')).IsOperational
12+
data.B.createDaqChannels(); % Add daq.ni obj
13+
end
14+
data.C = struct; % Non-scalar struct
15+
data.C(1,1).a = 1;
16+
data.C(2,1).a = 2;
17+
data.C(1,2).a = 3;
18+
data.C(2,2).a = 4;
19+
data.D = @(a,b,c)zeros(c,b,a); % Function handle
20+
21+
json = obj2json(data);
22+
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)"}';
23+
assert(strcmp(json,out), 'Test failed')

0 commit comments

Comments
 (0)