Skip to content

Commit

Permalink
Add failOnUnknown to MoshiConverterFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
NightlyNexus authored and JakeWharton committed Jan 30, 2017
1 parent e134cfd commit 4ec7731
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,37 @@ public static MoshiConverterFactory create() {
/** Create an instance using {@code moshi} for conversion. */
public static MoshiConverterFactory create(Moshi moshi) {
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 boolean lenient;
private final boolean failOnUnknown;
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.lenient = lenient;
this.failOnUnknown = failOnUnknown;
this.serializeNulls = serializeNulls;
}

/** Return a new factory which uses {@linkplain JsonAdapter#lenient() lenient} adapters. */
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. */
public MoshiConverterFactory withNullSerialization() {
return new MoshiConverterFactory(moshi, lenient, true);
return new MoshiConverterFactory(moshi, lenient, failOnUnknown, true);
}

@Override
Expand All @@ -81,16 +91,21 @@ public MoshiConverterFactory withNullSerialization() {
if (lenient) {
adapter = adapter.lenient();
}
if (failOnUnknown) {
adapter = adapter.failOnUnknown();
}
return new MoshiResponseBodyConverter<>(adapter);
}

@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
@Override public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
JsonAdapter<?> adapter = moshi.adapter(type, jsonAnnotations(parameterAnnotations));
if (lenient) {
adapter = adapter.lenient();
}
if (failOnUnknown) {
adapter = adapter.failOnUnknown();
}
return new MoshiRequestBodyConverter<>(adapter, serializeNulls);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.squareup.moshi.FromJson;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.JsonQualifier;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
Expand Down Expand Up @@ -120,6 +121,7 @@ interface Service {
private Service service;
private Service serviceLenient;
private Service serviceNulls;
private Service serviceFailOnUnknown;

@Before public void setUp() {
Moshi moshi = new Moshi.Builder()
Expand All @@ -139,6 +141,7 @@ interface Service {
MoshiConverterFactory factory = MoshiConverterFactory.create(moshi);
MoshiConverterFactory factoryLenient = factory.asLenient();
MoshiConverterFactory factoryNulls = factory.withNullSerialization();
MoshiConverterFactory factoryFailOnUnknown = factory.failOnUnknown();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(factory)
Expand All @@ -151,9 +154,14 @@ interface Service {
.baseUrl(server.url("/"))
.addConverterFactory(factoryNulls)
.build();
Retrofit retrofitFailOnUnknown = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(factoryFailOnUnknown)
.build();
service = retrofit.create(Service.class);
serviceLenient = retrofitLenient.create(Service.class);
serviceNulls = retrofitNulls.create(Service.class);
serviceFailOnUnknown = retrofitFailOnUnknown.create(Service.class);
}

@Test public void anInterface() throws IOException, InterruptedException {
Expand Down Expand Up @@ -222,6 +230,17 @@ interface Service {
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 {
Buffer responseBody = new Buffer()
.write(ByteString.decodeHex("EFBBBF"))
Expand Down

0 comments on commit 4ec7731

Please sign in to comment.