Skip to content

Commit fadeb35

Browse files
committed
add InterfaceAdapter to deserialize PushPayload
1 parent 8b83aaa commit fadeb35

File tree

12 files changed

+172
-181
lines changed

12 files changed

+172
-181
lines changed

example/main/java/cn/jpush/api/examples/PushExample.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
import cn.jpush.api.push.model.notification.*;
7+
import com.google.gson.Gson;
8+
import com.google.gson.GsonBuilder;
69
import org.slf4j.Logger;
710
import org.slf4j.LoggerFactory;
811

@@ -18,10 +21,6 @@
1821
import cn.jpush.api.push.model.SMS;
1922
import cn.jpush.api.push.model.audience.Audience;
2023
import cn.jpush.api.push.model.audience.AudienceTarget;
21-
import cn.jpush.api.push.model.notification.AndroidNotification;
22-
import cn.jpush.api.push.model.notification.IosAlert;
23-
import cn.jpush.api.push.model.notification.IosNotification;
24-
import cn.jpush.api.push.model.notification.Notification;
2524
import com.google.gson.JsonObject;
2625

2726
public class PushExample {
@@ -52,9 +51,14 @@ public static void testSendPush() {
5251

5352
// For push, all you need do is to build PushPayload object.
5453
PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
55-
String payloadJson = payload.toSerializeJSON().toString();
56-
System.out.print(payloadJson);
57-
PushPayload newPayload = PushPayload.fromJSON(payloadJson);
54+
System.out.println("原始String类型payload: " + payload.toString());
55+
Gson gson = new GsonBuilder()
56+
.registerTypeAdapter(PlatformNotification.class, new InterfaceAdapter<PlatformNotification>())
57+
.create();
58+
String payloadJson = gson.toJson(payload);
59+
System.out.println("gosn.toJSON: " + payloadJson);
60+
PushPayload newPayload = gson.fromJson(payloadJson, PushPayload.class);
61+
System.out.println("newPayload:" + newPayload.toString());
5862
try {
5963
PushResult result = jpushClient.sendPush(newPayload);
6064
LOG.info("Got result - " + result);

src/main/java/cn/jpush/api/push/model/Message.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public JsonElement toJSON() {
8282

8383
return json;
8484
}
85-
85+
8686
public static class Builder {
8787
private String title;
8888
private String msgContent;

src/main/java/cn/jpush/api/push/model/Platform.java

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6+
import cn.jiguang.commom.DeviceType;
67
import com.google.gson.JsonArray;
78
import com.google.gson.JsonElement;
8-
import com.google.gson.JsonObject;
99
import com.google.gson.JsonPrimitive;
1010
import cn.jiguang.commom.utils.Preconditions;
1111

1212
public class Platform implements PushModel {
1313
private static final String ALL = "all";
1414

1515
private final boolean all;
16-
private final Set<String> deviceTypes;
16+
private final Set<DeviceType> deviceTypes;
1717

18-
private Platform(boolean all, Set<String> deviceTypes) {
18+
private Platform(boolean all, Set<DeviceType> deviceTypes) {
1919
this.all = all;
2020
this.deviceTypes = deviceTypes;
2121
}
@@ -29,35 +29,35 @@ public static Platform all() {
2929
}
3030

3131
public static Platform android() {
32-
return newBuilder().addDeviceType("android").build();
32+
return newBuilder().addDeviceType(DeviceType.Android).build();
3333
}
3434

3535
public static Platform ios() {
36-
return newBuilder().addDeviceType("ios").build();
36+
return newBuilder().addDeviceType(DeviceType.IOS).build();
3737
}
3838

3939
public static Platform winphone() {
40-
return newBuilder().addDeviceType("winphone").build();
40+
return newBuilder().addDeviceType(DeviceType.WinPhone).build();
4141
}
4242

4343
public static Platform android_ios() {
4444
return newBuilder()
45-
.addDeviceType("android")
46-
.addDeviceType("ios")
45+
.addDeviceType(DeviceType.Android)
46+
.addDeviceType(DeviceType.IOS)
4747
.build();
4848
}
4949

5050
public static Platform android_winphone() {
5151
return newBuilder()
52-
.addDeviceType("android")
53-
.addDeviceType("winphone")
52+
.addDeviceType(DeviceType.Android)
53+
.addDeviceType(DeviceType.WinPhone)
5454
.build();
5555
}
5656

5757
public static Platform ios_winphone() {
5858
return newBuilder()
59-
.addDeviceType("ios")
60-
.addDeviceType("winphone")
59+
.addDeviceType(DeviceType.IOS)
60+
.addDeviceType(DeviceType.WinPhone)
6161
.build();
6262
}
6363

@@ -72,40 +72,24 @@ public JsonElement toJSON() {
7272
}
7373

7474
JsonArray json = new JsonArray();
75-
for (String deviceType : deviceTypes) {
76-
json.add(new JsonPrimitive(deviceType));
75+
for (DeviceType deviceType : deviceTypes) {
76+
json.add(new JsonPrimitive(deviceType.value()));
7777
}
7878
return json;
7979
}
8080

81-
public JsonElement toSerializeJSON() {
82-
JsonObject json = new JsonObject();
83-
if (all) {
84-
json.add(ALL, new JsonPrimitive(true));
85-
} else {
86-
json.add(ALL, new JsonPrimitive(false));
87-
}
88-
JsonArray jsonArray = new JsonArray();
89-
for (String deviceType : deviceTypes) {
90-
jsonArray.add(new JsonPrimitive(deviceType));
91-
}
92-
json.add("deviceTypes", jsonArray);
93-
return json;
94-
}
95-
96-
9781
public static class Builder {
9882
private boolean all;
99-
private Set<String> deviceTypes;
83+
private Set<DeviceType> deviceTypes;
10084

10185
public Builder setAll(boolean all) {
10286
this.all = all;
10387
return this;
10488
}
10589

106-
public Builder addDeviceType(String deviceType) {
90+
public Builder addDeviceType(DeviceType deviceType) {
10791
if (null == deviceTypes) {
108-
deviceTypes = new HashSet<String>();
92+
deviceTypes = new HashSet<DeviceType>();
10993
}
11094
deviceTypes.add(deviceType);
11195
return this;

src/main/java/cn/jpush/api/push/model/PushPayload.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -154,30 +154,6 @@ public JsonElement toJSON() {
154154
return json;
155155
}
156156

157-
public JsonElement toSerializeJSON() {
158-
JsonObject json = new JsonObject();
159-
if (null != platform) {
160-
json.add(PLATFORM, platform.toSerializeJSON());
161-
}
162-
if (null != audience) {
163-
json.add(AUDIENCE, audience.toSerializeJSON());
164-
}
165-
if (null != notification) {
166-
json.add(NOTIFICATION, notification.toJSON());
167-
}
168-
if (null != message) {
169-
json.add(MESSAGE, message.toJSON());
170-
}
171-
if (null != options) {
172-
json.add(OPTIONS, options.toJSON());
173-
}
174-
if (null != sms) {
175-
json.add(SMS, sms.toJSON());
176-
}
177-
178-
return json;
179-
}
180-
181157
public boolean isGlobalExceedLength() {
182158
int messageLength = 0;
183159
JsonObject payload = (JsonObject) this.toJSON();

src/main/java/cn/jpush/api/push/model/audience/Audience.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.HashSet;
55
import java.util.Set;
66

7+
import com.google.gson.JsonArray;
78
import com.google.gson.JsonElement;
89
import com.google.gson.JsonObject;
910
import com.google.gson.JsonPrimitive;
@@ -120,21 +121,6 @@ public JsonElement toJSON() {
120121
return json;
121122
}
122123

123-
public JsonElement toSerializeJSON() {
124-
JsonObject json = new JsonObject();
125-
if (all) {
126-
json.add(ALL, new JsonPrimitive(true));
127-
} else {
128-
json.add(ALL, new JsonPrimitive(false));
129-
}
130-
if (null != targets) {
131-
for (AudienceTarget target : targets) {
132-
json.add(target.getAudienceTypeValue(), target.toJSON());
133-
}
134-
}
135-
return json;
136-
}
137-
138124
public static class Builder {
139125
private boolean all = false;
140126
private Set<AudienceTarget> audienceBuilder = null;

src/main/java/cn/jpush/api/push/model/audience/AudienceTarget.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,85 +14,85 @@
1414
public class AudienceTarget implements PushModel {
1515
private final AudienceType audienceType;
1616
private final Set<String> values;
17-
17+
1818
private AudienceTarget(AudienceType audienceType, Set<String> values) {
1919
this.audienceType = audienceType;
2020
this.values = values;
2121
}
22-
22+
2323
public static Builder newBuilder() {
2424
return new Builder();
2525
}
26-
26+
2727
public static AudienceTarget tag(String... tag) {
2828
return newBuilder().setAudienceType(AudienceType.TAG).addAudienceTargetValues(tag).build();
2929
}
30-
30+
3131
public static AudienceTarget tag(Collection<String> tags) {
3232
return newBuilder().setAudienceType(AudienceType.TAG).addAudienceTargetValues(tags).build();
3333
}
34-
34+
3535
public static AudienceTarget tag_and(String... tag) {
3636
return newBuilder().setAudienceType(AudienceType.TAG_AND).addAudienceTargetValues(tag).build();
3737
}
38-
38+
3939
public static AudienceTarget tag_and(Collection<String> tags) {
4040
return newBuilder().setAudienceType(AudienceType.TAG_AND).addAudienceTargetValues(tags).build();
4141
}
42-
42+
4343
public static AudienceTarget alias(String... alias) {
4444
return newBuilder().setAudienceType(AudienceType.ALIAS).addAudienceTargetValues(alias).build();
4545
}
46-
46+
4747
public static AudienceTarget alias(Collection<String> aliases) {
4848
return newBuilder().setAudienceType(AudienceType.ALIAS).addAudienceTargetValues(aliases).build();
4949
}
5050

5151
public static AudienceTarget registrationId(String... registrationId) {
5252
return newBuilder().setAudienceType(AudienceType.REGISTRATION_ID).addAudienceTargetValues(registrationId).build();
5353
}
54-
54+
5555
public static AudienceTarget registrationId(Collection<String> registrationIds) {
5656
return newBuilder().setAudienceType(AudienceType.REGISTRATION_ID).addAudienceTargetValues(registrationIds).build();
5757
}
58-
59-
58+
59+
6060
public AudienceType getAudienceType() {
6161
return this.audienceType;
6262
}
63-
63+
6464
public String getAudienceTypeValue() {
6565
return this.audienceType.value();
6666
}
67-
67+
6868
public JsonElement toJSON() {
6969
JsonArray array = new JsonArray();
70-
if (null != values) {
71-
for (String value : values) {
72-
array.add(new JsonPrimitive(value));
73-
}
74-
}
70+
if (null != values) {
71+
for (String value : values) {
72+
array.add(new JsonPrimitive(value));
73+
}
74+
}
7575
return array;
7676
}
77-
78-
77+
78+
7979
public static class Builder {
8080
private AudienceType audienceType = null;
8181
private Set<String> valueBuilder = null;
82-
82+
8383
public Builder setAudienceType(AudienceType audienceType) {
8484
this.audienceType = audienceType;
8585
return this;
8686
}
87-
87+
8888
public Builder addAudienceTargetValue(String value) {
8989
if (null == valueBuilder) {
9090
valueBuilder = new HashSet<String>();
9191
}
9292
valueBuilder.add(value);
9393
return this;
9494
}
95-
95+
9696
public Builder addAudienceTargetValues(Collection<String> values) {
9797
if (null == valueBuilder) {
9898
valueBuilder = new HashSet<String>();
@@ -102,7 +102,7 @@ public Builder addAudienceTargetValues(Collection<String> values) {
102102
}
103103
return this;
104104
}
105-
105+
106106
public Builder addAudienceTargetValues(String... values) {
107107
if (null == valueBuilder) {
108108
valueBuilder = new HashSet<String>();
@@ -112,7 +112,7 @@ public Builder addAudienceTargetValues(String... values) {
112112
}
113113
return this;
114114
}
115-
115+
116116
public AudienceTarget build() {
117117
Preconditions.checkArgument(null != audienceType, "AudienceType should be set.");
118118
Preconditions.checkArgument(null != valueBuilder, "Target values should be set one at least.");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.jpush.api.push.model.notification;
2+
3+
import com.google.gson.*;
4+
5+
import java.lang.reflect.Type;
6+
7+
/**
8+
* Created by Ken on 2016/10/25.
9+
*/
10+
public class InterfaceAdapter<T> implements JsonSerializer, JsonDeserializer<T> {
11+
@Override
12+
public T deserialize(JsonElement jsonElement, Type type,
13+
JsonDeserializationContext jsonDeserializationContext)
14+
throws JsonParseException {
15+
JsonObject jsonObj = jsonElement.getAsJsonObject();
16+
String className = jsonObj.get("type").getAsString();
17+
try {
18+
Class<?> clz = Class.forName(className);
19+
return jsonDeserializationContext.deserialize(jsonElement, clz);
20+
} catch (ClassNotFoundException e) {
21+
throw new JsonParseException(e);
22+
}
23+
}
24+
25+
@Override
26+
public JsonElement serialize(Object object, Type type,
27+
JsonSerializationContext jsonSerializationContext) {
28+
JsonElement jsonEle = jsonSerializationContext.serialize(object, object.getClass());
29+
jsonEle.getAsJsonObject().addProperty("type",
30+
object.getClass().getCanonicalName());
31+
return jsonEle;
32+
}
33+
}

src/main/java/cn/jpush/api/push/model/notification/Notification.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public JsonElement toJSON() {
115115
}
116116
return json;
117117
}
118-
118+
119119
public static class Builder {
120120
private Object alert;
121121
private Set<PlatformNotification> builder;

0 commit comments

Comments
 (0)