Class and Enum adapters now rely only on Options.

This commit is contained in:
Serj Lotutovici 2017-01-22 23:06:33 +01:00
parent db30df146f
commit 5904b9ce8b
2 changed files with 7 additions and 16 deletions

View file

@ -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;
@ -122,18 +121,16 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
/** Returns true if fields with {@code modifiers} are included in the emitted JSON. */
private boolean includeField(boolean platformType, int modifiers) {
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) return false;
return Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)|| !platformType;
return Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers) || !platformType;
}
};
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,12 +159,9 @@ 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.skipValue();
continue;
}
reader.nextName();
reader.skipValue();
continue;
}
fieldBinding.read(reader, result);
}

View file

@ -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());
}