-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathPrinter.java
More file actions
168 lines (133 loc) · 4.42 KB
/
PathPrinter.java
File metadata and controls
168 lines (133 loc) · 4.42 KB
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
package io.github.trackerforce.path;
import io.github.trackerforce.path.api.DotPrinter;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
class PathPrinter implements DotPrinter {
private String indent;
private boolean prettier;
PathPrinter(int indentSize) {
setIndentSize(indentSize);
}
@Override
public String toJson(Object obj, boolean prettier) {
this.prettier = prettier;
return toJsonInternal(obj, 0);
}
@Override
public void setIndentSize(int indentSize) {
indent = " ".repeat(indentSize);
}
private String toJsonInternal(Object obj, int depth) {
if (obj == null) {
return "null";
}
if (obj instanceof String value) {
return "\"" + escapeString(value) + "\"";
}
if (obj instanceof Number || obj instanceof Boolean) {
return obj.toString();
}
if (obj instanceof List<?> value) {
return listToJson(value, depth);
}
if (obj instanceof Map<?, ?> value) {
return mapToJson(value, depth);
}
if (obj.getClass().isArray()) {
return arrayToJson(obj, depth);
}
return "\"" + escapeString(obj.toString()) + "\"";
}
private String mapToJson(Map<?, ?> map, int depth) {
if (map.isEmpty()) {
return "{}";
}
if (!prettier) {
StringJoiner joiner = new StringJoiner(", ", "{", "}");
for (Map.Entry<?, ?> entry : map.entrySet()) {
String key = "\"" + escapeString(entry.getKey().toString()) + "\"";
String value = toJsonInternal(entry.getValue(), depth);
joiner.add(key + ": " + value);
}
return joiner.toString();
}
StringBuilder sb = new StringBuilder();
sb.append("{");
boolean first = true;
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (!first) {
sb.append(",");
}
sb.append("\n").append(getIndent(depth + 1));
String key = "\"" + escapeString(entry.getKey().toString()) + "\"";
String value = toJsonInternal(entry.getValue(), depth + 1);
sb.append(key).append(": ").append(value);
first = false;
}
sb.append("\n").append(getIndent(depth)).append("}");
return sb.toString();
}
private String listToJson(List<?> list, int depth) {
if (list.isEmpty()) {
return "[]";
}
if (!prettier) {
StringJoiner joiner = new StringJoiner(", ", "[", "]");
for (Object item : list) {
joiner.add(toJsonInternal(item, depth));
}
return joiner.toString();
}
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean first = true;
for (Object item : list) {
if (!first) {
sb.append(",");
}
sb.append("\n").append(getIndent(depth + 1));
sb.append(toJsonInternal(item, depth + 1));
first = false;
}
sb.append("\n").append(getIndent(depth)).append("]");
return sb.toString();
}
private String arrayToJson(Object array, int depth) {
if (Array.getLength(array) == 0) {
return "[]";
}
if (!prettier) {
StringJoiner joiner = new StringJoiner(", ", "[", "]");
for (int i = 0; i < Array.getLength(array); i++) {
joiner.add(toJsonInternal(Array.get(array, i), depth));
}
return joiner.toString();
}
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < Array.getLength(array); i++) {
if (i > 0) {
sb.append(",");
}
sb.append("\n").append(getIndent(depth + 1));
sb.append(toJsonInternal(Array.get(array, i), depth + 1));
}
sb.append("\n").append(getIndent(depth)).append("]");
return sb.toString();
}
private String getIndent(int depth) {
return indent.repeat(depth);
}
private String escapeString(String str) {
if (str == null) {
return "";
}
return str.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t");
}
}