Nicer failure messages on partial adapters.

Closes https://github.com/square/moshi/issues/59
This commit is contained in:
jwilson
2015-06-21 15:25:54 -04:00
parent 4b7532bfb3
commit d87bf52053
3 changed files with 56 additions and 5 deletions

View File

@@ -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 {

View File

@@ -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();
}
} }

View File

@@ -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()]");
} }
} }