Custom Moshi Adapter的StackOverflowError

时间:2016-09-29 17:26:33

标签: java android retrofit stack-overflow moshi

我创建了一个moshi适配器来解决null String值:

public class NullStringAdapter {
    @FromJson
    public String stringFromJson(@Nullable String value) {
        if (value.equals(null)) {
            return "nulled";
        }
        return value;
    }
}

我用它创建了一个Moshi实例并将其添加到我的改造中:

Moshi moshi = new Moshi.Builder().add(new NullStringAdapter()).build();
Retrofit.Builder().baseUrl(baseURL)
        .addConverterFactory(MoshiConverterFactory.create(moshi))
        .client(client)
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .build();

}

在运行时,我从MoshiAdapterMethodsFactory中的重复方法得到一个StackOverflowError:

 Caused by: java.lang.StackOverflowError: stack size 1038KB
                  at com.squareup.moshi.Moshi.cacheKey(Moshi.java:140)
                  at com.squareup.moshi.Moshi.adapter(Moshi.java:69)
 at com.squareup.moshi.AdapterMethodsFactory$5.fromJson(AdapterMethodsFactory.java:212)
 at com.squareup.moshi.AdapterMethodsFactory$1.fromJson(AdapterMethodsFactory.java:81)     
 at com.squareup.moshi.AdapterMethodsFactory$5.fromJson(AdapterMethodsFactory.java:212)
 at com.squareup.moshi.AdapterMethodsFactory$1.fromJson(AdapterMethodsFactory.java:81)     
 at com.squareup.moshi.AdapterMethodsFactory$5.fromJson(AdapterMethodsFactory.java:212)
 at com.squareup.moshi.AdapterMethodsFactory$1.fromJson(AdapterMethodsFactory.java:81)

....等等。

两个问题代码区域是第212行:

@Override public Object fromJson(Moshi moshi, JsonReader reader)
        throws IOException, IllegalAccessException, InvocationTargetException {
      JsonAdapter<Object> delegate = moshi.adapter(parameterTypes[0], qualifierAnnotations);
      *****Object intermediate = delegate.fromJson(reader);*****
      return method.invoke(adapter, intermediate);
    }

第81行:

@Override public Object fromJson(JsonReader reader) throws IOException {
    if (fromAdapter == null) {
      return delegate.fromJson(reader);
    } else if (!fromAdapter.nullable && reader.peek() == JsonReader.Token.NULL) {
      reader.nextNull();
      return null;
    } else {
      try {
        *****return fromAdapter.fromJson(moshi, reader);*****
      } catch (IllegalAccessException e) {
        throw new AssertionError();
      } catch (InvocationTargetException e) {
        if (e.getCause() instanceof IOException) throw (IOException) e.getCause();
        throw new JsonDataException(e.getCause() + " at " + reader.getPath());
      }
    }
  }

为什么方法工厂无休止地生成这些方法?

1 个答案:

答案 0 :(得分:0)

问题在于您正在尝试将String调整为String,这就是创建循环的结果,从而产生StackOverflowError。为您希望具有"nulled"值的所有字符串创建自定义注释,如下所示:

@JsonQualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface NullableString { }

然后将适配器更改为:

public class NullStringAdapter {
  @FromJson @NullableString
  public String stringFromJson(@Nullable String value) {
    if (value.equals(null)) {
        return "nulled";
    }
    return value;
  }
}

或者如果你想强制执行这种类型或反序列化到你从json解析的每个String我不建议那个)你可以使用工厂声明你的适配器:

public static final JsonAdapter.Factory FACTORY = new JsonAdapter.Factory() {
  @Override public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
    if (!annotations.isEmpty()) return null;

    Class<?> rawType = Types.getRawType(type);
    if (rawType.equals(String.class) return /** your custom JsonAdapter here. */;

    return null;
  }
};