|
| 1 | +package com.wyjson.router.document; |
| 2 | + |
| 3 | +import androidx.arch.core.util.Function; |
| 4 | +import androidx.core.util.Supplier; |
| 5 | + |
| 6 | +import com.google.gson.Gson; |
| 7 | +import com.google.gson.GsonBuilder; |
| 8 | +import com.google.gson.JsonElement; |
| 9 | +import com.google.gson.JsonPrimitive; |
| 10 | +import com.google.gson.JsonSerializationContext; |
| 11 | +import com.google.gson.JsonSerializer; |
| 12 | +import com.google.gson.TypeAdapter; |
| 13 | +import com.google.gson.stream.JsonReader; |
| 14 | +import com.google.gson.stream.JsonWriter; |
| 15 | +import com.wyjson.router.core.CardMeta; |
| 16 | +import com.wyjson.router.core.GoRouter; |
| 17 | +import com.wyjson.router.core.RouteHashMap; |
| 18 | +import com.wyjson.router.exception.RouterException; |
| 19 | +import com.wyjson.router.interceptor.InterceptorTreeMap; |
| 20 | +import com.wyjson.router.interfaces.IService; |
| 21 | +import com.wyjson.router.service.ServiceHashMap; |
| 22 | +import com.wyjson.router.service.ServiceMeta; |
| 23 | +import com.wyjson.router.utils.MapUtils; |
| 24 | +import com.wyjson.router.utils.TextUtils; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.lang.reflect.Type; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +public class DocumentUtils { |
| 31 | + |
| 32 | + private static Gson gson; |
| 33 | + |
| 34 | + /** |
| 35 | + * 生成JSON格式文档 |
| 36 | + * |
| 37 | + * @param documentModel |
| 38 | + * @param tagFunction 不处理返回默认int类型tag,实现方法可自定义返回tag,示例[LOGIN, AUTHENTICATION] |
| 39 | + * @return JSON格式文档 |
| 40 | + */ |
| 41 | + public static String generate(DocumentModel documentModel, Function<Integer, String> tagFunction) { |
| 42 | + if (!GoRouter.logger.isShowLog()) { |
| 43 | + throw new RouterException("Call GoRouter.openLog(); Log enabled after use!"); |
| 44 | + } |
| 45 | + String json; |
| 46 | + gson = new GsonBuilder() |
| 47 | + .registerTypeAdapter(Class.class, new ClassDataSerializer()) |
| 48 | + .registerTypeAdapter(DocumentModel.class, new DocumentModelTypeAdapter()) |
| 49 | + .registerTypeAdapter(ServiceHashMap.class, new ServiceHashMapTypeAdapter()) |
| 50 | + .registerTypeAdapter(RouteHashMap.class, new RouteHashMapTypeAdapter()) |
| 51 | + .registerTypeAdapter(ServiceMeta.class, new ServiceMetaTypeAdapter()) |
| 52 | + .registerTypeAdapter(InterceptorTreeMap.class, new InterceptorTreeMapTypeAdapter()) |
| 53 | + .registerTypeAdapter(CardMeta.class, new CardMetaTypeAdapter(tagFunction)) |
| 54 | + .create(); |
| 55 | + json = gson.toJson(documentModel); |
| 56 | + return json; |
| 57 | + } |
| 58 | + |
| 59 | + static class DocumentModelTypeAdapter extends TypeAdapter<DocumentModel> { |
| 60 | + @Override |
| 61 | + public void write(JsonWriter out, DocumentModel value) throws IOException { |
| 62 | + out.beginObject(); |
| 63 | + |
| 64 | + if (MapUtils.isNotEmpty(value.getServices())) { |
| 65 | + out.name("services").jsonValue(gson.toJson(value.getServices())); |
| 66 | + } else { |
| 67 | + out.name("services").beginObject().endObject(); |
| 68 | + } |
| 69 | + |
| 70 | + if (MapUtils.isNotEmpty(value.getInterceptors())) { |
| 71 | + out.name("interceptors").jsonValue(gson.toJson(value.getInterceptors())); |
| 72 | + } else { |
| 73 | + out.name("interceptors").beginObject().endObject(); |
| 74 | + } |
| 75 | + |
| 76 | + if (MapUtils.isNotEmpty(value.getRoutes())) { |
| 77 | + out.name("routes").jsonValue(gson.toJson(value.getRoutes())); |
| 78 | + } else { |
| 79 | + out.name("routes").beginObject().endObject(); |
| 80 | + } |
| 81 | + |
| 82 | + out.endObject(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public DocumentModel read(JsonReader in) throws IOException { |
| 87 | + return null; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + static class ServiceHashMapTypeAdapter<K, V> extends TypeAdapter<ServiceHashMap<K, V>> { |
| 92 | + |
| 93 | + @Override |
| 94 | + public void write(JsonWriter out, ServiceHashMap<K, V> map) throws IOException { |
| 95 | + if (MapUtils.isEmpty(map)) { |
| 96 | + out.nullValue(); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + out.beginObject(); |
| 101 | + for (Map.Entry<K, V> entry : map.entrySet()) { |
| 102 | + out.name(((Class<? extends IService>) entry.getKey()).getSimpleName()).jsonValue(gson.toJson(entry.getValue())); |
| 103 | + } |
| 104 | + out.endObject(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public ServiceHashMap<K, V> read(JsonReader in) throws IOException { |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + static class RouteHashMapTypeAdapter<K, V> extends TypeAdapter<RouteHashMap<K, V>> { |
| 115 | + |
| 116 | + @Override |
| 117 | + public void write(JsonWriter out, RouteHashMap<K, V> map) throws IOException { |
| 118 | + if (MapUtils.isEmpty(map)) { |
| 119 | + out.nullValue(); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + out.beginArray(); |
| 124 | + for (Map.Entry<K, V> entry : map.entrySet()) { |
| 125 | + out.jsonValue(gson.toJson(entry.getValue())); |
| 126 | + } |
| 127 | + out.endArray(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public RouteHashMap<K, V> read(JsonReader in) throws IOException { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + static class InterceptorTreeMapTypeAdapter<K, V> extends TypeAdapter<InterceptorTreeMap<K, V>> { |
| 138 | + |
| 139 | + @Override |
| 140 | + public void write(JsonWriter out, InterceptorTreeMap<K, V> map) throws IOException { |
| 141 | + if (MapUtils.isEmpty(map)) { |
| 142 | + out.nullValue(); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + out.beginObject(); |
| 147 | + for (Map.Entry<K, V> entry : map.entrySet()) { |
| 148 | + out.name(entry.getKey().toString()).value(entry.getValue().getClass().getName()); |
| 149 | + } |
| 150 | + out.endObject(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public InterceptorTreeMap<K, V> read(JsonReader in) throws IOException { |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + static class ServiceMetaTypeAdapter extends TypeAdapter<ServiceMeta> { |
| 161 | + @Override |
| 162 | + public void write(JsonWriter out, ServiceMeta value) throws IOException { |
| 163 | + out.value(value.getServiceClass().getName()); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public ServiceMeta read(JsonReader in) throws IOException { |
| 168 | + return null; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + static class CardMetaTypeAdapter extends TypeAdapter<CardMeta> { |
| 173 | + |
| 174 | + Function<Integer, String> tagFunction; |
| 175 | + |
| 176 | + public CardMetaTypeAdapter(Function<Integer, String> tagFunction) { |
| 177 | + this.tagFunction = tagFunction; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void write(JsonWriter out, CardMeta value) throws IOException { |
| 182 | + out.beginObject(); |
| 183 | + out.name("path").value(value.getPath()); |
| 184 | + out.name("type").value(value.getType().toString()); |
| 185 | + out.name("pathClass").value(value.getPathClass().getName()); |
| 186 | + if (value.getTag() != 0) { |
| 187 | + if (tagFunction != null) { |
| 188 | + String tagValue = tagFunction.apply(value.getTag()); |
| 189 | + if (!TextUtils.isEmpty(tagValue)) { |
| 190 | + out.name("tag").value(tagValue); |
| 191 | + } else { |
| 192 | + out.name("tag").value(value.getTag()); |
| 193 | + } |
| 194 | + } else { |
| 195 | + out.name("tag").value(value.getTag()); |
| 196 | + } |
| 197 | + } |
| 198 | + if (MapUtils.isNotEmpty(value.getParamsType())) { |
| 199 | + out.name("paramsType").jsonValue(gson.toJson(value.getParamsType())); |
| 200 | + } |
| 201 | + out.endObject(); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public CardMeta read(JsonReader in) throws IOException { |
| 206 | + return null; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + static class ClassDataSerializer implements JsonSerializer<Class<?>> { |
| 211 | + @Override |
| 212 | + public JsonElement serialize(Class<?> src, Type typeOfSrc, JsonSerializationContext context) { |
| 213 | + return new JsonPrimitive(src.getName()); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | +} |
0 commit comments