|
| 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 | +} |
0 commit comments