mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 07:59:21 +08:00
Throw NPE for null indent string in factory method. (#289)
Fail when creating the JsonAdapter rather than when using it.
This commit is contained in:
committed by
Jesse Wilson
parent
f942e0fd52
commit
13fd0b252c
@@ -204,6 +204,9 @@ public abstract class JsonAdapter<T> {
|
|||||||
* @param indent a string containing only whitespace.
|
* @param indent a string containing only whitespace.
|
||||||
*/
|
*/
|
||||||
public JsonAdapter<T> indent(final String indent) {
|
public JsonAdapter<T> indent(final String indent) {
|
||||||
|
if (indent == null) {
|
||||||
|
throw new NullPointerException("indent == null");
|
||||||
|
}
|
||||||
final JsonAdapter<T> delegate = this;
|
final JsonAdapter<T> delegate = this;
|
||||||
return new JsonAdapter<T>() {
|
return new JsonAdapter<T>() {
|
||||||
@Override public T fromJson(JsonReader reader) throws IOException {
|
@Override public T fromJson(JsonReader reader) throws IOException {
|
||||||
|
@@ -145,6 +145,24 @@ public final class JsonAdapterTest {
|
|||||||
+ "]");
|
+ "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void indentDisallowsNull() throws Exception {
|
||||||
|
JsonAdapter<Object> adapter = new JsonAdapter<Object>() {
|
||||||
|
@Override public Object fromJson(JsonReader reader) {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void toJson(JsonWriter writer, Object value) {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
adapter.indent(null);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException expected) {
|
||||||
|
assertThat(expected).hasMessage("indent == null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test public void serializeNulls() throws Exception {
|
@Test public void serializeNulls() throws Exception {
|
||||||
JsonAdapter<Map<String, String>> serializeNulls = new JsonAdapter<Map<String, String>>() {
|
JsonAdapter<Map<String, String>> serializeNulls = new JsonAdapter<Map<String, String>>() {
|
||||||
@Override public Map<String, String> fromJson(JsonReader reader) throws IOException {
|
@Override public Map<String, String> fromJson(JsonReader reader) throws IOException {
|
||||||
|
Reference in New Issue
Block a user