|
| 1 | +package wu.seal.jsontokotlin; |
| 2 | + |
| 3 | +import com.google.gson.*; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | + |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +/** |
| 11 | + * Created by seal.wu on 2017/8/21. |
| 12 | + */ |
| 13 | +public class KotlinMaker { |
| 14 | + private final static Gson gson = new Gson(); |
| 15 | + |
| 16 | + private String className; |
| 17 | + private JsonElement inputElement; |
| 18 | + |
| 19 | + private Set<String> toBeAppend = new HashSet<String>(); |
| 20 | + |
| 21 | + public KotlinMaker(String className, String inputText) { |
| 22 | + this.inputElement = gson.fromJson(inputText, JsonElement.class); |
| 23 | + this.className = className; |
| 24 | + } |
| 25 | + |
| 26 | + public KotlinMaker(String className, JsonElement jsonElement) { |
| 27 | + this.inputElement = jsonElement; |
| 28 | + this.className = className; |
| 29 | + } |
| 30 | + |
| 31 | + public String makeKotlinData() { |
| 32 | + StringBuilder stringBuilder = new StringBuilder(); |
| 33 | + stringBuilder.append("\n"); |
| 34 | + |
| 35 | + JsonElement jsonElement = inputElement; |
| 36 | + if (jsonElement.isJsonObject()) { |
| 37 | + appClassName(stringBuilder); |
| 38 | + appendJsonObject(stringBuilder, (JsonObject) jsonElement); |
| 39 | + } else { |
| 40 | + throw new IllegalArgumentException("不可能有其它情况"); |
| 41 | + } |
| 42 | + |
| 43 | + int index = stringBuilder.lastIndexOf(","); |
| 44 | + if (index != -1) { |
| 45 | + stringBuilder.deleteCharAt(index); |
| 46 | + } |
| 47 | + stringBuilder.append(")"); |
| 48 | + for (String append : toBeAppend) { |
| 49 | + stringBuilder.append("\n"); |
| 50 | + stringBuilder.append(append); |
| 51 | + } |
| 52 | + return stringBuilder.toString(); |
| 53 | + } |
| 54 | + |
| 55 | + private void appClassName(StringBuilder stringBuilder) { |
| 56 | + stringBuilder.append("data class ").append(className).append("(\n"); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + private void appendJsonObject(StringBuilder stringBuilder, JsonObject jsonObject) { |
| 61 | + for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { |
| 62 | + String property = entry.getKey(); |
| 63 | + JsonElement jsonElementValue = entry.getValue(); |
| 64 | + String type = "String"; |
| 65 | + if (jsonElementValue.isJsonArray()) { |
| 66 | + type = getArrayType(property, (JsonArray) jsonElementValue); |
| 67 | + addProperty(stringBuilder, property, type, null); |
| 68 | + } else if (jsonElementValue.isJsonPrimitive()) { |
| 69 | + type = getPrimitiveType(jsonElementValue); |
| 70 | + addProperty(stringBuilder, property, type, jsonElementValue.getAsString()); |
| 71 | + } else if (jsonElementValue.isJsonObject()) { |
| 72 | + type = getJsonObjectType(property, (JsonObject) jsonElementValue); |
| 73 | + addProperty(stringBuilder, property, type, null); |
| 74 | + } else if (jsonElementValue.isJsonNull()) { |
| 75 | + addProperty(stringBuilder, property, type, null); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @NotNull |
| 81 | + private String getJsonObjectType(String property, JsonObject jsonElementValue) { |
| 82 | + String type; |
| 83 | + type = property.subSequence(0, 1).toString().toUpperCase() + property.subSequence(1, property.length()); |
| 84 | + toBeAppend.add(new KotlinMaker(type, jsonElementValue).makeKotlinData()); |
| 85 | + return type; |
| 86 | + } |
| 87 | + |
| 88 | + @NotNull |
| 89 | + private String getArrayType(String property, JsonArray jsonElementValue) { |
| 90 | + String type = "String"; |
| 91 | + JsonArray jsonArray = jsonElementValue; |
| 92 | + JsonElement next = jsonArray.iterator().next(); |
| 93 | + if (next.isJsonPrimitive()) { |
| 94 | + String subType = getPrimitiveType(next); |
| 95 | + type = "List" + "<" + subType + ">"; |
| 96 | + |
| 97 | + } else if (next.isJsonObject()) { |
| 98 | + property = modifyPropertyForArrayObjType(property); |
| 99 | + String subType = getJsonObjectType(property, (JsonObject) next); |
| 100 | + type = "List" + "<" + subType + ">"; |
| 101 | + /** |
| 102 | + * 处理子类 |
| 103 | + */ |
| 104 | + toBeAppend.add(new KotlinMaker(subType, next).makeKotlinData()); |
| 105 | + } else if (next.isJsonArray()) { |
| 106 | + property = modifyPropertyForArrayObjType(property); |
| 107 | + String subType = getArrayType(property, (JsonArray) next); |
| 108 | + type = "List" + "<" + subType + ">"; |
| 109 | + |
| 110 | + } else if (next.isJsonNull()) { |
| 111 | + type = "List" + "<" + "String" + ">"; |
| 112 | + |
| 113 | + } |
| 114 | + return type; |
| 115 | + } |
| 116 | + |
| 117 | + @NotNull |
| 118 | + private String modifyPropertyForArrayObjType(String property) { |
| 119 | + if (property.endsWith("ies")) { |
| 120 | + property = property.substring(0, property.length() - 3).concat("y"); |
| 121 | + } else if (property.contains("List")) { |
| 122 | + int firstLatterAfterListIndex = property.lastIndexOf("List") + 4; |
| 123 | + if (property.length() > firstLatterAfterListIndex) { |
| 124 | + char c = property.charAt(firstLatterAfterListIndex); |
| 125 | + if (c >= 'A' && c <= 'Z') { |
| 126 | + String pre = property.substring(0, property.lastIndexOf("List")); |
| 127 | + String end = property.substring(firstLatterAfterListIndex, property.length()); |
| 128 | + property = pre.concat(end); |
| 129 | + } |
| 130 | + } else if (property.length() == firstLatterAfterListIndex) { |
| 131 | + property = property.substring(0, property.lastIndexOf("List")); |
| 132 | + } |
| 133 | + } else if (property.contains("list")) { |
| 134 | + if (property.indexOf("list") == 0) { |
| 135 | + String end = property.substring(5); |
| 136 | + String pre = (property.charAt(4) + "").toLowerCase(); |
| 137 | + property = pre.concat(end); |
| 138 | + } |
| 139 | + } else if (property.endsWith("s")) { |
| 140 | + property = property.substring(0, property.length() - 1); |
| 141 | + } |
| 142 | + |
| 143 | + return property; |
| 144 | + } |
| 145 | + |
| 146 | + private void addProperty(StringBuilder stringBuilder, String property, String type, String value) { |
| 147 | + stringBuilder.append("\t\tvar ").append(property).append(": ").append(type).append(","); |
| 148 | + if (value != null) { |
| 149 | + stringBuilder.append("// ").append(value).append("\n"); |
| 150 | + } else { |
| 151 | + stringBuilder.append("\n"); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @NotNull |
| 156 | + private String getPrimitiveType(JsonElement next) { |
| 157 | + String subType = "String"; |
| 158 | + JsonPrimitive asJsonPrimitive = next.getAsJsonPrimitive(); |
| 159 | + if (asJsonPrimitive.isBoolean()) { |
| 160 | + subType = "Boolean"; |
| 161 | + } else if (asJsonPrimitive.isNumber()) { |
| 162 | + if (asJsonPrimitive.getAsString().contains(".")) { |
| 163 | + subType = "Double"; |
| 164 | + } else if (asJsonPrimitive.getAsLong() > Integer.MAX_VALUE) { |
| 165 | + subType = "Long"; |
| 166 | + } else { |
| 167 | + subType = "Int"; |
| 168 | + } |
| 169 | + } else if (asJsonPrimitive.isString()) { |
| 170 | + subType = "String"; |
| 171 | + } |
| 172 | + return subType; |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments