mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Use readJsonValue() in DefaultOnDataMismatchAdapter.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user