First, your backend implementation is wrong.
You should not send an empty string to represent an empty array.
If you can't fix it on backend side because the API are not under your control, you can try with something like this:
public final class IgnoreStringForArrays implements JsonAdapter.Factory {
@Retention(RetentionPolicy.RUNTIME)
@JsonQualifier
public @interface IgnoreJsonArrayError {
}
@Override
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
if (annotations != null && annotations.size() > 0) {
for (Annotation annotation : annotations) {
if (annotation instanceof IgnoreJsonArrayError) {
final JsonAdapter<Object> delegate = moshi.nextAdapter(this, type, Types.nextAnnotations(annotations, IgnoreJsonArrayError.class));
return new JsonAdapter<Object>() {
@Override
public Object fromJson(JsonReader reader) throws IOException {
JsonReader.Token peek = reader.peek();
if (peek != JsonReader.Token.BEGIN_ARRAY) {
reader.skipValue();
return null;
}
return delegate.fromJson(reader);
}
@Override
public void toJson(JsonWriter writer, Object value) throws IOException {
delegate.toJson(writer, value);
}
};
}
}
}
return null;
}
}
like suggested here: https://github.com/square/moshi/issues/295#issuecomment-299636385
And then annotate your listOfSomething
with: IgnoreJsonArrayError
annotation