-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmautogradeJsonWriter.m
100 lines (93 loc) · 2.61 KB
/
mautogradeJsonWriter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
%A limited function to write a variable as JSON into a file
%function mautogradeJsonWriter(fid,s)
%Recursively writes the contents of the variable s to the given file id
%using JSON syntax
%Notes:
% 1. only some types of variables are supported (structs, arrays, double,
% strings).
% 2. arrays are flattened to linear arrays
%Inputs
% fid file id for writing (use 1 for terminal)
% s MATLAB variable to be written to JSON
function mautogradeJsonWriter(fid,s,indentation)
if ~exist('indentation','var')
indentation='';
end
%check number of elements in s
switch numel(s)
case 0 %(empty)
switch class(s)
case 'char'
fprintf(fid,'""');
otherwise
fprintf(fid,'{}');
end
case 1
switch class(s)
case 'struct'
writeStruct(fid,s,indentation)
case 'char'
writeChar(fid,s)
case 'double'
fprintf(fid,num2str(s));
otherwise
error(['Type ' class(s) 'not supported'])
end
otherwise
switch class(s)
case 'char'
writeChar(fid,s)
case 'cell'
writeCellArray(fid,s,indentation)
otherwise
writeArray(fid,s,indentation)
end
end
if isempty(indentation)
fprintf(fid,'\n');
end
function writeChar(fid,s)
% Remove invalid control characters
s(s<=31)=[];
fprintf(fid,'"%s"%s"',s);
function writeStruct(fid,s,indentation)
fields=fieldnames(s);
nbFields=length(fields);
indentationNew=[' ' indentation];
fprintf(fid,[indentation '{ \n']);
for iField=1:nbFields
name=fields{iField};
data=s.(name);
fprintf(fid,'%s"%s" : ',indentationNew,name);
mautogradeJsonWriter(fid,data,indentationNew)
if iField~=nbFields
fprintf(fid,',');
end
fprintf(fid,'\n');
end
fprintf(fid,[indentation '}']);
function writeArray(fid,s,indentation)
fprintf(fid,['\n' indentation ' [\n']);
indentationNew=[' ' indentation];
nbElements=numel(s);
for iElement=1:nbElements
mautogradeJsonWriter(fid,s(iElement),indentationNew)
if iElement~=nbElements
fprintf(fid,',');
end
fprintf(fid,'\n');
end
fprintf(fid,[indentation ' ]']);
function writeCellArray(fid,s,indentation)
fprintf(fid,['\n' indentation ' [\n']);
indentationNew=[' ' indentation];
nbElements=numel(s);
for iElement=1:nbElements
fprintf(fid,indentationNew);
mautogradeJsonWriter(fid,s{iElement},indentationNew)
if iElement~=nbElements
fprintf(fid,',');
end
fprintf(fid,'\n');
end
fprintf(fid,[indentation ' ]']);