Skip to content

Commit

Permalink
Re-order factories to allow @JsonAdapter on enums which are user-defi…
Browse files Browse the repository at this point in the history
…ned types.
  • Loading branch information
jw@squareup.com committed Nov 4, 2014
1 parent 7a3719c commit e5f0029
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ public Gson() {
factories.add(SqlDateTypeAdapter.FACTORY);
factories.add(TypeAdapters.TIMESTAMP_FACTORY);
factories.add(ArrayTypeAdapter.FACTORY);
factories.add(TypeAdapters.ENUM_FACTORY);
factories.add(TypeAdapters.CLASS_FACTORY);

// type adapters for composite and user-defined types
factories.add(new CollectionTypeAdapterFactory(constructorConstructor));
factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
factories.add(new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor));
factories.add(TypeAdapters.ENUM_FACTORY);
factories.add(new ReflectiveTypeAdapterFactory(
constructorConstructor, fieldNamingPolicy, excluder));

Expand Down Expand Up @@ -410,7 +410,7 @@ public <T> TypeAdapter<T> getAdapter(TypeToken<T> type) {
* }</pre>
* Note that since you can not override type adapter factories for String and Java primitive
* types, our stats factory will not count the number of String or primitives that will be
* read or written.
* read or written.
* @param skipPast The type adapter factory that needs to be skipped while searching for
* a matching type adapter. In most cases, you should just pass <i>this</i> (the type adapter
* factory from where {@link #getDelegateAdapter} method is being invoked).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Locale;
import junit.framework.TestCase;

/**
Expand All @@ -51,6 +52,11 @@ public void testJsonAdapterInvoked() {
User user = gson.fromJson("{'name':'Joel Leitch'}", User.class);
assertEquals("Joel", user.firstName);
assertEquals("Leitch", user.lastName);

json = gson.toJson(Foo.BAR);
assertEquals("\"bar\"", json);
Foo baz = gson.fromJson("\"baz\"", Foo.class);
assertEquals(Foo.BAZ, baz);
}

public void testJsonAdapterFactoryInvoked() {
Expand Down Expand Up @@ -208,4 +214,15 @@ private static class UserJsonAdapter extends TypeAdapter<User> {
}
}

@JsonAdapter(FooJsonAdapter.class)
private static enum Foo { BAR, BAZ }
private static class FooJsonAdapter extends TypeAdapter<Foo> {
@Override public void write(JsonWriter out, Foo value) throws IOException {
out.value(value.name().toLowerCase(Locale.US));
}

@Override public Foo read(JsonReader in) throws IOException {
return Foo.valueOf(in.nextString().toUpperCase(Locale.US));
}
}
}

0 comments on commit e5f0029

Please sign in to comment.