Skip to content

Commit

Permalink
Support nested previous_attributes; version bump.
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag committed Mar 17, 2012
1 parent 7cbc87e commit c8ca589
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Add this dependency to your project's POM:
<dependency>
<groupId>com.stripe</groupId>
<artifactId>stripe-java</artifactId>
<version>1.0.13</version>
<version>1.0.14</version>
</dependency>

### Others
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.13
1.0.14
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.stripe</groupId>
<artifactId>stripe-java</artifactId>
<packaging>jar</packaging>
<version>1.0.13</version>
<version>1.0.14</version>
<name>stripe-java</name>
<url>https://github.com/stripe/stripe-java</url>
<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/stripe/Stripe.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public abstract class Stripe
{
public static final String API_BASE = "https://api.stripe.com/v1";
public static final String VERSION = "1.0.13";
public static final String VERSION = "1.0.14";
public static String apiKey;
}
58 changes: 54 additions & 4 deletions src/main/java/com/stripe/model/EventDataDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit c8ca589

Please sign in to comment.