forked from gllmflndn/JSONio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonwrite.m
221 lines (208 loc) · 6.47 KB
/
jsonwrite.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
function varargout = jsonwrite(varargin)
% Serialize a JSON (JavaScript Object Notation) structure
% FORMAT jsonwrite(filename,json)
% filename - JSON filename
% json - JSON structure
%
% FORMAT S = jsonwrite(json)
% json - JSON structure
% S - serialized JSON structure (string)
%
% FORMAT [...] = jsonwrite(...,opts)
% opts - structure of optional parameters:
% indent: string to use for indentation [Default: '']
% replacementStyle: string to control how non-alphanumeric
% characters are replaced [Default: 'underscore']
%
% References:
% JSON Standard: http://www.json.org/
% Guillaume Flandin
% $Id: jsonwrite.m 7025 2017-02-22 10:03:58Z guillaume $
%-Input parameters
%--------------------------------------------------------------------------
opts = struct('indent','','replacementstyle','underscore');
opt = struct([]);
if nargin > 1
if ischar(varargin{1})
filename = varargin{1};
json = varargin{2};
else
filename = '';
json = varargin{1};
opt = varargin{2};
end
if nargin > 2
opt = varargin{3};
end
else
filename = '';
json = varargin{1};
end
fn = fieldnames(opt);
for i=1:numel(fn)
if ~isfield(opts,lower(fn{i})), warning('Unknown option "%s".',fn{i}); end
opts.(lower(fn{i})) = opt.(fn{i});
end
optregistry(opts);
%-JSON serialization
%--------------------------------------------------------------------------
fmt('init',sprintf(opts.indent));
S = jsonwrite_var(json,~isempty(opts.indent));
%-Output
%--------------------------------------------------------------------------
if isempty(filename)
varargout = { S };
else
fid = fopen(filename,'wt');
if fid == -1
error('Unable to open file "%s" for writing.',filename);
end
fprintf(fid,'%s',S);
fclose(fid);
end
%==========================================================================
function S = jsonwrite_var(json,tab)
if nargin < 2, tab = ''; end
if isstruct(json) || isa(json,'containers.Map')
S = jsonwrite_struct(json,tab);
elseif iscell(json)
S = jsonwrite_cell(json,tab);
elseif ischar(json)
if size(json,1) <= 1
S = jsonwrite_char(json);
else
S = jsonwrite_cell(cellstr(json),tab);
end
elseif isnumeric(json) || islogical(json)
S = jsonwrite_numeric(json);
elseif isa(json,'string')
if numel(json) == 1
S = jsonwrite_char(char(json));
else
json = arrayfun(@(x)x,json,'UniformOutput',false);
json(cellfun(@(x) ismissing(x),json)) = {NaN}; % to be saved as null
idx = find(size(json)~=1);
if numel(idx) == 1 % vector
S = jsonwrite_cell(json,tab);
else % array
S = jsonwrite_cell(num2cell(json,setdiff(1:ndims(json),idx(1))),tab);
end
end
elseif isa(json,'datetime') || isa(json,'categorical')
S = jsonwrite_var(string(json));
elseif isa(json,'table')
S = struct;
s = size(json);
vn = json.Properties.VariableNames;
for i=1:s(1)
for j=1:s(2)
S(i).(vn{j}) = json{i,j};
end
end
S = jsonwrite_struct(S,tab);
else
if numel(json) ~= 1
json = arrayfun(@(x)x,json,'UniformOutput',false);
S = jsonwrite_cell(json,tab);
else
p = properties(json);
if isempty(p), p = fieldnames(json); end % for pre-classdef
s = struct;
for i=1:numel(p)
s.(p{i}) = json.(p{i});
end
S = jsonwrite_struct(s,tab);
%error('Class "%s" is not supported.',class(json));
end
end
%==========================================================================
function S = jsonwrite_struct(json,tab)
if numel(json) == 1
if isstruct(json), fn = fieldnames(json); else fn = keys(json); end
S = ['{' fmt('\n',tab)];
for i=1:numel(fn)
key = fn{i};
if strcmp(optregistry('replacementStyle'),'hex')
key = regexprep(key,...
'x0x([0-9a-fA-F]+)_', '${native2unicode(hex2dec($1))}');
end
if isstruct(json), val = json.(fn{i}); else val = json(fn{i}); end
S = [S fmt(tab) jsonwrite_char(key) ':' fmt(' ',tab) ...
jsonwrite_var(val,tab+1)];
if i ~= numel(fn), S = [S ',']; end
S = [S fmt('\n',tab)];
end
S = [S fmt(tab-1) '}'];
else
S = jsonwrite_cell(arrayfun(@(x) {x},json),tab);
end
%==========================================================================
function S = jsonwrite_cell(json,tab)
if numel(json) == 0 ...
|| (numel(json) == 1 && iscellstr(json)) ...
|| all(all(cellfun(@isnumeric,json))) ...
|| all(all(cellfun(@islogical,json)))
tab = '';
end
S = ['[' fmt('\n',tab)];
for i=1:numel(json)
S = [S fmt(tab) jsonwrite_var(json{i},tab+1)];
if i ~= numel(json), S = [S ',']; end
S = [S fmt('\n',tab)];
end
S = [S fmt(tab-1) ']'];
%==========================================================================
function S = jsonwrite_char(json)
% any-Unicode-character-except-"-or-\-or-control-character
% \" \\ \/ \b \f \n \r \t \u four-hex-digits
json = strrep(json,'\','\\');
json = strrep(json,'"','\"');
json = strrep(json,'/','\/');
json = strrep(json,sprintf('\b'),'\b');
json = strrep(json,sprintf('\f'),'\f');
json = strrep(json,sprintf('\n'),'\n');
json = strrep(json,sprintf('\r'),'\r');
json = strrep(json,sprintf('\t'),'\t');
S = ['"' json '"'];
%==========================================================================
function S = jsonwrite_numeric(json)
if any(imag(json(:)))
error('Complex numbers not supported.');
end
if numel(json) == 0
S = jsonwrite_cell({});
return;
elseif numel(json) > 1
idx = find(size(json)~=1);
if numel(idx) == 1 % vector
S = jsonwrite_cell(num2cell(json),'');
else % array
S = jsonwrite_cell(num2cell(json,setdiff(1:ndims(json),idx(1))),'');
end
return;
end
if islogical(json)
if json, S = 'true'; else S = 'false'; end
else
S = num2str(json,16);
end
%==========================================================================
function b = fmt(varargin)
persistent tab;
if nargin == 2 && isequal(varargin{1},'init')
tab = varargin{2};
end
b = '';
if nargin == 1
if varargin{1} > 0, b = repmat(tab,1,varargin{1}); end
elseif nargin == 2
if ~isempty(tab) && ~isempty(varargin{2}), b = sprintf(varargin{1}); end
end
%==========================================================================
function val = optregistry(opts)
persistent options
if isstruct(opts)
options = opts;
else
val = options.(lower(opts));
end