-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support nested previous_attributes; version bump.
- Loading branch information
Showing
5 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.13 | ||
1.0.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,16 @@ | |
|
||
import java.lang.reflect.Type; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.JsonPrimitive; | ||
import com.stripe.net.APIResource; | ||
|
||
public class EventDataDeserializer implements JsonDeserializer<EventData> { | ||
|
@@ -26,6 +28,54 @@ public class EventDataDeserializer implements JsonDeserializer<EventData> { | |
objectMap.put("subscription", Subscription.class); | ||
objectMap.put("token", Token.class); | ||
} | ||
|
||
private Object deserializeJsonPrimitive(JsonPrimitive element) { | ||
if (element.isBoolean()) { | ||
return element.getAsBoolean(); | ||
} else if (element.isNumber()) { | ||
return element.getAsNumber(); | ||
} else { | ||
return element.getAsString(); | ||
} | ||
} | ||
|
||
private Object[] deserializeJsonArray(JsonArray arr) { | ||
Object[] elems = new Object[arr.size()]; | ||
Iterator<JsonElement> elemIter = arr.iterator(); | ||
int i = 0; | ||
while (elemIter.hasNext()) { | ||
JsonElement elem = elemIter.next(); | ||
elems[++i] = deserializeJsonElement(elem); | ||
} | ||
return elems; | ||
} | ||
|
||
private Object deserializeJsonElement(JsonElement element) { | ||
if (element.isJsonNull()) { | ||
return null; | ||
} else if (element.isJsonObject()) { | ||
Map<String, Object> valueMap = new HashMap<String, Object>(); | ||
populateMapFromJSONObject(valueMap, element.getAsJsonObject()); | ||
return valueMap; | ||
} else if (element.isJsonPrimitive()) { | ||
return deserializeJsonPrimitive(element.getAsJsonPrimitive()); | ||
} else if (element.isJsonArray()) { | ||
return deserializeJsonArray(element.getAsJsonArray()); | ||
} else { | ||
System.err.println("Unknown JSON element type for element " + element + ". " + | ||
"If you're seeing this messaage, it's probably a bug in the Stripe Java " + | ||
"library. Please contact us by email at [email protected]."); | ||
return null; | ||
} | ||
} | ||
|
||
private void populateMapFromJSONObject(Map<String, Object> objMap, JsonObject jsonObject) { | ||
for(Map.Entry<String, JsonElement> entry: jsonObject.entrySet()) { | ||
String key = entry.getKey(); | ||
JsonElement element = entry.getValue(); | ||
objMap.put(key, deserializeJsonElement(element)); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public EventData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
|
@@ -36,9 +86,9 @@ public EventData deserialize(JsonElement json, Type typeOfT, JsonDeserialization | |
String key = entry.getKey(); | ||
JsonElement element = entry.getValue(); | ||
if("previous_attributes".equals(key)) { | ||
Type typeOfPrevAttrs = new TypeToken<Map<String, Object>>(){}.getType(); | ||
eventData.setPreviousAttributes( | ||
(Map<String, Object>) APIResource.gson.fromJson(element, typeOfPrevAttrs)); | ||
Map<String, Object> previousAttributes = new HashMap<String, Object>(); | ||
populateMapFromJSONObject(previousAttributes, element.getAsJsonObject()); | ||
eventData.setPreviousAttributes(previousAttributes); | ||
} else if ("object".equals(key)) { | ||
String type = element.getAsJsonObject().get("object").getAsString(); | ||
Class<StripeObject> cl = objectMap.get(type); | ||
|