当属性是空列表时,不要返回属性

时间:2022-07-05 18:05:05

I have a class need to be dessrialized using jackson and the class has an collection property. The collection is empty, but not null. Question: How to desersialise the class without empty collection. Sample code below:

我有一个类需要使用jackson进行dessrialized,这个类有一个collection属性。集合是空的,但不是空的。问题:如何在没有空收藏的情况下抛弃这个类。示例代码如下:

class Person
{
    String name;
    List<Event> events;
    //....getter, setter
}

if

如果

person.list = new List<Event>(); 
persion.name = "hello";

then excepted the json would be:

但json除外:

{name: "hello"}

not

{name: "hello", events:[]}

How to make it? Thank you~~

如何制作吗?谢谢你~ ~

================================================

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

I have solved this by n1ckolas's advice. Thank you first. My jackson version is 2.1.1, and Spring-3.2.2 import better support for this verson of jackson. Also, this works for both arrays and collection. Below is my configuraion:

我已经根据n1ckolas的建议解决了这个问题。先谢谢你。我的jackson版本是2.1.1,Spring-3.2.2引入了对jackson的verson更好的支持。同样,这也适用于数组和集合。下面是我的configuraion:

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="objectMapper"/>
        </bean>
    </mvc:message-converters>        
</mvc:annotation-driven>

<!--Json Mapper-->
<bean name="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" autowire="no">
    <property name="featuresToDisable">
        <list>
            <!--not to return empty colletion-->
            <value type="com.fasterxml.jackson.databind.SerializationFeature">WRITE_EMPTY_JSON_ARRAYS</value>
        </list>
    </property>
</bean>

4 个解决方案

#1


7  

If you can change your original model with Jackson annotations, here is the way how to achieve this.

如果您可以使用Jackson注释更改原始模型,那么以下是实现这一点的方法。

Jackson has WRITE_EMPTY_JSON_ARRAYS. And you may turn off it with:

杰克逊WRITE_EMPTY_JSON_ARRAYS。你可以用:

objectMapper.configure(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS, false);

But it works only for Arrays, but not for Collections. But you may combine @JsonProperty with @JsonIgnore, something like the following:

但它只适用于数组,而不适用于集合。但是您可以将@JsonProperty与@JsonIgnore合并,如下所示:

//....getter, setter of Person class
@JsonIgnore
public List<Event> getEvents() {
    return events;
}

@JsonProperty("events")
public Event[] getEventsArr() {
    return events.toArray(new Event[0]);
}

And afterwards you'll have output, as you expected.

然后你会得到输出,正如你所期望的。

EDIT: If you are using SpringMVC, then you can configure your actual ObjectMapper with explicit reference in mvc:annotation-driven:

编辑:如果您使用的是SpringMVC,那么您可以在mvc:注解驱动的:

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="objectMapper" ref="objectMapper"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<!--custom Json Mapper configuration-->
<bean name="objectMapper" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean" autowire="no">
    <property name="featuresToDisable">
        <list>
            <!--Ignore unknown properties-->
            <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_EMPTY_JSON_ARRAYS</value>
        </list>
    </property>
</bean>

Offtop: usually it's quite useful to specify explicit instance of ObjectMapper, because:

Offtop:通常指定ObjectMapper的显式实例非常有用,因为:

  • you can configure it in the way you want
  • 您可以按照您希望的方式配置它
  • you can use its reference through @Autowired
  • 您可以通过@Autowired使用它的引用

#2


12  

You can use @JsonInclude(JsonInclude.Include.NON_EMPTY) annotation on your class or filed.

您可以在类或文件上使用@JsonInclude(JsonInclude.Include.NON_EMPTY)注释。

import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
class Person
{
    String name;
    List<Event> events;
    //....getter, setter
}

#3


3  

If you are using Spring Boot, just add in your application.properties the following :

如果您正在使用Spring Boot,只需在应用程序中添加。属性如下:

spring.jackson.serialization-inclusion=NON_EMPTY

#4


1  

  1. You can use JSON View, like in here : Jackson - suppressing serialization(write) of properties dynamically

    您可以使用JSON视图,如下所示:Jackson -动态地抑制属性的序列化(写)

  2. Another way to do it is to define another class, almost similar with Person, but without the events property. Then based on the value of the collections, you can decide to serialize the original Person object, or the alternative, events-less object. This is IMHO simpler, and should works for your case above, but not as flexible as the first alternative.

    另一种方法是定义另一个类,几乎与Person相似,但没有events属性。然后,根据集合的值,您可以决定序列化原始的Person对象,或者选择无事件对象。这更简单,应该适用于上面的情况,但不像第一个方案那样灵活。

#1


7  

If you can change your original model with Jackson annotations, here is the way how to achieve this.

如果您可以使用Jackson注释更改原始模型,那么以下是实现这一点的方法。

Jackson has WRITE_EMPTY_JSON_ARRAYS. And you may turn off it with:

杰克逊WRITE_EMPTY_JSON_ARRAYS。你可以用:

objectMapper.configure(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS, false);

But it works only for Arrays, but not for Collections. But you may combine @JsonProperty with @JsonIgnore, something like the following:

但它只适用于数组,而不适用于集合。但是您可以将@JsonProperty与@JsonIgnore合并,如下所示:

//....getter, setter of Person class
@JsonIgnore
public List<Event> getEvents() {
    return events;
}

@JsonProperty("events")
public Event[] getEventsArr() {
    return events.toArray(new Event[0]);
}

And afterwards you'll have output, as you expected.

然后你会得到输出,正如你所期望的。

EDIT: If you are using SpringMVC, then you can configure your actual ObjectMapper with explicit reference in mvc:annotation-driven:

编辑:如果您使用的是SpringMVC,那么您可以在mvc:注解驱动的:

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="objectMapper" ref="objectMapper"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<!--custom Json Mapper configuration-->
<bean name="objectMapper" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean" autowire="no">
    <property name="featuresToDisable">
        <list>
            <!--Ignore unknown properties-->
            <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_EMPTY_JSON_ARRAYS</value>
        </list>
    </property>
</bean>

Offtop: usually it's quite useful to specify explicit instance of ObjectMapper, because:

Offtop:通常指定ObjectMapper的显式实例非常有用,因为:

  • you can configure it in the way you want
  • 您可以按照您希望的方式配置它
  • you can use its reference through @Autowired
  • 您可以通过@Autowired使用它的引用

#2


12  

You can use @JsonInclude(JsonInclude.Include.NON_EMPTY) annotation on your class or filed.

您可以在类或文件上使用@JsonInclude(JsonInclude.Include.NON_EMPTY)注释。

import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
class Person
{
    String name;
    List<Event> events;
    //....getter, setter
}

#3


3  

If you are using Spring Boot, just add in your application.properties the following :

如果您正在使用Spring Boot,只需在应用程序中添加。属性如下:

spring.jackson.serialization-inclusion=NON_EMPTY

#4


1  

  1. You can use JSON View, like in here : Jackson - suppressing serialization(write) of properties dynamically

    您可以使用JSON视图,如下所示:Jackson -动态地抑制属性的序列化(写)

  2. Another way to do it is to define another class, almost similar with Person, but without the events property. Then based on the value of the collections, you can decide to serialize the original Person object, or the alternative, events-less object. This is IMHO simpler, and should works for your case above, but not as flexible as the first alternative.

    另一种方法是定义另一个类,几乎与Person相似,但没有events属性。然后,根据集合的值,您可以决定序列化原始的Person对象,或者选择无事件对象。这更简单,应该适用于上面的情况,但不像第一个方案那样灵活。