使用jackson转换Java对象时如何忽略可选属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24622635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:31:12  来源:igfitidea点击:

How an optional property can be ignored when using Hymanson to convert Java object

javajsonHymanson

提问by taoxiaopang

I'm using Hymanson 1.9.2(org.codehaus.Hymanson) to convent from Java object to matching JSON construct. Here is my java object:

我正在使用 Hymanson 1.9.2(org.codehaus.Hymanson) 将 Java 对象转换为匹配的 JSON 构造。这是我的java对象:

Class ColorLight {
    String type;
    boolean isOn;
    String value;

    public String getType(){
        return type;
    }

    public setType(String type) {
        this.type = type;
    }

    public boolean getIsOn(){
        return isOn;
    }

    public setIsOn(boolean isOn) {
        this.isOn = isOn;
    }

    public String getValue(){
        return value;
    }

    public setValue(String value) {
        this.value = value;
    }
}

If I did the following conversion, I'd get the result I want.

如果我做了以下转换,我会得到我想要的结果。

ColorLight light = new ColorLight();
light.setType("red");
light.setIsOn("true");
light.setValue("255");
objectMapper mapper = new ObjectMapper();
jsonString = mapper.writeValueAsString();

jsonString would be like:

jsonString 会是这样的:

{"type":"red","isOn":"true", "value":"255"}

But sometimes I don't have the value of isOn property

但有时我没有 isOn 属性的值

ColorLight light = new ColorLight();
light.setType("red");
light.setValue("255");

But the jsonString is still like:

但是 jsonString 仍然是这样的:

{"type":"red","isOn":"false", "value":"255"}

Where "isOn:false" is default value of Java boolean type which I don't want it be there. How can I remove the isOn property in the final json construct like this?

其中“isOn:false”是 Java 布尔类型的默认值,我不希望它在那里。如何在最终的 json 构造中删除 isOn 属性?

{"type":"red","value":"255"}

采纳答案by chrylis -cautiouslyoptimistic-

To skip the value if it's not present:

如果值不存在则跳过该值:

  • Use Booleaninstead of the booleanprimitive (booleanvalues are always set to trueor false).
  • Configure Hymanson not to serialize nulls by using @JsonInclude(Include.NON_NULL)or @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL), depending on the version.
  • 使用Boolean而不是boolean原始boolean值(值始终设置为truefalse)。
  • 将 Hymanson 配置为不使用@JsonInclude(Include.NON_NULL)或序列化空值@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL),具体取决于版本。

回答by Alexey Gavrilov

You can mark your class with the @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)in 1.x annotation that indicates that only properties that have values that differ from default settings (meaning values they have when Bean is constructed with its no-arguments constructor) are to be included.

您可以使用@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)in 1.x 批注标记您的类,该批注指示仅包含具有与默认设置不同的值的属性(这意味着它们在使用无参数构造函数构造 Bean 时具有的值)将被包含在内。

The @JsonInclude(JsonInclude.Include.NON_DEFAULT)annotation is used for version 2.x.

@JsonInclude(JsonInclude.Include.NON_DEFAULT)注释用于 2.x 版。

Here is an example:

下面是一个例子:

public class HymansonInclusion {

    @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
    public static class ColorLight {
        public String type;
        public boolean isOn;

        public ColorLight() {
        }

        public ColorLight(String type, boolean isOn) {
            this.type = type;
            this.isOn = isOn;
        }
    }

    public static void main(String[] args) throws IOException {
        ColorLight light = new ColorLight("value", false);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(light));
    }
}

Output:

输出:

{"type":"value"}