Skip to content

Commit

Permalink
Update toString; bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag committed Jan 31, 2012
1 parent 9be708e commit 7451ed5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.11
1.0.12
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.11</version>
<version>1.0.12</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.11";
public static final String VERSION = "1.0.12";
public static String apiKey;
}
2 changes: 2 additions & 0 deletions src/main/java/com/stripe/model/EventDataDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

public class EventDataDeserializer implements JsonDeserializer<EventData> {

@SuppressWarnings("rawtypes")
static Map<String, Class> objectMap = new HashMap<String, Class>();
static {
objectMap.put("charge", Charge.class);
Expand All @@ -26,6 +27,7 @@ public class EventDataDeserializer implements JsonDeserializer<EventData> {
objectMap.put("token", Token.class);
}

@SuppressWarnings("unchecked")
public EventData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
EventData eventData = new EventData();
Expand Down
54 changes: 30 additions & 24 deletions src/main/java/com/stripe/model/StripeObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,40 @@

import java.lang.reflect.Field;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public abstract class StripeObject {

// only for debugging
public static final Gson prettyPrintGson = new GsonBuilder().
setPrettyPrinting().
serializeNulls().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).
registerTypeAdapter(EventData.class, new EventDataDeserializer()).
create();

@Override public String toString() {
StringBuilder result = new StringBuilder();
String newLine = System.getProperty("line.separator");

result.append( this.getClass().getName() );
result.append( " Object {" );
result.append(newLine);

//determine fields declared in this class only (no fields of superclass)
Field[] fields = this.getClass().getDeclaredFields();
return String.format(
"<%s@%s id=%s> JSON: %s",
this.getClass().getName(),
System.identityHashCode(this),
this.getIdString(),
prettyPrintGson.toJson(this));
}

for ( Field field : fields) {
result.append(" ");
try {
result.append(field.getName());
result.append(": ");
//requires access to private field:
result.append(field.get(this));
}
catch (IllegalAccessException ex) {
System.out.println(ex);
}
result.append(newLine);
private Object getIdString() {
try {
Field idField = this.getClass().getDeclaredField("id");
return idField.get(this);
} catch (SecurityException e) {
return "";
} catch (NoSuchFieldException e) {
return "";
} catch (IllegalArgumentException e) {
return "";
} catch (IllegalAccessException e) {
return "";
}
result.append("}");
return result.toString();
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/stripe/net/APIResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@

public abstract class APIResource extends StripeObject {

public static final Gson gson = new GsonBuilder().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).
registerTypeAdapter(EventData.class, new EventDataDeserializer()).
create();

private static String className(Class<?> clazz) { return clazz.getSimpleName().toLowerCase().replace("$",""); }
protected static String classURL(Class<?> clazz) { return String.format("%s/%ss", Stripe.API_BASE, className(clazz)); }
protected static String instanceURL(Class<?> clazz, String id) { return String.format("%s/%s", classURL(clazz), id); }
Expand All @@ -40,11 +45,6 @@ public abstract class APIResource extends StripeObject {

protected enum RequestMethod { GET, POST, DELETE }

public static final Gson gson = new GsonBuilder().setFieldNamingPolicy(
FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).registerTypeAdapter(
EventData.class, new EventDataDeserializer()).create();


private static String base64(String in) {
return new String(Base64.encodeToString(in.getBytes(), false));
}
Expand Down Expand Up @@ -225,7 +225,7 @@ protected static <T> T request(APIResource.RequestMethod method, String url, Map
if (rCode < 200 || rCode >= 300) {
handleAPIError(rBody, rCode);
}
return gson.fromJson(rBody, clazz);
return gson.fromJson(rBody, clazz);
}

private static void handleAPIError(String rBody, int rCode) throws StripeException {
Expand Down

0 comments on commit 7451ed5

Please sign in to comment.