mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Merge pull request #212 from square/jwilson.1023.nulls_in_readers
Adapter methods get nulls if they accept JsonReader/JsonWriter.
This commit is contained in:
@@ -154,7 +154,7 @@ final class AdapterMethodsFactory implements JsonAdapter.Factory {
|
||||
// void pointToJson(JsonWriter jsonWriter, Point point, JsonAdapter<?> adapter, ...) {
|
||||
Set<? extends Annotation> qualifierAnnotations = jsonAnnotations(parameterAnnotations[1]);
|
||||
return new AdapterMethod(parameterTypes[1], qualifierAnnotations, adapter, method,
|
||||
parameterTypes.length, 2, false) {
|
||||
parameterTypes.length, 2, true) {
|
||||
@Override public void toJson(Moshi moshi, JsonWriter writer, Object value)
|
||||
throws IOException, InvocationTargetException {
|
||||
invoke(writer, value);
|
||||
@@ -217,7 +217,7 @@ final class AdapterMethodsFactory implements JsonAdapter.Factory {
|
||||
// Point pointFromJson(JsonReader jsonReader) {
|
||||
// Point pointFromJson(JsonReader jsonReader, JsonAdapter<?> adapter, ...) {
|
||||
return new AdapterMethod(returnType, returnTypeAnnotations, adapter, method,
|
||||
parameterTypes.length, 1, false) {
|
||||
parameterTypes.length, 1, true) {
|
||||
@Override public Object fromJson(Moshi moshi, JsonReader reader)
|
||||
throws IOException, InvocationTargetException {
|
||||
return invoke(reader);
|
||||
|
@@ -294,6 +294,33 @@ public final class AdapterMethodsTest {
|
||||
@interface Nullable {
|
||||
}
|
||||
|
||||
@Test public void toAndFromNullJsonWithWriterAndReader() throws Exception {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new NullableIntToJsonAdapter())
|
||||
.build();
|
||||
JsonAdapter<Point> pointAdapter = moshi.adapter(Point.class);
|
||||
assertThat(pointAdapter.fromJson("{\"x\":null,\"y\":3}")).isEqualTo(new Point(-1, 3));
|
||||
assertThat(pointAdapter.toJson(new Point(-1, 3))).isEqualTo("{\"y\":3}");
|
||||
}
|
||||
|
||||
static class NullableIntToJsonAdapter {
|
||||
@FromJson int intToJson(JsonReader reader) throws IOException {
|
||||
if (reader.peek() == JsonReader.Token.NULL) {
|
||||
reader.nextNull();
|
||||
return -1;
|
||||
}
|
||||
return reader.nextInt();
|
||||
}
|
||||
|
||||
@ToJson void jsonToInt(JsonWriter writer, int value) throws IOException {
|
||||
if (value == -1) {
|
||||
writer.nullValue();
|
||||
} else {
|
||||
writer.value(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void adapterThrows() throws Exception {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new ExceptionThrowingPointJsonAdapter())
|
||||
@@ -317,10 +344,12 @@ public final class AdapterMethodsTest {
|
||||
|
||||
static class ExceptionThrowingPointJsonAdapter {
|
||||
@ToJson void pointToJson(JsonWriter writer, Point point) throws Exception {
|
||||
throw new Exception("pointToJson fail!");
|
||||
if (point != null) throw new Exception("pointToJson fail!");
|
||||
writer.nullValue();
|
||||
}
|
||||
|
||||
@FromJson Point pointFromJson(JsonReader reader) throws Exception {
|
||||
if (reader.peek() == JsonReader.Token.NULL) return reader.nextNull();
|
||||
throw new Exception("pointFromJson fail!");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user