|
| 1 | +package com.puppycrawl.tools.checkstyle; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +/** |
| 9 | + * Our own Json processing utility. |
| 10 | + * @author LuoLiangchen |
| 11 | + */ |
| 12 | +class JsonUtil { |
| 13 | + /** |
| 14 | + * Adds indent of the given text. |
| 15 | + * @param text the text to add indent |
| 16 | + * @return the result text with indent |
| 17 | + */ |
| 18 | + private static String addIndent(String text) { |
| 19 | + return Arrays.stream(text.split("\n")) |
| 20 | + .map(line -> " " + line) |
| 21 | + .collect(Collectors.joining("\n")); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Formats the Json object. |
| 26 | + * @param object the object to format |
| 27 | + * @return the format result |
| 28 | + */ |
| 29 | + private static String format(Object object) { |
| 30 | + if (object instanceof String) { |
| 31 | + return "\"" + object + "\""; |
| 32 | + } else { |
| 33 | + return object.toString(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** Represents an object type in Json. */ |
| 38 | + public static final class JsonObject { |
| 39 | + /** Fields of this Json object. */ |
| 40 | + private final List<KeyValue> members = new LinkedList<>(); |
| 41 | + |
| 42 | + /** |
| 43 | + * Adds a member. |
| 44 | + * @param key the key of the field |
| 45 | + * @param value the value of the field |
| 46 | + */ |
| 47 | + void addProperty(String key, Object value) { |
| 48 | + add(key, value); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Adds a member. |
| 53 | + * @param key the key of the field |
| 54 | + * @param value the value of the field |
| 55 | + */ |
| 56 | + void add(String key, Object value) { |
| 57 | + members.add(new KeyValue(key, value)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String toString() { |
| 62 | + final String keyValueLines = members.stream() |
| 63 | + .map(Object::toString) |
| 64 | + .collect(Collectors.joining(",\n")); |
| 65 | + return "{\n" + addIndent(keyValueLines) + "\n}"; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** Represents an array type in Json. */ |
| 70 | + public static final class JsonArray { |
| 71 | + /** Items of this Json array. */ |
| 72 | + private final List<Object> members = new LinkedList<>(); |
| 73 | + |
| 74 | + /** |
| 75 | + * Adds a member. |
| 76 | + * @param object the member to add |
| 77 | + */ |
| 78 | + void add(Object object) { |
| 79 | + members.add(object); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String toString() { |
| 84 | + final String membersLines = members.stream() |
| 85 | + .map(JsonUtil::format) |
| 86 | + .collect(Collectors.joining(",\n")); |
| 87 | + return "[\n" + addIndent(membersLines) + "\n]"; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** Represents a key-value pair in Json object. */ |
| 92 | + private static final class KeyValue { |
| 93 | + /** The key of the field. */ |
| 94 | + private final String key; |
| 95 | + |
| 96 | + /** The value of the field. */ |
| 97 | + private final Object value; |
| 98 | + |
| 99 | + /** |
| 100 | + * Creates a new instance of KeyValue. |
| 101 | + * @param key the key of the field |
| 102 | + * @param value the value of the field |
| 103 | + */ |
| 104 | + KeyValue(String key, Object value) { |
| 105 | + this.key = key; |
| 106 | + this.value = value; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String toString() { |
| 111 | + return "\"" + key + "\": " + format(value); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments