Skip to content

Commit 0966f9f

Browse files
committed
fixed #236
1 parent ffd8381 commit 0966f9f

16 files changed

+208
-34
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cn.leancloud.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target({ElementType.FIELD})
10+
public @interface JsonField {
11+
String value();
12+
}

core/src/main/java/cn/leancloud/gson/FileUploadTokenAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.google.gson.stream.JsonWriter;
88

99
import java.io.IOException;
10-
10+
@Deprecated
1111
public class FileUploadTokenAdapter extends TypeAdapter<FileUploadToken> {
1212
private static final String FIELD_BUCKET = "bucket";
1313
private static final String FIELD_OBJECTID = "objectId";

core/src/main/java/cn/leancloud/gson/GSONParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import cn.leancloud.json.JSONParser;
66
import cn.leancloud.json.TypeReference;
77
import com.google.gson.JsonArray;
8-
import com.google.gson.JsonObject;
98

109
import java.util.ArrayList;
1110
import java.util.List;

core/src/main/java/cn/leancloud/gson/GeneralFieldMappingObjectAdapter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
import java.lang.reflect.Type;
1212
import java.util.*;
1313

14+
/**
15+
* deprecated since 8.2.19.
16+
* @param <T> generic type.
17+
*/
18+
19+
@Deprecated
1420
public class GeneralFieldMappingObjectAdapter<T> extends TypeAdapter<T> {
1521
private Class targetClazz;
1622
private Map<String, Type> displayFields;
@@ -90,6 +96,7 @@ public T read(JsonReader jsonReader) throws IOException {
9096
try {
9197
Field field = result.getClass().getDeclaredField(identifyFieldName);
9298
if (null == valueType || null == field) {
99+
jsonReader.skipValue();
93100
continue;
94101
}
95102
field.setAccessible(true);
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package cn.leancloud.gson;
2+
3+
import cn.leancloud.annotation.JsonField;
4+
import com.google.gson.TypeAdapter;
5+
import com.google.gson.stream.JsonReader;
6+
import com.google.gson.stream.JsonToken;
7+
import com.google.gson.stream.JsonWriter;
8+
9+
import java.io.IOException;
10+
import java.lang.reflect.Field;
11+
import java.lang.reflect.Type;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
public class GeneralSimpleObjectAdapter<T> extends TypeAdapter<T> {
16+
private Class targetClazz;
17+
private Map<String, Field> displayFields = new HashMap<>();
18+
19+
public GeneralSimpleObjectAdapter(Class clazz) {
20+
this.targetClazz = clazz;
21+
Field[] fields = clazz.getDeclaredFields();
22+
for (Field field: fields) {
23+
String fieldName = field.getName();
24+
JsonField annotation = field.getAnnotation(JsonField.class);
25+
if (null != annotation) {
26+
fieldName = annotation.value();
27+
}
28+
displayFields.put(fieldName, field);
29+
}
30+
}
31+
@Override
32+
public void write(JsonWriter jsonWriter, T t) throws IOException {
33+
jsonWriter.beginObject();
34+
Field[] fields = t.getClass().getDeclaredFields();
35+
for(Field field : fields) {
36+
field.setAccessible(true);
37+
String canonicalName = field.getName();
38+
String outputName = canonicalName;
39+
JsonField jsonField = field.getAnnotation(JsonField.class);
40+
if (null != jsonField) {
41+
outputName = jsonField.value();
42+
}
43+
Type valueType = field.getType();
44+
try {
45+
if (valueType.equals(Character.class) || valueType.equals(char.class)) {
46+
char value = field.getChar(t);
47+
jsonWriter.name(outputName).value(value);
48+
} else if (valueType.equals(Boolean.class) || valueType.equals(boolean.class)) {
49+
boolean value = field.getBoolean(t);
50+
jsonWriter.name(outputName).value(value);
51+
} else if (valueType.equals(String.class)) {
52+
String value = (String)field.get(t);
53+
jsonWriter.name(outputName).value(value);
54+
} else if (valueType.equals(Integer.class) || valueType.equals(int.class)) {
55+
Integer value = (Integer) field.get(t);
56+
jsonWriter.name(outputName).value(value);
57+
} else if (valueType.equals(Long.class) || valueType.equals(long.class)) {
58+
Long value = (Long) field.get(t);
59+
jsonWriter.name(outputName).value(value);
60+
} else if (valueType.equals(Float.class) || valueType.equals(float.class)) {
61+
Float value = (Float) field.get(t);
62+
jsonWriter.name(outputName).value(value);
63+
} else if (valueType.equals(Double.class) || valueType.equals(double.class)) {
64+
Double value = (Double) field.get(t);
65+
jsonWriter.name(outputName).value(value);
66+
}
67+
} catch (Exception ex) {
68+
ex.printStackTrace();
69+
}
70+
}
71+
jsonWriter.endObject();
72+
jsonWriter.flush();
73+
}
74+
75+
@Override
76+
public T read(JsonReader jsonReader) throws IOException {
77+
try {
78+
T result = (T)this.targetClazz.newInstance();
79+
jsonReader.beginObject();
80+
String jsonFieldName = null;
81+
while(jsonReader.hasNext()) {
82+
JsonToken token = jsonReader.peek();
83+
if (token.equals(JsonToken.NAME)) {
84+
// get current token.
85+
jsonFieldName = jsonReader.nextName();
86+
}
87+
Field targetField = this.displayFields.get(jsonFieldName);
88+
// move to next token
89+
jsonReader.peek();
90+
try {
91+
if (null == targetField) {
92+
jsonReader.skipValue();
93+
continue;
94+
}
95+
targetField.setAccessible(true);
96+
Object value = null;
97+
Type valueType = targetField.getType();
98+
if (valueType.equals(String.class)) {
99+
value = jsonReader.nextString();
100+
} else if (valueType.equals(Integer.class) || valueType.equals(int.class)) {
101+
value = jsonReader.nextInt();
102+
} else if (valueType.equals(Boolean.class) || valueType.equals(boolean.class)) {
103+
value = jsonReader.nextBoolean();
104+
} else if (valueType.equals(Character.class) || valueType.equals(char.class)) {
105+
value = jsonReader.nextString();
106+
} else if (valueType.equals(Long.class) || valueType.equals(long.class)) {
107+
value = jsonReader.nextLong();
108+
} else if (valueType.equals(Float.class) || valueType.equals(float.class)) {
109+
value = jsonReader.nextDouble();
110+
} else if (valueType.equals(Double.class) || valueType.equals(double.class)) {
111+
value = jsonReader.nextDouble();
112+
}
113+
targetField.set(result, value);
114+
} catch (Exception ex) {
115+
ex.printStackTrace();
116+
}
117+
}
118+
jsonReader.endObject();
119+
return result;
120+
} catch (InstantiationException e) {
121+
throw new RuntimeException(e);
122+
} catch (IllegalAccessException e) {
123+
throw new RuntimeException(e);
124+
}
125+
}
126+
}

core/src/main/java/cn/leancloud/gson/GsonWrapper.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@ public class GsonWrapper {
2323
static final BaseOperationAdapter baseOperationAdapter = new BaseOperationAdapter();
2424
static final JSONObjectAdapter jsonObjectAdapter = new JSONObjectAdapter();
2525
static final JSONArrayAdapter jsonArrayAdapter = new JSONArrayAdapter();
26-
static final Map<String, Type> appAccessEndpointFields = new HashMap<String, Type>() {{
27-
put("ttl", Long.class);
28-
put("stats_server", String.class);
29-
put("push_server", String.class);
30-
put("rtm_router_server", String.class);
31-
put("api_server", String.class);
32-
put("engine_server", String.class);
33-
}};
34-
static final Map<String, Type> captchaDigestFields = new HashMap<String, Type>() {{
35-
put("captcha_token", String.class);
36-
put("captcha_url", String.class);
37-
}};
38-
static final Map<String, Type> captchaValidateResultFields = new HashMap<String, Type>() {{
39-
put("validate_token", String.class);
40-
}};
4126

4227
static final Gson gson = new GsonBuilder().serializeNulls()
4328
.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE)
@@ -68,16 +53,14 @@ public class GsonWrapper {
6853
.registerTypeAdapter(JSONObject.class, jsonObjectAdapter)
6954
.registerTypeAdapter(GsonArray.class, jsonArrayAdapter)
7055
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
71-
.registerTypeAdapter(FileUploadToken.class, new FileUploadTokenAdapter())
56+
.registerTypeAdapter(FileUploadToken.class,
57+
new GeneralSimpleObjectAdapter<FileUploadToken>(FileUploadToken.class))
7258
.registerTypeAdapter(AppAccessEndpoint.class,
73-
new GeneralFieldMappingObjectAdapter<AppAccessEndpoint>(AppAccessEndpoint.class,
74-
appAccessEndpointFields, FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES ))
59+
new GeneralSimpleObjectAdapter<AppAccessEndpoint>(AppAccessEndpoint.class))
7560
.registerTypeAdapter(LCCaptchaDigest.class,
76-
new GeneralFieldMappingObjectAdapter<LCCaptchaDigest>(LCCaptchaDigest.class,
77-
captchaDigestFields, FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES))
61+
new GeneralSimpleObjectAdapter<LCCaptchaDigest>(LCCaptchaDigest.class))
7862
.registerTypeAdapter(LCCaptchaValidateResult.class,
79-
new GeneralFieldMappingObjectAdapter<LCCaptchaValidateResult>(LCCaptchaValidateResult.class,
80-
captchaValidateResultFields, FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES))
63+
new GeneralSimpleObjectAdapter<LCCaptchaValidateResult>(LCCaptchaValidateResult.class))
8164
.registerTypeAdapter(new TypeToken<Map<String, Object>>(){}.getType(), new MapDeserializerDoubleAsIntFix())
8265
.registerTypeAdapter(Map.class, new MapDeserializerDoubleAsIntFix())
8366
.setLenient()

core/src/main/java/cn/leancloud/gson/JSONObjectAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cn.leancloud.gson;
22

33
import cn.leancloud.json.JSONObject;
4-
import com.google.gson.Gson;
54
import com.google.gson.JsonElement;
65
import com.google.gson.JsonObject;
76
import com.google.gson.TypeAdapter;
@@ -12,6 +11,7 @@
1211
import java.io.IOException;
1312

1413
public class JSONObjectAdapter extends TypeAdapter<JSONObject> {
14+
private MapDeserializerDoubleAsIntFix mapDeserializerDoubleAsIntFix = new MapDeserializerDoubleAsIntFix();
1515
public void write(JsonWriter writer, JSONObject object) throws IOException {
1616
if (!(object instanceof GsonObject)) {
1717
writer.nullValue();

core/src/main/java/cn/leancloud/gson/MapDeserializerDoubleAsIntFix.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package cn.leancloud.gson;
22

33
import com.google.gson.*;
4-
import com.google.gson.internal.LinkedTreeMap;
4+
//import com.google.gson.internal.LinkedTreeMap;
55

66
import java.lang.reflect.Type;
7-
import java.util.ArrayList;
8-
import java.util.List;
9-
import java.util.Map;
10-
import java.util.Set;
7+
import java.util.*;
118

129
public class MapDeserializerDoubleAsIntFix implements JsonDeserializer<Map<String, Object>> {
1310

@@ -26,7 +23,7 @@ public Object read(JsonElement in) {
2623
}
2724
return list;
2825
}else if(in.isJsonObject()){
29-
Map<String, Object> map = new LinkedTreeMap<String, Object>();
26+
Map<String, Object> map = new HashMap<>();
3027
JsonObject obj = in.getAsJsonObject();
3128
Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
3229
for(Map.Entry<String, JsonElement> entry: entitySet){

core/src/main/java/cn/leancloud/gson/ObjectDeserializer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import cn.leancloud.ops.Utils;
66
import cn.leancloud.utils.StringUtil;
77
import com.google.gson.*;
8-
import com.google.gson.internal.LinkedTreeMap;
98

109
import java.lang.reflect.Type;
1110
import java.util.Collection;
@@ -37,7 +36,7 @@ private LCObject generateObject(Map<String, Object> objectMap, String className)
3736
className = (String) objectMap.get(LCObject.KEY_CLASSNAME);
3837
objectMap.remove(LCObject.KEY_CLASSNAME);
3938
if (objectMap.containsKey(KEY_SERVERDATA)) {
40-
LinkedTreeMap<String, Object> serverData = (LinkedTreeMap<String, Object>) objectMap.get(KEY_SERVERDATA);//
39+
Map<String, Object> serverData = (Map<String, Object>) objectMap.get(KEY_SERVERDATA);//
4140
objectMap.remove(KEY_SERVERDATA);
4241
objectMap.putAll(serverData);
4342
}

core/src/main/java/cn/leancloud/service/AppAccessEndpoint.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package cn.leancloud.service;
22

3+
import cn.leancloud.annotation.JsonField;
34
import cn.leancloud.core.LeanService;
45
import cn.leancloud.utils.StringUtil;
56

67
public class AppAccessEndpoint {
78
private long ttl;
89

10+
@JsonField("stats_server")
911
private String statsServer;
1012

13+
@JsonField("push_server")
1114
private String pushServer;
1215

16+
@JsonField("rtm_router_server")
1317
private String rtmRouterServer;
1418

19+
@JsonField("api_server")
1520
private String apiServer;
1621

22+
@JsonField("engine_server")
1723
private String engineServer;
1824

1925
public long getTtl() {

0 commit comments

Comments
 (0)