Re-order factories to allow @JsonAdapter on enums which are user-defined types.
This commit is contained in:
parent
6580921f9d
commit
7f8f490fdc
|
@ -232,13 +232,13 @@ public final class 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));
|
||||
|
||||
|
@ -410,7 +410,7 @@ public final class Gson {
|
|||
* }</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).
|
||||
|
|
|
@ -33,6 +33,7 @@ import com.google.gson.stream.JsonReader;
|
|||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Locale;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
|
@ -51,6 +52,11 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
|||
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() {
|
||||
|
@ -208,4 +214,15 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user