有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JodaTime LocalTime到JSON实际堆栈溢出

我正在尝试将Java对象序列化为JSON。我的一个Java对象的一个字段是JodaTime LocalTime对象

我的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()之间的乒乓球。但我不知道怎么做,因为他们不打电话

我成功地制作了一个SSSSCCcceee,或者不管它的首字母缩写是什么,如下所示:

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,每次都得到相同的错误

依赖项:

有趣的是,您可以在声称使用Include.NON_EMPTY限定符成功的GitHub a unit test(“testLocalDateSer()”)中找到它。我看不出它怎么可能起作用


共 (1) 个答案

  1. # 1 楼答案

    升级到

    • FasterXML的Jackson 2.5.3
    • FasterXML的Jackson数据类型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));
      }