java 如何告诉杰克逊在反序列化期间忽略空对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40366524/
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-11-03 05:09:33  来源:igfitidea点击:

How to tell Hymanson to ignore empty object during deserialization?

javajsonspringHymanson

提问by Mauricio Campagner

At the deserialization process (which as I understand is the process of converting JSON data into a Java Object), how can I tell Hymanson that when it reads a object that contains no data, it should be ignored?

在反序列化过程(据我所知是将 JSON 数据转换为 Java 对象的过程),我如何告诉 Hymanson 当它读取一个不包含数据的对象时,它应该被忽略?

I'm using Hymanson 2.6.6 and Spring 4.2.6

我正在使用 Hymanson 2.6.6 和 Spring 4.2.6

The JSON data received by my controller is as follows:

我的控制器接收到的JSON数据如下:

{
    "id": 2,
    "description": "A description",
    "containedObject": {}
}

The problem is that the object "containedObject" is interpreted as is and it's being instantiated. Therefore, as soon as my controller reads this JSON data, it produces an instance of the ContainedObject object type but I need this to be null instead.

问题是对象“containedObject”被解释为原样并且正在被实例化。因此,只要我的控制器读取此 JSON 数据,它就会生成一个 ContainedObject 对象类型的实例,但我需要将其设为 null。

The easiest and fastest solution would be that in the JSON data received, this value be null like this:

最简单和最快的解决方案是在接收到的 JSON 数据中,该值为 null,如下所示:

 {
        "id": 2,
        "description": "A description",
        "containedObject": null
    }

But this isn't possible since I'm not in control of the JSON data that is sent to me.

但这是不可能的,因为我无法控制发送给我的 JSON 数据。

Is there an annotation (like this explained here) that works for the deserialization process and could be helpfull in my situation?

是否有一个注释(就像这里解释的那样)适用于反序列化过程并且可能对我的情况有所帮助?

I leave a representation of my classes for more information:

我留下了我的课程的代表以获取更多信息:

My entity class is as follows:

我的实体类如下:

public class Entity {
    private long id;
    private String description;
    private ContainedObject containedObject;

//Contructor, getters and setters omitted

}

And my contained object class as follows:

我包含的对象类如下:

public class ContainedObject {
    private long contObjId;
    private String aString;

//Contructor, getters and setters omitted

}

采纳答案by Mechkov

I would use a JsonDeserializer. Inspect the field in question, determine, if it is emtpyand return null, so your ContainedObjectwould be null.

我会使用一个JsonDeserializer. 检查有问题的字段,确定它是否是emtpy并返回null,因此您的ContainedObject值为空。

Something like this (semi-pseudo):

像这样的东西(半伪):

 public class MyDes extends JsonDeserializer<ContainedObject> {

        @Override
        public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
            //read the JsonNode and determine if it is empty JSON object
            //and if so return null

            if (node is empty....) {
                return null;
            }
            return node;
        }

    }

then in your model:

然后在你的模型中:

 public class Entity {
    private long id;
    private String description;

    @JsonDeserialize(using = MyDes.class)
    private ContainedObject containedObject;

   //Contructor, getters and setters omitted

 }

Hope this helps!

希望这可以帮助!

回答by Melik?ah ?im?ek

You can implement a custom deserializer as follows:

您可以按如下方式实现自定义反序列化器:

public class Entity {
    private long id;
    private String description;
    @JsonDeserialize(using = EmptyToNullObject.class)
    private ContainedObject containedObject;

//Contructor, getters and setters omitted

}


public class EmptyToNullObject extends JsonDeserializer<ContainedObject> {

  public ContainedObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonNode node = jp.getCodec().readTree(jp);
    long contObjId = (Long) ((LongNode) node.get("contObjId")).numberValue();
    String aString = node.get("aString").asText();
    if(aString.equals("") && contObjId == 0L) {
      return null;
    } else {
      return new ContainedObject(contObjId, aString);
    }

  }
}

回答by denzal

Approach 1 : This is mostly used. @JsonInclude is used to exclude properties with empty/null/default values.Use @JsonInclude(JsonInclude.Include.NON_NULL) or @JsonInclude(JsonInclude.Include.NON_EMPTY) as per your requirement.

方法1:这是最常用的。@JsonInclude 用于排除具有空/空/默认值的属性。根据您的要求使用 @JsonInclude(JsonInclude.Include.NON_NULL) 或 @JsonInclude(JsonInclude.Include.NON_EMPTY)。

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Employee {

        private String empId;
        private String firstName;

        @JsonInclude(JsonInclude.Include.NON_NULL)
        private String lastName;
        private String address;
        private String emailId;

   }

More info about the Hymanson annotations : https://github.com/FasterXML/Hymanson-annotations/wiki/Hymanson-Annotations

有关Hyman逊注释的更多信息:https: //github.com/FasterXML/Hymanson-annotations/wiki/Hymanson-Annotations

Approach 2 : GSON

方法二:GSON

use GSON (https://code.google.com/p/google-gson/)

使用 GSON ( https://code.google.com/p/google-gson/)