将枚举添加到Jackson注释的类中可以吗?

时间:2018-09-24 05:14:55

标签: java enums jackson

我在类中添加了一个枚举,并且该类使用Jackson注释进行了注释。我不希望在序列化或反序列化过程中考虑此枚举。我是否需要添加任何特殊标签来忽略枚举,就像我们对方法或变量进行@JsonIgnore一样?

IGetDetails

这是一些使用此类的代码。很好。

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "name", "license", "description" })
public class Car {

    @JsonProperty("name")
    private String name;
    @JsonProperty("license")
    private String license;
    @JsonProperty("description")
    private String description;

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("license")
    public String getLicense() {
        return license;
    }

    @JsonProperty("license")
    public void setLicense(String license) {
        this.license = license;
    }

    @JsonProperty("description")
    public String getDescription() {
        return description;
    }

    @JsonProperty("description")
    public void setDescription(String description) {
        this.description = description;
    }

    @JsonIgnore
    public Type getType() {
        if(this.description.contains("electric")) {
            return Type.ELECTRIC;
        }else if(this.description.contains("diesel")) {
            return Type.DIESEL;
        }else {
            return Type.UNKNOWN;
        }
    }

    public enum Type {
        ELECTRIC, DIESEL, GASOLINE, HYDROGEN, BIOFUEL, UNKNOWN
    }
}

0 个答案:

没有答案