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.WhosebugError
.
的时候
好像在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
,但每次都出现相同的错误。
依赖关系:
- JodaTime 2.6
- FasterXML 的杰克逊 2.5.0
- FasterXML 的 Jackson-DataType-Joda 2.5.0
有趣的是,您可以在 GitHub a unit test ("testLocalDateSer()
") 中找到声称使用 Include.NON_EMPTY
限定符成功的内容。我看不出它是如何发挥作用的。
升级到
- 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));
}
我正在尝试将 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.WhosebugError
.
好像在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
,但每次都出现相同的错误。
依赖关系:
- JodaTime 2.6
- FasterXML 的杰克逊 2.5.0
- FasterXML 的 Jackson-DataType-Joda 2.5.0
有趣的是,您可以在 GitHub a unit test ("testLocalDateSer()
") 中找到声称使用 Include.NON_EMPTY
限定符成功的内容。我看不出它是如何发挥作用的。
升级到
- 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));
}