Skip to content

Commit 6fa2760

Browse files
committed
生成JSON格式文档功能
1 parent 0782333 commit 6fa2760

File tree

17 files changed

+427
-39
lines changed

17 files changed

+427
-39
lines changed

GoRouter/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ android {
2929

3030
dependencies {
3131
implementation 'androidx.appcompat:appcompat:1.6.1'
32+
implementation 'com.google.code.gson:gson:2.10.1'
3233
}

GoRouter/src/main/java/com/wyjson/router/core/GoRouter.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
import androidx.annotation.NonNull;
1313
import androidx.annotation.Nullable;
14+
import androidx.arch.core.util.Function;
1415
import androidx.core.app.ActivityCompat;
16+
import androidx.core.util.Supplier;
1517
import androidx.fragment.app.Fragment;
1618

1719
import com.wyjson.router.callback.GoCallback;
20+
import com.wyjson.router.document.DocumentModel;
21+
import com.wyjson.router.document.DocumentUtils;
1822
import com.wyjson.router.enums.ParamType;
1923
import com.wyjson.router.enums.RouteType;
2024
import com.wyjson.router.exception.RouterException;
@@ -26,15 +30,14 @@
2630
import com.wyjson.router.interfaces.IInterceptor;
2731
import com.wyjson.router.interfaces.IService;
2832
import com.wyjson.router.interfaces.PretreatmentService;
33+
import com.wyjson.router.logger.DefaultLogger;
34+
import com.wyjson.router.logger.ILogger;
2935
import com.wyjson.router.service.ServiceHelper;
3036
import com.wyjson.router.thread.DefaultPoolExecutor;
31-
import com.wyjson.router.utils.DefaultLogger;
32-
import com.wyjson.router.utils.ILogger;
3337
import com.wyjson.router.utils.MapUtils;
3438
import com.wyjson.router.utils.TextUtils;
3539

3640
import java.lang.reflect.Field;
37-
import java.util.HashMap;
3841
import java.util.Map;
3942
import java.util.concurrent.ThreadPoolExecutor;
4043

@@ -44,7 +47,7 @@ public final class GoRouter {
4447
public static final String ROUTER_PARAM_INJECT = "router_param_inject";
4548

4649
private final Handler mHandler = new Handler(Looper.getMainLooper());
47-
private static final Map<String, CardMeta> routes = new HashMap<>();
50+
private static final Map<String, CardMeta> routes = new RouteHashMap<>();
4851
private volatile static ThreadPoolExecutor executor = DefaultPoolExecutor.getInstance();
4952
public static ILogger logger = new DefaultLogger("GoRouter");
5053

@@ -86,6 +89,20 @@ public static synchronized void printStackTrace() {
8689
logger.info(null, "[printStackTrace]");
8790
}
8891

92+
public static String generateDocument() {
93+
return generateDocument(null);
94+
}
95+
96+
/**
97+
* 生成JSON格式文档
98+
*
99+
* @param tagFunction 不处理返回默认int类型tag,实现方法可自定义返回tag,示例[LOGIN, AUTHENTICATION]
100+
* @return JSON格式文档
101+
*/
102+
public static String generateDocument(Function<Integer, String> tagFunction) {
103+
return DocumentUtils.generate(new DocumentModel(routes, ServiceHelper.getInstance().getServices(), InterceptorUtils.getInterceptors()), tagFunction);
104+
}
105+
89106
@Nullable
90107
CardMeta getCardMeta(Card card) {
91108
CardMeta cardMeta = routes.get(card.getPath());
@@ -99,8 +116,8 @@ CardMeta getCardMeta(Card card) {
99116

100117
void addCardMeta(CardMeta cardMeta) {
101118
if (cardMeta.getType() != null) {
119+
// 检查路由是否有重复提交的情况
102120
if (logger.isShowLog()) {
103-
// 检查路由是否有重复提交的情况
104121
for (Map.Entry<String, CardMeta> cardMetaEntry : routes.entrySet()) {
105122
if (TextUtils.equals(cardMetaEntry.getKey(), cardMeta.getPath())) {
106123
logger.error(null, "[addCardMeta] Path duplicate commit!!! path[" + cardMetaEntry.getValue().getPath() + "]");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.wyjson.router.core;
2+
3+
import java.util.HashMap;
4+
5+
public class RouteHashMap<K, V> extends HashMap<K, V> {
6+
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.wyjson.router.document;
2+
3+
import com.wyjson.router.core.CardMeta;
4+
import com.wyjson.router.interfaces.IInterceptor;
5+
import com.wyjson.router.interfaces.IService;
6+
import com.wyjson.router.service.ServiceMeta;
7+
8+
import java.io.Serializable;
9+
import java.util.Map;
10+
11+
public class DocumentModel implements Serializable {
12+
13+
private final Map<String, CardMeta> routes;
14+
private final Map<Class<? extends IService>, ServiceMeta> services;
15+
private final Map<Integer, IInterceptor> interceptors;
16+
17+
public DocumentModel(Map<String, CardMeta> routes, Map<Class<? extends IService>, ServiceMeta> services, Map<Integer, IInterceptor> interceptors) {
18+
this.routes = routes;
19+
this.services = services;
20+
this.interceptors = interceptors;
21+
}
22+
23+
public Map<String, CardMeta> getRoutes() {
24+
return routes;
25+
}
26+
27+
public Map<Class<? extends IService>, ServiceMeta> getServices() {
28+
return services;
29+
}
30+
31+
public Map<Integer, IInterceptor> getInterceptors() {
32+
return interceptors;
33+
}
34+
35+
}
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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+
}

GoRouter/src/main/java/com/wyjson/router/interceptor/UniqueKeyTreeMap.java renamed to GoRouter/src/main/java/com/wyjson/router/interceptor/InterceptorTreeMap.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
import java.util.Iterator;
99
import java.util.TreeMap;
1010

11-
public class UniqueKeyTreeMap<K, V> extends TreeMap<K, V> {
11+
/**
12+
* UniqueKeyTreeMap
13+
*
14+
* @param <K>
15+
* @param <V>
16+
*/
17+
public class InterceptorTreeMap<K, V> extends TreeMap<K, V> {
1218
private final String tipText;
1319

14-
public UniqueKeyTreeMap(String exceptionText) {
20+
public InterceptorTreeMap(String exceptionText) {
1521
super();
1622
tipText = exceptionText;
1723
}

GoRouter/src/main/java/com/wyjson/router/interceptor/InterceptorUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class InterceptorUtils {
1111
private InterceptorUtils() {
1212
}
1313

14-
private static final Map<Integer, IInterceptor> interceptors = new UniqueKeyTreeMap<>("More than one interceptors use same priority [%s]");
14+
private static final Map<Integer, IInterceptor> interceptors = new InterceptorTreeMap<>("More than one interceptors use same priority [%s]");
1515

1616
public static Map<Integer, IInterceptor> getInterceptors() {
1717
return interceptors;

GoRouter/src/main/java/com/wyjson/router/utils/DefaultLogger.java renamed to GoRouter/src/main/java/com/wyjson/router/logger/DefaultLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.wyjson.router.utils;
1+
package com.wyjson.router.logger;
22

33
import android.text.TextUtils;
44
import android.util.Log;

GoRouter/src/main/java/com/wyjson/router/utils/ILogger.java renamed to GoRouter/src/main/java/com/wyjson/router/logger/ILogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.wyjson.router.utils;
1+
package com.wyjson.router.logger;
22

33
public interface ILogger {
44

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.wyjson.router.service;
2+
3+
import java.util.HashMap;
4+
5+
public class ServiceHashMap<K, V> extends HashMap<K, V> {
6+
7+
}

0 commit comments

Comments
 (0)