Yatao Wang
Yatao Wang

Reputation: 36

How to don't serialize the class field in FastJson?

Today I'm using FastJson(https://github.com/alibaba/fastjson), the following is my demo code.

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 + '\'' +
            '}';
  }
}

It will serialize the field which has getXXX method. I do not want to serialize the password, and I will call the getPassword() to get the password value.

I do not want to rename the method getPassword and update the variable password to public.

Does anyone know how to ignore a field when serializing this class?

Upvotes: 0

Views: 1028

Answers (1)

Yatao Wang
Yatao Wang

Reputation: 36

@JSONField(serialze=false)
public String getPassword() {
  return password;
}

Upvotes: 2

Related Questions