mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Class and Enum adapters now rely only on Options.
This commit is contained in:
@@ -21,7 +21,6 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
@@ -127,13 +126,11 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
||||
};
|
||||
|
||||
private final ClassFactory<T> classFactory;
|
||||
private final Map<String, FieldBinding<?>> fieldsMap;
|
||||
private final FieldBinding<?>[] fieldsArray;
|
||||
private final JsonReader.Options options;
|
||||
|
||||
ClassJsonAdapter(ClassFactory<T> classFactory, Map<String, FieldBinding<?>> fieldsMap) {
|
||||
this.classFactory = classFactory;
|
||||
this.fieldsMap = new LinkedHashMap<>(fieldsMap);
|
||||
this.fieldsArray = fieldsMap.values().toArray(new FieldBinding[fieldsMap.size()]);
|
||||
this.options = JsonReader.Options.of(
|
||||
fieldsMap.keySet().toArray(new String[fieldsMap.size()]));
|
||||
@@ -162,13 +159,10 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
||||
if (index != -1) {
|
||||
fieldBinding = fieldsArray[index];
|
||||
} else {
|
||||
String name = reader.nextName();
|
||||
fieldBinding = fieldsMap.get(name);
|
||||
if (fieldBinding == null) {
|
||||
reader.nextName();
|
||||
reader.skipValue();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
fieldBinding.read(reader, result);
|
||||
}
|
||||
reader.endObject();
|
||||
|
@@ -19,6 +19,7 @@ import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -214,7 +215,6 @@ final class StandardJsonAdapters {
|
||||
|
||||
static final class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
|
||||
private final Class<T> enumType;
|
||||
private final Map<String, T> nameConstantMap;
|
||||
private final String[] nameStrings;
|
||||
private final T[] constants;
|
||||
private final JsonReader.Options options;
|
||||
@@ -223,13 +223,11 @@ final class StandardJsonAdapters {
|
||||
this.enumType = enumType;
|
||||
try {
|
||||
constants = enumType.getEnumConstants();
|
||||
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);
|
||||
String name = annotation != null ? annotation.name() : constant.name();
|
||||
nameConstantMap.put(name, constant);
|
||||
nameStrings[i] = name;
|
||||
}
|
||||
options = JsonReader.Options.of(nameStrings);
|
||||
@@ -242,11 +240,10 @@ final class StandardJsonAdapters {
|
||||
int index = reader.selectString(options);
|
||||
if (index != -1) return constants[index];
|
||||
|
||||
// We can consume the string safely, we are terminating anyway.
|
||||
String name = reader.nextString();
|
||||
T constant = nameConstantMap.get(name);
|
||||
if (constant != null) return constant;
|
||||
throw new JsonDataException("Expected one of "
|
||||
+ nameConstantMap.keySet() + " but was " + name + " at path "
|
||||
+ Arrays.asList(nameStrings) + " but was " + name + " at path "
|
||||
+ reader.getPath());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user