Use readJsonValue() in DefaultOnDataMismatchAdapter.

This commit is contained in:
jwilson
2017-02-04 14:22:24 -05:00
parent aebd860d1c
commit 559e3a22ea

View File

@@ -28,20 +28,17 @@ import java.util.Set;
public final class DefaultOnDataMismatchAdapter<T> extends JsonAdapter<T> {
private final JsonAdapter<T> delegate;
private final T defaultValue;
private final JsonAdapter<Object> objectAdapter;
private DefaultOnDataMismatchAdapter(JsonAdapter<T> delegate, T defaultValue,
JsonAdapter<Object> objectAdapter) {
private DefaultOnDataMismatchAdapter(JsonAdapter<T> delegate, T defaultValue) {
this.delegate = delegate;
this.defaultValue = defaultValue;
this.objectAdapter = objectAdapter;
}
@Override public T fromJson(JsonReader reader) throws IOException {
// Read the value first so that the reader will be in a known state even if there's an
// exception. Otherwise it may be awkward to recover: it might be between calls to
// beginObject() and endObject() for example.
Object jsonValue = objectAdapter.fromJson(reader);
Object jsonValue = reader.readJsonValue();
// Use the delegate to convert the JSON value to the target type.
try {
@@ -60,9 +57,8 @@ public final class DefaultOnDataMismatchAdapter<T> extends JsonAdapter<T> {
@Override public JsonAdapter<?> create(
Type requestedType, Set<? extends Annotation> annotations, Moshi moshi) {
if (type != requestedType) return null;
JsonAdapter<Object> objectAdapter = moshi.adapter(Object.class);
JsonAdapter<T> delegate = moshi.nextAdapter(this, type, annotations);
return new DefaultOnDataMismatchAdapter<>(delegate, defaultValue, objectAdapter);
return new DefaultOnDataMismatchAdapter<>(delegate, defaultValue);
}
};
}