mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Use an array indexed by ordinal to go from enum to name.
This commit is contained in:
@@ -19,7 +19,6 @@ import java.io.IOException;
|
|||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -215,21 +214,25 @@ final class StandardJsonAdapters {
|
|||||||
|
|
||||||
static final class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
|
static final class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
|
||||||
private final Class<T> enumType;
|
private final Class<T> enumType;
|
||||||
private final Map<String, T> nameConstantMap = new LinkedHashMap<>();
|
private final Map<String, T> nameConstantMap;
|
||||||
|
private final String[] nameStrings;
|
||||||
|
|
||||||
public EnumJsonAdapter(Class<T> enumType) {
|
public EnumJsonAdapter(Class<T> enumType) {
|
||||||
this.enumType = enumType;
|
this.enumType = enumType;
|
||||||
try {
|
try {
|
||||||
T[] constants = enumType.getEnumConstants();
|
T[] constants = enumType.getEnumConstants();
|
||||||
for (T constant : constants) {
|
nameConstantMap = new LinkedHashMap<>();
|
||||||
|
nameStrings = new String[constants.length];
|
||||||
|
for (int i = 0; i < constants.length; i++) {
|
||||||
|
T constant = constants[i];
|
||||||
Json annotation = enumType.getField(constant.name()).getAnnotation(Json.class);
|
Json annotation = enumType.getField(constant.name()).getAnnotation(Json.class);
|
||||||
String name = annotation != null ? annotation.name() : constant.name();
|
String name = annotation != null ? annotation.name() : constant.name();
|
||||||
nameConstantMap.put(name, constant);
|
nameConstantMap.put(name, constant);
|
||||||
|
nameStrings[i] = name;
|
||||||
}
|
}
|
||||||
} catch (NoSuchFieldException e) {
|
} catch (NoSuchFieldException e) {
|
||||||
throw new AssertionError("Missing field in " + enumType.getName(), e);
|
throw new AssertionError("Missing field in " + enumType.getName(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public T fromJson(JsonReader reader) throws IOException {
|
@Override public T fromJson(JsonReader reader) throws IOException {
|
||||||
@@ -242,12 +245,7 @@ final class StandardJsonAdapters {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void toJson(JsonWriter writer, T value) throws IOException {
|
@Override public void toJson(JsonWriter writer, T value) throws IOException {
|
||||||
for (Map.Entry<String, T> entry : nameConstantMap.entrySet()) {
|
writer.value(nameStrings[value.ordinal()]);
|
||||||
if (entry.getValue() == value) {
|
|
||||||
writer.value(entry.getKey());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
|
Reference in New Issue
Block a user