forked from marioarbras/aircraft-design-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prettyjson.m
71 lines (58 loc) · 2.58 KB
/
prettyjson.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
function [less_ugly] = prettyjson(ugly)
% Makes JSON strings (relatively) pretty
% Probably inefficient
% Mostly meant for structures with simple strings and arrays;
% gets confused and !!mangles!! JSON when strings contain [ ] { or }.
MAX_ARRAY_WIDTH = 80;
TAB = ' ';
ugly = strrep(ugly, '{', sprintf('{\n'));
ugly = strrep(ugly, '}', sprintf('\n}'));
ugly = strrep(ugly, ',"', sprintf(', \n"'));
ugly = strrep(ugly, ',{', sprintf(', \n{'));
indent = 0;
lines = splitlines(ugly);
for i = 1:length(lines)
line = lines{i};
next_indent = 0;
% Count brackets
open_brackets = length(strfind(line, '['));
close_brackets = length(strfind(line, ']'));
open_braces = length(strfind(line, '{'));
close_braces = length(strfind(line, '}'));
if close_brackets > open_brackets || close_braces > open_braces
indent = indent - 1;
end
if open_brackets > close_brackets
line = strrep(line, '[', sprintf('[\n'));
next_indent = 1;
elseif open_brackets < close_brackets
line = strrep(line, ']', sprintf('\n]'));
next_indent = -1;
elseif open_brackets == close_brackets && length(line) > MAX_ARRAY_WIDTH
first_close_bracket = strfind(line, ']');
if first_close_bracket > MAX_ARRAY_WIDTH % Just a long array -> each element on a new line
line = strrep(line, '[', sprintf('[\n%s', TAB));
line = strrep(line, ']', sprintf('\n]'));
line = strrep(line, ',', sprintf(', \n%s', TAB)); % Add indents!
else % Nested array, probably 2d, first level is not too wide -> each sub-array on a new line
line = strrep(line, '[[', sprintf('[\n%s[', TAB));
line = strrep(line, '],', sprintf('], \n%s', TAB)); % Add indents!
line = strrep(line, ']]', sprintf(']\n]'));
end
end
sublines = splitlines(line);
for j = 1:length(sublines)
if j > 1 % todo: dumb to do this check at every line...
sublines{j} = sprintf('%s%s', repmat(TAB, 1, indent+next_indent), sublines{j});
else
sublines{j} = sprintf('%s%s', repmat(TAB, 1, indent), sublines{j});
end
end
if open_brackets > close_brackets || open_braces > close_braces
indent = indent + 1;
end
indent = indent + next_indent;
lines{i} = strjoin(sublines, newline);
end
less_ugly = strjoin(lines, newline);
end