-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.bal
96 lines (84 loc) · 3.04 KB
/
utils.bal
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
isolated function getIndentation(int indentation) returns string {
string result = "";
foreach int i in 1...indentation {
result += " ";
}
return result;
}
isolated function getIndentationForLevel(string indentation, int level) returns string {
string result = "";
foreach int i in 1...level {
result += indentation;
}
return result;
}
isolated function getInitialIndentation(string indentation, int level, boolean isMapField) returns string {
if isMapField {
return " ";
}
return getIndentationForLevel(indentation, level);
}
isolated function prettifyJson(json value, string indentation, int level, boolean isMapField = false) returns string {
if value == () {
return "";
} else if value is map<json> {
return prettifyJsonMap(value, indentation, level, isMapField);
} else if value is json[] {
return prettifyJsonArray(value, indentation, level, isMapField);
} else {
return prettifyJsonField(value, indentation, level, isMapField);
}
}
isolated function prettifyJsonMap(map<json> value, string indentation, int level, boolean isMapField) returns string {
string initialIndentation = getInitialIndentation(indentation, level, isMapField);
string result = string`${initialIndentation}{`;
boolean isEmptyMap = value.keys().length() == 0;
if !isEmptyMap {
result += "\n";
}
int fieldLevel = level + 1;
string fieldIndentation = getIndentationForLevel(indentation, fieldLevel);
int length = value.length();
int i = 1;
foreach string key in value.keys() {
string fieldValue = prettifyJson(value.get(key), indentation, fieldLevel, true);
string line = string`${fieldIndentation}"${key}":${fieldValue}`;
result += line;
if i != length {
result += ",";
}
result += "\n";
i += 1;
}
if !isEmptyMap {
result += getIndentationForLevel(indentation, level);
}
result += "}";
return result;
}
isolated function prettifyJsonArray(json[] array, string indentation, int level, boolean isMapField) returns string {
string initialIndentation = getInitialIndentation(indentation, level, isMapField);
string result = string`${initialIndentation}[`;
boolean isEmptyArray = array.length() == 0;
if !isEmptyArray {
result += "\n";
}
int elementLevel = level + 1;
string[] elements = [];
foreach json value in array {
elements.push(prettifyJson(value, indentation, elementLevel));
}
string separator = ",\n";
result += 'string:'join(separator, ...elements);
if !isEmptyArray {
result += "\n" + getIndentationForLevel(indentation, level);
}
result += "]";
return result;
}
isolated function prettifyJsonField(json value, string indentation, int level, boolean isMapField) returns string {
string initialIndentation = getInitialIndentation(indentation, level, isMapField);
string result = initialIndentation;
result += value.toJsonString();
return result;
}