Moshi 将 null 反序列化为空列表
Moshi deserialise null to empty list
我正在尝试编写一个 null 安全列表适配器,它将一个可为 null 的列表序列化为一个不可为 null 的对象。我知道你可以这样做:
object {
@FromJson
fun fromJson(@Nullable list: List<MyObject>?): List<MyObject> {
return list ?: emptyList()
}
@ToJson
fun toJson(@Nullable list: List<MyObject>?) = list ?: emptyList()
这适用于 List<MyObject>
,但如果我使用 List<Any>
或 List<T>
,它就不起作用。有什么办法让它适用于所有列表吗?
@FromJson
/@ToJson
适配器尚不支持像 List 这样的泛型。它们直接匹配类型。您将需要一个完全写出的 JsonAdapter.Factory。请记住将 NullToEmptyListJsonAdapter.FACTORY
添加到您的 Moshi.Builder
.
final class NullToEmptyListJsonAdapter extends JsonAdapter<List<?>> {
static final Factory FACTORY = new Factory() {
@Nullable @Override
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
if (!annotations.isEmpty()) {
return null;
}
if (Types.getRawType(type) != List.class) {
return null;
}
JsonAdapter<List<?>> objectJsonAdapter = moshi.nextAdapter(this, type, annotations);
return new NullToEmptyListJsonAdapter(objectJsonAdapter);
}
};
final JsonAdapter<List<?>> delegate;
NullToEmptyListJsonAdapter(JsonAdapter<List<?>> delegate) {
this.delegate = delegate;
}
@Override public List<?> fromJson(JsonReader reader) throws IOException {
if (reader.peek() == JsonReader.Token.NULL) {
reader.skipValue();
return emptyList();
}
return delegate.fromJson(reader);
}
@Override public void toJson(JsonWriter writer, @Nullable List<?> value) throws IOException {
if (value == null) {
throw new IllegalStateException("Wrap JsonAdapter with .nullSafe().");
}
delegate.toJson(writer, value);
}
}
我正在尝试编写一个 null 安全列表适配器,它将一个可为 null 的列表序列化为一个不可为 null 的对象。我知道你可以这样做:
object {
@FromJson
fun fromJson(@Nullable list: List<MyObject>?): List<MyObject> {
return list ?: emptyList()
}
@ToJson
fun toJson(@Nullable list: List<MyObject>?) = list ?: emptyList()
这适用于 List<MyObject>
,但如果我使用 List<Any>
或 List<T>
,它就不起作用。有什么办法让它适用于所有列表吗?
@FromJson
/@ToJson
适配器尚不支持像 List 这样的泛型。它们直接匹配类型。您将需要一个完全写出的 JsonAdapter.Factory。请记住将 NullToEmptyListJsonAdapter.FACTORY
添加到您的 Moshi.Builder
.
final class NullToEmptyListJsonAdapter extends JsonAdapter<List<?>> {
static final Factory FACTORY = new Factory() {
@Nullable @Override
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
if (!annotations.isEmpty()) {
return null;
}
if (Types.getRawType(type) != List.class) {
return null;
}
JsonAdapter<List<?>> objectJsonAdapter = moshi.nextAdapter(this, type, annotations);
return new NullToEmptyListJsonAdapter(objectJsonAdapter);
}
};
final JsonAdapter<List<?>> delegate;
NullToEmptyListJsonAdapter(JsonAdapter<List<?>> delegate) {
this.delegate = delegate;
}
@Override public List<?> fromJson(JsonReader reader) throws IOException {
if (reader.peek() == JsonReader.Token.NULL) {
reader.skipValue();
return emptyList();
}
return delegate.fromJson(reader);
}
@Override public void toJson(JsonWriter writer, @Nullable List<?> value) throws IOException {
if (value == null) {
throw new IllegalStateException("Wrap JsonAdapter with .nullSafe().");
}
delegate.toJson(writer, value);
}
}