Merge pull request #252 from square/jwilson.0204.use_fromJsonObject

Use readJsonValue() in DefaultOnDataMismatchAdapter.
This commit is contained in:
Jesse Wilson
2017-02-04 15:13:04 -05:00
committed by GitHub

View File

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