Skip to content

Compress output of 'm2json' #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
51 changes: 31 additions & 20 deletions plotly/plotly_aux/m2json.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,60 @@
valstr = struct2json(val);
elseif iscell(val)
valstr = cell2json(val);
elseif isa(val, 'numeric')
elseif isa(val, "numeric")
sz = size(val);
if isa(val,"single")
precision = "7";
else
precision = "15";
end
fmt = "%." + precision + "g,";
if length(find(sz>1))>1 % 2D or higher array
valstr = '';
valstr = "";
for i = 1:sz(1)
valsubstr = [sprintf('%.15g, ', val(i,:))];
valsubstr = valsubstr(1:(end-2));
valstr = [valstr ', [' valsubstr ']'];
valsubstr = sprintf(fmt, val(i,:));
valsubstr = valsubstr(1:(end-1));
valstr = valstr + ", [" + valsubstr + "]";
end
valstr = valstr(3:end); % trail leading commas
else
valstr = [sprintf('%.15g, ', val)];
valstr = valstr(1:(end-2));
valstr = [sprintf(fmt, val)];
valstr = valstr(1:(end-1));
end
if length(val)>1
valstr = ['[' valstr ']'];
elseif length(val) == 0
valstr = '[]';
valstr = "[" + valstr + "]";
elseif isempty(val)
valstr = "[]";
end
valstr = strrep(valstr,'-Inf', 'null');
valstr = strrep(valstr, 'Inf', 'null');
valstr = strrep(valstr, 'NaN', 'null');
valstr = strrep(valstr,"-Inf", "null");
valstr = strrep(valstr, "Inf", "null");
valstr = strrep(valstr, "NaN", "null");
elseif ischar(val)
[r, ~] = size(val);
% We can't use checkescape() if we have ['abc'; 'xyz']
if r > 1
valstr = cell2json(cellstr(val));
else
val = checkescape(val); %add escape characters
valstr = ['"' val '"'];
val = checkescape(val); % add escape characters
valstr = sprintf('"%s"', val);
end
elseif islogical(val)
if val
valstr = 'true';
valstr = "true";
else
valstr = 'false';
valstr = "false";
end
elseif isdatetime(val)
valstr = m2json(convertDate(val));
elseif isstring(val)
fh = ifel(isscalar(val),@char,@cellstr);
if isscalar(val)
fh = @char;
else
fh = @cellstr;
end
valstr = m2json(fh(val));
else
valstr = ''; % wtf is it?
warning("Failed to m2json encode class of type: %s",class(val));
valstr = "";
warning("Failed to m2json encode class of type: %s", class(val));
end
end