Skip to content

Commit a718869

Browse files
committed
new EventDataDeserializer
1 parent 055e64b commit a718869

7 files changed

Lines changed: 149 additions & 40 deletions

File tree

src/com/pingplusplus/model/Event.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Event extends APIResource {
1717
private String object;
1818
private Boolean livemode;
1919
private Long created;
20-
private Map<String, Object> data;
20+
private EventData data;
2121
private Integer pendingWebhooks;
2222
private String type;
2323
private String request;
@@ -54,11 +54,11 @@ public void setCreated(Long created) {
5454
this.created = created;
5555
}
5656

57-
public Map<String, Object> getData() {
57+
public EventData getData() {
5858
return data;
5959
}
6060

61-
public void setData(Map<String, Object> data) {
61+
public void setData(EventData data) {
6262
this.data = data;
6363
}
6464

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.pingplusplus.model;
2+
3+
/**
4+
* Created by Afon on 15/12/30.
5+
*/
6+
public class EventData extends PingppObject {
7+
PingppObject object;
8+
9+
public PingppObject getObject() {
10+
return object;
11+
}
12+
13+
public void setObject(PingppObject object) {
14+
this.object = object;
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.pingplusplus.model;
2+
3+
import com.google.gson.JsonObject;
4+
5+
/**
6+
* Created by Afon on 15/12/30.
7+
*/
8+
public class PingppRawJsonObject extends PingppObject {
9+
public JsonObject json;
10+
}

src/com/pingplusplus/model/Webhooks.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,7 @@ public static Object parseEvnet(String eventStr) {
3535
* @return
3636
*/
3737
public static Object getObject(String eventStr) {
38-
Gson gson = new GsonBuilder()
39-
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
40-
.create();
41-
InnerObject innerObject = gson.fromJson(eventStr, InnerObject.class);
42-
if (null == innerObject) {
43-
return null;
44-
}
45-
JsonParser jsonParser = new JsonParser();
46-
JsonObject jsonObject = jsonParser.parse(eventStr).getAsJsonObject();
47-
String eventData = jsonObject.get("data").getAsJsonObject().get("object").getAsJsonObject().toString();
48-
if ("summary.daily.available".equals(innerObject.type)) {
49-
return gson.fromJson(eventData, Summary.class);
50-
} else if ("summary.weekly.available".equals(innerObject.type)) {
51-
return gson.fromJson(eventData, Summary.class);
52-
} else if ("summary.monthly.available".equals(innerObject.type)) {
53-
return gson.fromJson(eventData, Summary.class);
54-
} else if ("charge.succeeded".equals(innerObject.type)) {
55-
return APIResource.GSON.fromJson(eventData, Charge.class);
56-
} else if ("refund.succeeded".equals(innerObject.type)) {
57-
return gson.fromJson(eventData, Refund.class);
58-
}
59-
return null;
38+
return eventParse(eventStr).getData().getObject();
6039
}
6140

6241
/**
@@ -66,15 +45,6 @@ public static Object getObject(String eventStr) {
6645
* @return
6746
*/
6847
public static Event eventParse(String eventStr) {
69-
Gson gson = new GsonBuilder()
70-
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
71-
.create();
72-
InnerObject innerObject = gson.fromJson(eventStr, InnerObject.class);
73-
74-
if (innerObject == null || innerObject.type == null || innerObject.type.isEmpty()) {
75-
return null;
76-
}
77-
78-
return gson.fromJson(eventStr, Event.class);
48+
return APIResource.GSON.fromJson(eventStr, Event.class);
7949
}
8050
}

src/com/pingplusplus/net/APIResource.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
import com.pingplusplus.exception.AuthenticationException;
1010
import com.pingplusplus.exception.ChannelException;
1111
import com.pingplusplus.exception.InvalidRequestException;
12-
import com.pingplusplus.model.Charge;
13-
import com.pingplusplus.model.ChargeRefundCollection;
14-
import com.pingplusplus.model.PingppObject;
15-
import com.pingplusplus.model.RedEnvelope;
16-
import com.pingplusplus.model.Transfer;
12+
import com.pingplusplus.model.*;
1713

1814
import java.io.IOException;
1915
import java.io.InputStream;
@@ -55,6 +51,8 @@ protected enum RequestMethod {
5551
.registerTypeAdapter(RedEnvelope.class, new RedEnvelopeDeserializer())
5652
.registerTypeAdapter(Transfer.class, new TransferDeserializer())
5753
.registerTypeAdapter(ChargeRefundCollection.class, new ChargeRefundCollectionDeserializer())
54+
.registerTypeAdapter(EventData.class, new EventDataDeserializer())
55+
.registerTypeAdapter(PingppRawJsonObject.class, new PingppRawJsonObjectDeserializer())
5856
.create();
5957

6058
/**
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.pingplusplus.net;
2+
3+
import com.google.gson.*;
4+
import com.pingplusplus.model.*;
5+
6+
import java.lang.reflect.Type;
7+
import java.util.HashMap;
8+
import java.util.Iterator;
9+
import java.util.Map;
10+
11+
/**
12+
* Created by Afon on 15/12/30.
13+
*/
14+
public class EventDataDeserializer implements JsonDeserializer<EventData> {
15+
16+
@SuppressWarnings("rawtypes")
17+
static final Map<String, Class> objectMap = new HashMap<String, Class>();
18+
static {
19+
objectMap.put("charge", Charge.class);
20+
objectMap.put("transfer", Transfer.class);
21+
objectMap.put("refund", Refund.class);
22+
objectMap.put("red_envelope", RedEnvelope.class);
23+
objectMap.put("account_daily_summary", Summary.class);
24+
objectMap.put("account_weekly_summary", Summary.class);
25+
objectMap.put("account_monthly_summary", Summary.class);
26+
objectMap.put("app_monthly_summary", Summary.class);
27+
objectMap.put("app_daily_summary", Summary.class);
28+
objectMap.put("app_weekly_summary", Summary.class);
29+
}
30+
31+
private Object deserializeJsonPrimitive(JsonPrimitive element) {
32+
if (element.isBoolean()) {
33+
return element.getAsBoolean();
34+
} else if (element.isNumber()) {
35+
return element.getAsNumber();
36+
} else {
37+
return element.getAsString();
38+
}
39+
}
40+
41+
private Object[] deserializeJsonArray(JsonArray arr) {
42+
Object[] elems = new Object[arr.size()];
43+
Iterator<JsonElement> elemIter = arr.iterator();
44+
int i = 0;
45+
while (elemIter.hasNext()) {
46+
JsonElement elem = elemIter.next();
47+
elems[i++] = deserializeJsonElement(elem);
48+
}
49+
return elems;
50+
}
51+
52+
private Object deserializeJsonElement(JsonElement element) {
53+
if (element.isJsonNull()) {
54+
return null;
55+
} else if (element.isJsonObject()) {
56+
Map<String, Object> valueMap = new HashMap<String, Object>();
57+
populateMapFromJSONObject(valueMap, element.getAsJsonObject());
58+
return valueMap;
59+
} else if (element.isJsonPrimitive()) {
60+
return deserializeJsonPrimitive(element.getAsJsonPrimitive());
61+
} else if (element.isJsonArray()) {
62+
return deserializeJsonArray(element.getAsJsonArray());
63+
} else {
64+
System.err.println("Unknown JSON element type for element " + element + ".");
65+
return null;
66+
}
67+
}
68+
69+
private void populateMapFromJSONObject(Map<String, Object> objMap, JsonObject jsonObject) {
70+
for(Map.Entry<String, JsonElement> entry: jsonObject.entrySet()) {
71+
String key = entry.getKey();
72+
JsonElement element = entry.getValue();
73+
objMap.put(key, deserializeJsonElement(element));
74+
}
75+
}
76+
77+
@SuppressWarnings("unchecked")
78+
public EventData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
79+
throws JsonParseException {
80+
EventData eventData = new EventData();
81+
JsonObject jsonObject = json.getAsJsonObject();
82+
for(Map.Entry<String, JsonElement> entry: jsonObject.entrySet()) {
83+
String key = entry.getKey();
84+
JsonElement element = entry.getValue();
85+
if ("object".equals(key)) {
86+
String type = element.getAsJsonObject().get("object").getAsString();
87+
Class<PingppObject> cl = objectMap.get(type);
88+
PingppObject object = APIResource.GSON.fromJson(entry.getValue(), cl != null ? cl : PingppRawJsonObject.class);
89+
eventData.setObject(object);
90+
}
91+
}
92+
return eventData;
93+
}
94+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.pingplusplus.net;
2+
3+
import com.google.gson.JsonDeserializationContext;
4+
import com.google.gson.JsonDeserializer;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonParseException;
7+
import com.pingplusplus.model.PingppRawJsonObject;
8+
9+
import java.lang.reflect.Type;
10+
11+
/**
12+
* Created by Afon on 15/12/30.
13+
*/
14+
public class PingppRawJsonObjectDeserializer implements JsonDeserializer<PingppRawJsonObject> {
15+
public PingppRawJsonObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
16+
throws JsonParseException {
17+
PingppRawJsonObject object = new PingppRawJsonObject();
18+
object.json = json.getAsJsonObject();
19+
return object;
20+
}
21+
}

0 commit comments

Comments
 (0)