簡體   English   中英

JodaTime LocalTime到JSON-實際堆棧溢出

[英]JodaTime LocalTime to JSON - Actual Stack Overflow

我正在嘗試將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()之間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限定符成功。 我看不到它可能如何運作。

升級到

  • FasterXML的Jackson 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));
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM