|
15 | 15 | */
|
16 | 16 | package org.utplsql.sqldev.model;
|
17 | 17 |
|
18 |
| -import org.springframework.core.style.DefaultToStringStyler; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.StringJoiner; |
| 21 | + |
| 22 | +import javax.annotation.Nullable; |
| 23 | + |
19 | 24 | import org.springframework.core.style.ToStringStyler;
|
20 |
| -import org.springframework.util.ClassUtils; |
| 25 | +import org.springframework.core.style.ValueStyler; |
21 | 26 |
|
22 |
| -public class UtplsqlToStringStyler extends DefaultToStringStyler { |
23 |
| - public static final ToStringStyler INSTANCE = new UtplsqlToStringStyler(); |
| 27 | +public class JsonToStringStyler implements ToStringStyler, ValueStyler{ |
| 28 | + public static final ToStringStyler INSTANCE = new JsonToStringStyler(); |
| 29 | + public static final String INDENT_SPACES = " "; |
| 30 | + private int indent = 0; |
| 31 | + |
| 32 | + private void newLine(StringBuilder buffer) { |
| 33 | + buffer.append('\n'); |
| 34 | + buffer.append(getIndentSpaces(0)); |
| 35 | + } |
| 36 | + |
| 37 | + private String getIndentSpaces(int indentOffset) { |
| 38 | + StringBuilder sb = new StringBuilder(); |
| 39 | + for (int i=0; i<indent+indentOffset; i++) { |
| 40 | + sb.append(INDENT_SPACES); |
| 41 | + } |
| 42 | + return sb.toString(); |
| 43 | + } |
24 | 44 |
|
25 |
| - public UtplsqlToStringStyler() { |
26 |
| - super(new UtplsqlValueStyler()); |
| 45 | + private String getStringStyle(String value) { |
| 46 | + StringBuilder sb = new StringBuilder(); |
| 47 | + sb.append('"'); |
| 48 | + sb.append(value.replace("\"", "\\\"").replace("\n", "\\n").replace("\r", "")); |
| 49 | + sb.append('"'); |
| 50 | + return sb.toString(); |
27 | 51 | }
|
28 | 52 |
|
29 |
| - @Override |
30 |
| - public void styleFieldSeparator(StringBuilder buffer) { |
31 |
| - buffer.append(",\n"); |
| 53 | + private String getArrayStyle(Object[] array) { |
| 54 | + if (array.length == 0) { |
| 55 | + return "[]"; |
| 56 | + } |
| 57 | + |
| 58 | + StringJoiner result = new StringJoiner(",\n" + getIndentSpaces(1), "[\n" + getIndentSpaces(1) , "\n" + getIndentSpaces(0) + "]"); |
| 59 | + indent++; |
| 60 | + for (Object o : array) { |
| 61 | + result.add(style(o)); |
| 62 | + } |
| 63 | + indent--; |
| 64 | + return result.toString(); |
32 | 65 | }
|
33 | 66 |
|
| 67 | + private String getListStyle(List<?> list) { |
| 68 | + if (list.isEmpty()) { |
| 69 | + return "[]"; |
| 70 | + } |
| 71 | + |
| 72 | + StringJoiner result = new StringJoiner(",\n" + getIndentSpaces(1), "[\n" + getIndentSpaces(1) , "\n" + getIndentSpaces(0) + "]"); |
| 73 | + indent++; |
| 74 | + for (Object o : list) { |
| 75 | + result.add(style(o)); |
| 76 | + } |
| 77 | + indent--; |
| 78 | + return result.toString(); |
| 79 | + } |
| 80 | + |
| 81 | + private String getMapStyle(Map<?, ?> map) { |
| 82 | + if (map.isEmpty()) { |
| 83 | + return "[]"; |
| 84 | + } |
| 85 | + |
| 86 | + StringJoiner result = new StringJoiner(",\n" + getIndentSpaces(1), "[\n" + getIndentSpaces(1) , "\n" + getIndentSpaces(0) + "]"); |
| 87 | + indent++; |
| 88 | + for (Object o : map.values()) { |
| 89 | + result.add(style(o)); |
| 90 | + } |
| 91 | + indent--; |
| 92 | + return result.toString(); |
| 93 | + } |
| 94 | + |
| 95 | + private String getDefaultStyle(Object value) { |
| 96 | + return String.valueOf(value); |
| 97 | + } |
| 98 | + |
34 | 99 | @Override
|
35 | 100 | public void styleStart(StringBuilder buffer, Object obj) {
|
| 101 | + indent++; |
36 | 102 | if (!obj.getClass().isArray()) {
|
37 |
| - buffer.append("[").append(ClassUtils.getShortName(obj.getClass())); |
38 |
| - buffer.append('\n'); |
| 103 | + buffer.append("{"); |
| 104 | + newLine(buffer); |
| 105 | + buffer.append("\"className\": "); |
| 106 | + buffer.append('"'); |
| 107 | + buffer.append(obj.getClass().getSimpleName()); |
| 108 | + buffer.append('"'); |
| 109 | + buffer.append(','); |
39 | 110 | } else {
|
40 | 111 | buffer.append('[');
|
41 | 112 | styleValue(buffer, obj);
|
42 | 113 | }
|
43 | 114 | }
|
| 115 | + |
| 116 | + @Override |
| 117 | + public void styleEnd(StringBuilder buffer, Object obj) { |
| 118 | + indent--; |
| 119 | + newLine(buffer); |
| 120 | + if (!obj.getClass().isArray()) { |
| 121 | + buffer.append('}'); |
| 122 | + } else { |
| 123 | + buffer.append(']'); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void styleField(StringBuilder buffer, String fieldName, @Nullable Object value) { |
| 129 | + newLine(buffer); |
| 130 | + buffer.append('"'); |
| 131 | + buffer.append(fieldName); |
| 132 | + buffer.append('"'); |
| 133 | + buffer.append(": "); |
| 134 | + styleValue(buffer, value); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void styleValue(StringBuilder buffer, Object value) { |
| 139 | + buffer.append(style(value)); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void styleFieldSeparator(StringBuilder buffer) { |
| 144 | + buffer.append(","); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String style(Object value) { |
| 149 | + if (value == null) { |
| 150 | + return "null"; |
| 151 | + } else if (value instanceof String) { |
| 152 | + return getStringStyle((String) value); |
| 153 | + } else if (value instanceof Object[]) { |
| 154 | + return getArrayStyle((Object[]) value); |
| 155 | + } else if (value instanceof List<?>) { |
| 156 | + return getListStyle((List<?>) value); |
| 157 | + } else if (value instanceof Map) { |
| 158 | + return getMapStyle((Map<?, ?>) value); |
| 159 | + } else { |
| 160 | + return getDefaultStyle(value); |
| 161 | + } |
| 162 | + } |
44 | 163 | }
|
0 commit comments