【问题标题】:How to don't serialize the class field in FastJson?如何不序列化 FastJson 中的类字段?
【发布时间】:2021-06-04 09:55:53
【问题描述】:

今天我用的是FastJson(https://github.com/alibaba/fastjson),下面是我的demo代码。

class User {
  private final String name;
  private final int age;
  private final String birthday;
  private String password;

  public User(String name, int age, String birthday) {
    this.name = name;
    this.age = age;
    this.birthday = birthday;
  }

  public void setPassword(String pwd) {
    this.password = pwd;
  }

  public String getName() {
    return name;
  }

  public int getAge() {
    return age;
  }

  public String getBirthday() {
    return birthday;
  }

  public String getPassword() {
    return password;
  }

  @Override
  public String toString() {
    return "User{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", birthday='" + birthday + '\'' +
            ", password='" + password + '\'' +
            '}';
  }
}

它将序列化具有 getXXX 方法的字段。我不想序列化密码,我会调用getPassword()来获取密码值。

我不想重命名方法 getPassword 并将变量密码更新为 public。

有谁知道在序列化此类时如何忽略字段?

【问题讨论】:

    标签: serialization fastjson


    【解决方案1】:
    @JSONField(serialze=false)
    public String getPassword() {
      return password;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 2012-10-10
      • 2012-07-05
      • 2011-12-30
      • 2011-06-19
      相关资源
      最近更新 更多