fastjson 构造函数提供参数设置的情况下,可以解析private final 修饰的字段吗?

q8l4jmvw  于 2021-11-27  发布在  Java
关注(0)|答案(7)|浏览(204)

VictorAlbertos/RxCache#106
使用RxCache库的时候,自己实现了FastJsonJolyglot,然而还是解析失败,原因竟然是他们的实体类是这样定义的:

public final class ConfigProvider {
  private final String providerKey;
  private final Boolean useExpiredDataIfNotLoaderAvailable;
  private final Long lifeTime;
  private final boolean requiredDetailedResponse;
  private final boolean expirable;
  private final boolean encrypted;
  private final String dynamicKey, dynamicKeyGroup;
  private final Observable loaderObservable;
  private final EvictProvider evictProvider;

  public ConfigProvider(String providerKey, Boolean useExpiredDataIfNotLoaderAvailable,
      Long lifeTime, boolean requiredDetailedResponse,
      boolean expirable, boolean encrypted, String dynamicKey, String dynamicKeyGroup,
      Observable loaderObservable, EvictProvider evictProvider) {
    this.providerKey = providerKey;
    this.useExpiredDataIfNotLoaderAvailable = useExpiredDataIfNotLoaderAvailable;
    this.lifeTime = lifeTime;
    this.requiredDetailedResponse = requiredDetailedResponse;
    this.expirable = expirable;
    this.encrypted = encrypted;
    this.dynamicKey = dynamicKey;
    this.dynamicKeyGroup = dynamicKeyGroup;
    this.loaderObservable = loaderObservable;
    this.evictProvider = evictProvider;
    checkIntegrity();
  }

  public String getProviderKey() {
    return providerKey;
  }

  public String getDynamicKey() {
    return dynamicKey;
  }

  public String getDynamicKeyGroup() {
    return dynamicKeyGroup;
  }

  public Long getLifeTimeMillis() {
    return lifeTime;
  }

  public boolean requiredDetailedResponse() {
    return requiredDetailedResponse;
  }

  public Observable getLoaderObservable() {
    return loaderObservable;
  }

  public EvictProvider evictProvider() {
    return evictProvider;
  }

  public boolean isExpirable() {
    return expirable;
  }

  public boolean isEncrypted() {
    return encrypted;
  }

  public Boolean useExpiredDataIfNotLoaderAvailable() {
    return useExpiredDataIfNotLoaderAvailable;
  }

  private void checkIntegrity() {
    if (evictProvider() instanceof io.rx_cache2.EvictDynamicKeyGroup
        && getDynamicKeyGroup().isEmpty()) {
      String errorMessage = providerKey
          + Locale.EVICT_DYNAMIC_KEY_GROUP_PROVIDED_BUT_NOT_PROVIDED_ANY_DYNAMIC_KEY_GROUP;
      throw new IllegalArgumentException(errorMessage);
    }

    if (evictProvider() instanceof io.rx_cache2.EvictDynamicKey
        && getDynamicKey().isEmpty()) {
      String errorMessage =
          providerKey + Locale.EVICT_DYNAMIC_KEY_PROVIDED_BUT_NOT_PROVIDED_ANY_DYNAMIC_KEY;
      throw new IllegalArgumentException(errorMessage);
    }
  }
}
m3eecexj

m3eecexj1#

能否支持这种实体类的解析呢?
https://github.com/imliujun/Jolyglot 库实现了Gson、Jackson、Moshi 对这种实体类进行解析都没有问题,但是FastJson解析不了,这样对库的推广和使用范围还是有一些影响,希望能够超过国外的库。

jyztefdp

jyztefdp2#

FastJsonJolyglot 发一下。

xa9qqrwz

xa9qqrwz3#

https://github.com/alibaba/fastjson/releases/tag/1.2.40
升级到最新版本试试看,新版本已经至此非缺省构造函数

yi0zb3m4

yi0zb3m44#

VictorAlbertos/Jolyglot#9
根据我实现的FastJsonJolyglot跑一下这个项目里的单元测试就知道结果了

uubf1zoe

uubf1zoe5#

最新版本的我今天试过了,还是不行

uz75evzq

uz75evzq6#

@kimmking@wenshao
https://github.com/imliujun/Jolyglot 我将完整的FastJsonJolyglot和单元测试上传到这个仓库了
执行FasrJsonSpeakerGenericsTest 可以debug查看解析失败的原因

ghhaqwfi

ghhaqwfi7#

compile 'com.alibaba:fastjson:1.2.41'

相关问题