mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 07:59: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> {
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user