java Jackson JSON 生成器为丢失的对象创建空 JSON 值

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

Hymanson JSON generator creates null JSON values for missing objects

javajsonHymansongson

提问by geekyaleks

I've started using Hymanson as a JSON generator, as an alternative to google GSON. I've run into an issue where Hymanson is generating object: null if the object is indeed null. GSON on the other hand generates NO entry in JSON, which is the behavior I want. Is there a way to stop Hymanson from generating null object/value when an object is missing?

我已经开始使用 Hymanson 作为 JSON 生成器,作为 google GSON 的替代品。我遇到了 Hymanson 生成对象的问题:如果对象确实为 null,则为 null。另一方面,GSON 不会在 JSON 中生成任何条目,这是我想要的行为。当对象丢失时,有没有办法阻止Hyman逊生成空对象/值?

Hymanson

Hyman逊

        ObjectMapper mapper = new ObjectMapper();
    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, some_complex_object);
    String Hymanson = sw.getBuffer().toString();

    System.out.println("********************* START HymanSON JSON ****************************");
    System.out.println(Hymanson);
    System.out.println("********************* END HymanSON JSON ****************************");

generates this:

生成这个:

{"eatwithrustyspoon":{"urlList":null,"device":"iPad","os":"iPhone OS","peer_id":

{"eatwithrustyspoon":{ "urlList":null,"device":"iPad","os":"iPhone OS","peer_id":

and GSON looks like this:

和 GSON 看起来像这样:

        Gson gson = new Gson();
    String json = gson.toJson(some_complex_object);

    System.out.println("********************* START GSON JSON ****************************");
    System.out.println(json);
    System.out.println("********************* END GSON JSON ****************************");

and it generates this (which is what I want - note that "urlList":null was not generated) :

它生成了这个(这是我想要的 - 请注意没有生成“urlList”:null):

{"eatwithrustyspoon":{"device":"iPad","os":"iPhone OS","peer_id"

{"eatwithrustyspoon":{"device":"iPad","os":"iPhone OS","peer_id"

回答by Brian Roach

From the Hymanson FAQ:

Hyman逊常见问题解答

Can I omit writing of Bean properties with null value? ("how to prevent writing of null properties", "how to suppress null values")

我可以省略写具有空值的 Bean 属性吗?(“如何防止写入空属性”、“如何抑制空值”)

Yes. As per HymansonAnnotationSerializeNulls, you can use:

是的。根据HymansonAnnotationSerializeNulls,您可以使用:

  objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
  // or (for older versions):
  objectMapper.configure(SerializationConfig.WRITE_NULL_PROPERTIES, false);

and voila, no more null values. Note that you MUST configure mapper before beans are serialized, since this setting may be cached along with serializers. So setting it too late might prevent change from taking effect.

瞧,没有空值了。请注意,您必须在 bean 序列化之前配置映射器,因为此设置可能与序列化器一起缓存。因此,设置得太晚可能会阻止更改生效。

回答by Prateek S

my issue was bit different actually i was getting Null values for the properties of POJO class.

我的问题有点不同,实际上我正在为 POJO 类的属性获取 Null 值。

however i solved the problem by giving mapping to properties in my pojo class like this :

但是我通过在我的 pojo 类中映射属性解决了这个问题,如下所示:

@JsonProperty("PROPERTY_NAME")

@JsonProperty("PROPERTY_NAME")

thought it may help someone :)

认为它可以帮助某人:)

回答by Ghost Rider

The following solution saved me.

以下解决方案救了我。

  objectMapper.setSerializationInclusion(Include.NON_NULL);