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

2025-11-11 235 views
2

https://github.com/VictorAlbertos/RxCache/issues/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);
    }
  }
}

回答

1

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

7

FastJsonJolyglot 发一下。

4

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

4

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

4

compile 'com.alibaba:fastjson:1.2.41'