0

我正在尝试将 Java 对象序列化为 JSON。我的一个 Java 对象有一个 JodaTimeLocalTime对象作为其字段之一。

我的许多 Java 对象也有各种Collection可能为空的字段。我想防止像这样的 JSON 序列化:

{id: 2348904, listOfThings: [], listOfStuff: [], nowASet: []}

在这三个Collection为空的场景中,我宁愿看到这个 JSON:

{id: 2348904}

这样做的正确方法是ObjectMapper使用以下代码行进行配置:

objectMapper.setSerializationInclusion(Include.NON_EMPTY);

LocalTime这工作得很好......直到我用它的内部击中那个Java对象。那是我得到一个实际的java.lang.StackOverflowError.

它似乎在JodaDateSerializerBase.isEmpty()和之间来回摆动JsonSerializer.isEmpty()。不过,我不确定如何,因为他们不互相打电话。

我设法制作了一个 SSSSSSCCCCEEEE,或者任何缩写词,如下所示:

package whatever.you.like;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import org.joda.time.LocalTime;
import org.junit.Test;

public class TestClass {
  public class JodaMapper extends ObjectMapper {
    private static final long serialVersionUID = 34785437895L;

    public JodaMapper() {
      registerModule(new JodaModule());
    }

    public boolean getWriteDatesAsTimestamps() {
      return getSerializationConfig().isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    }

    public void setWriteDatesAsTimestamps(boolean state) {
      configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, state);
    }
  }

  private class Thing {
    private LocalTime localTime;

    public Thing() {}

    public void setLocalTime(LocalTime localTime) {
      this.localTime = localTime;
    }

    public LocalTime getLocalTime() {
      return localTime;
    }
  }

  @Test
  public void extendObjectMapperTest() throws JsonProcessingException {
    JodaMapper objectMapper = new JodaMapper();
    objectMapper.setWriteDatesAsTimestamps(false);
    objectMapper.setSerializationInclusion(Include.NON_EMPTY);
    Thing thing = new Thing();
    LocalTime localTime = new LocalTime(12389340L);
    thing.setLocalTime(localTime);
    System.out.println("Never manages to print this out: " + objectMapper.writeValueAsString(thing));
  }

  @Test
  public void configureObjectMapperTest() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JodaModule());
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.setSerializationInclusion(Include.NON_EMPTY);
    Thing thing = new Thing();
    LocalTime localTime = new LocalTime(12389340L);
    thing.setLocalTime(localTime);
    System.out.println("Never manages to print this out: " + objectMapper.writeValueAsString(thing));
  }
}

我尝试扩展ObjectMapper和配置ObjectMapper,每次都得到相同的错误。

依赖项:

有趣的是,您可以在 GitHub 中找到一个声称使用限定符成功的单元测试(“ ”)。我看不出它是如何发挥作用的。testLocalDateSer()Include.NON_EMPTY

4

1 回答 1

0

升级到

  • FasterXML 的杰克逊 2.5.3
  • FasterXML 的 Jackson-DataType-Joda 2.5.3。

这行得通。

 @Test
  public void configureObjectMapperTest() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JodaModule());
//    objectMapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false);
    objectMapper.setSerializationInclusion(Include.NON_EMPTY);
    Thing thing = new Thing();
    LocalTime localTime = new LocalTime(12389340L);
    thing.setLocalTime(localTime);
    System.out.println("Never manages to print this out: " + objectMapper.writeValueAsString(thing));
  }
于 2015-07-08T18:21:10.483 回答