[英]JodaTime LocalDate/LocalTime not Parsing with custom JSON Serializer classes
[英]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
限定符成功。 我看不到它可能如何運作。
升級到
這可行。
@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.