提交 4ec77317 编写于 作者: E Eric Cochran 提交者: Jake Wharton

Add failOnUnknown to MoshiConverterFactory

上级 e134cfd0
...@@ -51,27 +51,37 @@ public final class MoshiConverterFactory extends Converter.Factory { ...@@ -51,27 +51,37 @@ public final class MoshiConverterFactory extends Converter.Factory {
/** Create an instance using {@code moshi} for conversion. */ /** Create an instance using {@code moshi} for conversion. */
public static MoshiConverterFactory create(Moshi moshi) { public static MoshiConverterFactory create(Moshi moshi) {
if (moshi == null) throw new NullPointerException("moshi == null"); if (moshi == null) throw new NullPointerException("moshi == null");
return new MoshiConverterFactory(moshi, false, false); return new MoshiConverterFactory(moshi, false, false, false);
} }
private final Moshi moshi; private final Moshi moshi;
private final boolean lenient; private final boolean lenient;
private final boolean failOnUnknown;
private final boolean serializeNulls; private final boolean serializeNulls;
private MoshiConverterFactory(Moshi moshi, boolean lenient, boolean serializeNulls) { private MoshiConverterFactory(Moshi moshi, boolean lenient, boolean failOnUnknown,
boolean serializeNulls) {
this.moshi = moshi; this.moshi = moshi;
this.lenient = lenient; this.lenient = lenient;
this.failOnUnknown = failOnUnknown;
this.serializeNulls = serializeNulls; this.serializeNulls = serializeNulls;
} }
/** Return a new factory which uses {@linkplain JsonAdapter#lenient() lenient} adapters. */ /** Return a new factory which uses {@linkplain JsonAdapter#lenient() lenient} adapters. */
public MoshiConverterFactory asLenient() { public MoshiConverterFactory asLenient() {
return new MoshiConverterFactory(moshi, true, serializeNulls); return new MoshiConverterFactory(moshi, true, failOnUnknown, serializeNulls);
}
/**
* Return a new factory which uses {@link JsonAdapter#failOnUnknown()} adapters.
*/
public MoshiConverterFactory failOnUnknown() {
return new MoshiConverterFactory(moshi, lenient, true, serializeNulls);
} }
/** Return a new factory which includes null values into the serialized JSON. */ /** Return a new factory which includes null values into the serialized JSON. */
public MoshiConverterFactory withNullSerialization() { public MoshiConverterFactory withNullSerialization() {
return new MoshiConverterFactory(moshi, lenient, true); return new MoshiConverterFactory(moshi, lenient, failOnUnknown, true);
} }
@Override @Override
...@@ -81,16 +91,21 @@ public final class MoshiConverterFactory extends Converter.Factory { ...@@ -81,16 +91,21 @@ public final class MoshiConverterFactory extends Converter.Factory {
if (lenient) { if (lenient) {
adapter = adapter.lenient(); adapter = adapter.lenient();
} }
if (failOnUnknown) {
adapter = adapter.failOnUnknown();
}
return new MoshiResponseBodyConverter<>(adapter); return new MoshiResponseBodyConverter<>(adapter);
} }
@Override @Override public Converter<?, RequestBody> requestBodyConverter(Type type,
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
JsonAdapter<?> adapter = moshi.adapter(type, jsonAnnotations(parameterAnnotations)); JsonAdapter<?> adapter = moshi.adapter(type, jsonAnnotations(parameterAnnotations));
if (lenient) { if (lenient) {
adapter = adapter.lenient(); adapter = adapter.lenient();
} }
if (failOnUnknown) {
adapter = adapter.failOnUnknown();
}
return new MoshiRequestBodyConverter<>(adapter, serializeNulls); return new MoshiRequestBodyConverter<>(adapter, serializeNulls);
} }
......
...@@ -17,6 +17,7 @@ package retrofit2.converter.moshi; ...@@ -17,6 +17,7 @@ package retrofit2.converter.moshi;
import com.squareup.moshi.FromJson; import com.squareup.moshi.FromJson;
import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.JsonQualifier; import com.squareup.moshi.JsonQualifier;
import com.squareup.moshi.JsonReader; import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter; import com.squareup.moshi.JsonWriter;
...@@ -120,6 +121,7 @@ public final class MoshiConverterFactoryTest { ...@@ -120,6 +121,7 @@ public final class MoshiConverterFactoryTest {
private Service service; private Service service;
private Service serviceLenient; private Service serviceLenient;
private Service serviceNulls; private Service serviceNulls;
private Service serviceFailOnUnknown;
@Before public void setUp() { @Before public void setUp() {
Moshi moshi = new Moshi.Builder() Moshi moshi = new Moshi.Builder()
...@@ -139,6 +141,7 @@ public final class MoshiConverterFactoryTest { ...@@ -139,6 +141,7 @@ public final class MoshiConverterFactoryTest {
MoshiConverterFactory factory = MoshiConverterFactory.create(moshi); MoshiConverterFactory factory = MoshiConverterFactory.create(moshi);
MoshiConverterFactory factoryLenient = factory.asLenient(); MoshiConverterFactory factoryLenient = factory.asLenient();
MoshiConverterFactory factoryNulls = factory.withNullSerialization(); MoshiConverterFactory factoryNulls = factory.withNullSerialization();
MoshiConverterFactory factoryFailOnUnknown = factory.failOnUnknown();
Retrofit retrofit = new Retrofit.Builder() Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/")) .baseUrl(server.url("/"))
.addConverterFactory(factory) .addConverterFactory(factory)
...@@ -151,9 +154,14 @@ public final class MoshiConverterFactoryTest { ...@@ -151,9 +154,14 @@ public final class MoshiConverterFactoryTest {
.baseUrl(server.url("/")) .baseUrl(server.url("/"))
.addConverterFactory(factoryNulls) .addConverterFactory(factoryNulls)
.build(); .build();
Retrofit retrofitFailOnUnknown = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(factoryFailOnUnknown)
.build();
service = retrofit.create(Service.class); service = retrofit.create(Service.class);
serviceLenient = retrofitLenient.create(Service.class); serviceLenient = retrofitLenient.create(Service.class);
serviceNulls = retrofitNulls.create(Service.class); serviceNulls = retrofitNulls.create(Service.class);
serviceFailOnUnknown = retrofitFailOnUnknown.create(Service.class);
} }
@Test public void anInterface() throws IOException, InterruptedException { @Test public void anInterface() throws IOException, InterruptedException {
...@@ -222,6 +230,17 @@ public final class MoshiConverterFactoryTest { ...@@ -222,6 +230,17 @@ public final class MoshiConverterFactoryTest {
assertEquals("{\"theName\":null}", server.takeRequest().getBody().readUtf8()); assertEquals("{\"theName\":null}", server.takeRequest().getBody().readUtf8());
} }
@Test public void failOnUnknown() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("{\"taco\":\"delicious\"}"));
Call<AnImplementation> call = serviceFailOnUnknown.anImplementation(new AnImplementation(null));
try {
call.execute();
} catch (JsonDataException e) {
assertThat(e).hasMessage("Cannot skip unexpected STRING at $.taco");
}
}
@Test public void utf8BomSkipped() throws IOException { @Test public void utf8BomSkipped() throws IOException {
Buffer responseBody = new Buffer() Buffer responseBody = new Buffer()
.write(ByteString.decodeHex("EFBBBF")) .write(ByteString.decodeHex("EFBBBF"))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册