Kotlin-reflect QoL APIs (#875)

* Start MoshiKotlinExtensions

* Add Moshi.Builder.add() with reified typeOf

* Use non-shadowing name for addAdapter

* Implement KType.toType()

.javaType isn't implemented

* Don't re-wrap wrapped adapters

* Use new adapter functions in tests to smoke test

* Don't enable uses experimental in kotlin-reflect, only the test

* Use internal leniency API

* Use toType in addAdapter too

* Update error message in test
This commit is contained in:
Zac Sweers
2019-09-04 20:24:59 -04:00
committed by GitHub
parent 73cc9656ba
commit 9db91f2dce
8 changed files with 267 additions and 132 deletions

View File

@@ -15,6 +15,7 @@
*/
package com.squareup.moshi;
import com.squareup.moshi.internal.NonNullJsonAdapter;
import com.squareup.moshi.internal.NullSafeJsonAdapter;
import java.io.IOException;
import java.lang.annotation.Annotation;
@@ -140,29 +141,7 @@ public abstract class JsonAdapter<T> {
* handled elsewhere. This should only be used to fail on explicit nulls.
*/
@CheckReturnValue public final JsonAdapter<T> nonNull() {
final JsonAdapter<T> delegate = this;
return new JsonAdapter<T>() {
@Override public @Nullable T fromJson(JsonReader reader) throws IOException {
if (reader.peek() == JsonReader.Token.NULL) {
throw new JsonDataException("Unexpected null at " + reader.getPath());
} else {
return delegate.fromJson(reader);
}
}
@Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException {
if (value == null) {
throw new JsonDataException("Unexpected null at " + writer.getPath());
} else {
delegate.toJson(writer, value);
}
}
@Override boolean isLenient() {
return delegate.isLenient();
}
@Override public String toString() {
return delegate + ".nonNull()";
}
};
return new NonNullJsonAdapter<>(this);
}
/** Returns a JSON adapter equal to this, but is lenient when reading and writing. */

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2019 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.moshi.internal;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import java.io.IOException;
import javax.annotation.Nullable;
public final class NonNullJsonAdapter<T> extends JsonAdapter<T> {
private final JsonAdapter<T> delegate;
public NonNullJsonAdapter(JsonAdapter<T> delegate) {
this.delegate = delegate;
}
public JsonAdapter<T> delegate() {
return delegate;
}
@Nullable
@Override
public T fromJson(JsonReader reader) throws IOException {
if (reader.peek() == JsonReader.Token.NULL) {
throw new JsonDataException("Unexpected null at " + reader.getPath());
} else {
return delegate.fromJson(reader);
}
}
@Override
public void toJson(JsonWriter writer, @Nullable T value) throws IOException {
if (value == null) {
throw new JsonDataException("Unexpected null at " + writer.getPath());
} else {
delegate.toJson(writer, value);
}
}
@Override
public String toString() {
return delegate + ".nonNull()";
}
}

View File

@@ -282,7 +282,7 @@ public final class JsonAdapterTest {
@Override public void toJson(JsonWriter writer, @Nullable Boolean value) throws IOException {
throw new AssertionError();
}
}.lenient().nonNull();
}.lenient().serializeNulls();
assertThat(adapter.fromJson("true true")).isEqualTo(true);
}
}