mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Nicer failure messages on partial adapters.
Closes https://github.com/square/moshi/issues/59
This commit is contained in:
@@ -39,9 +39,18 @@ final class AdapterMethodsFactory implements JsonAdapter.Factory {
|
|||||||
final AdapterMethod fromAdapter = get(fromAdapters, type, annotations);
|
final AdapterMethod fromAdapter = get(fromAdapters, type, annotations);
|
||||||
if (toAdapter == null && fromAdapter == null) return null;
|
if (toAdapter == null && fromAdapter == null) return null;
|
||||||
|
|
||||||
final JsonAdapter<Object> delegate = toAdapter == null || fromAdapter == null
|
final JsonAdapter<Object> delegate;
|
||||||
? moshi.nextAdapter(this, type, annotations)
|
if (toAdapter == null || fromAdapter == null) {
|
||||||
: null;
|
try {
|
||||||
|
delegate = moshi.nextAdapter(this, type, annotations);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
String missingAnnotation = toAdapter == null ? "@ToJson" : "@FromJson";
|
||||||
|
throw new IllegalArgumentException("No " + missingAnnotation + " adapter for "
|
||||||
|
+ type + " annotated " + annotations);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
delegate = null;
|
||||||
|
}
|
||||||
|
|
||||||
return new JsonAdapter<Object>() {
|
return new JsonAdapter<Object>() {
|
||||||
@Override public void toJson(JsonWriter writer, Object value) throws IOException {
|
@Override public void toJson(JsonWriter writer, Object value) throws IOException {
|
||||||
|
@@ -264,6 +264,44 @@ public final class AdapterMethodsTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void adapterDoesToJsonOnly() throws Exception {
|
||||||
|
Object shapeToJsonAdapter = new Object() {
|
||||||
|
@ToJson String shapeToJson(Shape shape) {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Moshi toJsonMoshi = new Moshi.Builder()
|
||||||
|
.add(shapeToJsonAdapter)
|
||||||
|
.build();
|
||||||
|
try {
|
||||||
|
toJsonMoshi.adapter(Shape.class);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat(e).hasMessage("No @FromJson adapter for interface "
|
||||||
|
+ "com.squareup.moshi.AdapterMethodsTest$Shape annotated []");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void adapterDoesFromJsonOnly() throws Exception {
|
||||||
|
Object shapeFromJsonAdapter = new Object() {
|
||||||
|
@FromJson Shape shapeFromJson(String shape) {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Moshi fromJsonMoshi = new Moshi.Builder()
|
||||||
|
.add(shapeFromJsonAdapter)
|
||||||
|
.build();
|
||||||
|
try {
|
||||||
|
fromJsonMoshi.adapter(Shape.class);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat(e).hasMessage("No @ToJson adapter for interface "
|
||||||
|
+ "com.squareup.moshi.AdapterMethodsTest$Shape annotated []");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class Point {
|
static class Point {
|
||||||
final int x;
|
final int x;
|
||||||
final int y;
|
final int y;
|
||||||
@@ -281,4 +319,8 @@ public final class AdapterMethodsTest {
|
|||||||
return x * 37 + y;
|
return x * 37 + y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Shape {
|
||||||
|
String draw();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -332,7 +332,7 @@ public final class JsonQualifiersTest {
|
|||||||
moshi.adapter(StringAndFooString.class);
|
moshi.adapter(StringAndFooString.class);
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
assertThat(expected).hasMessage("No JsonAdapter for class java.lang.String "
|
assertThat(expected).hasMessage("No @FromJson adapter for class java.lang.String "
|
||||||
+ "annotated [@com.squareup.moshi.JsonQualifiersTest$FooPrefix()]");
|
+ "annotated [@com.squareup.moshi.JsonQualifiersTest$FooPrefix()]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -353,7 +353,7 @@ public final class JsonQualifiersTest {
|
|||||||
moshi.adapter(StringAndFooString.class);
|
moshi.adapter(StringAndFooString.class);
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
assertThat(expected).hasMessage("No JsonAdapter for class java.lang.String "
|
assertThat(expected).hasMessage("No @ToJson adapter for class java.lang.String "
|
||||||
+ "annotated [@com.squareup.moshi.JsonQualifiersTest$FooPrefix()]");
|
+ "annotated [@com.squareup.moshi.JsonQualifiersTest$FooPrefix()]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user