0

我的类包含一个属性“状态”,它的值从 0 到 5。当我序列化对象以将其发布到 webapi 时,我还需要发送一个布尔标志。标志的值可以通过状态值计算(见下面的代码)。

如何序列化 computeFlag() 方法的输出?在Java中可能吗?该标志仅在与 webapi 通信时使用,在其他任何地方都不使用。

@Expose
@SerializedName("State") 
protected int state;

// I want to avoid setting up and storing this flag
//
// @Expose
// @SerializedName("WebApiFlag")
// private boolean flag = false;

// I wanna do this instead, but it doesn't compile
@Expose
@SerializedName("WebApiFlag")
private boolean computeFlag(){
    if (this.state == 0)
        return true;
    else
       return false;
}
4

0 回答 0