FasterXML / FasterXML/jackson-modules-java8

Add customization option for LocalDateTime handling of seconds and nanos

Đang mở
#117 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
date-time-config new-feature
Ngôn ngữ chính
Java
Star
425
Fork
124
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Hi all,

as seen in https://github.com/FasterXML/jackson-modules-java8/blob/master/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java method _serializeAsArrayContents line 112, if seconds or nanos are set to 0 in the LocalDateTime object being serialized, they will NOT be included into the resulting JSON.

I understand this might be trying to follow what its toString https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#toString does, but it is not convenient when senders and receivers either do not understand the LocalDateTime specification or are unable to handle it properly.

Please add a customization option to allow sending seconds and nanos even when set to 0, the only current workaround is to implement a custom serializer and add it to a custom mapper to be passed in whatever configuration needs it, which is not really convenient:

````
public static class LocalDateTimeWithSecondsSerializer extends StdSerializer {

public LocalDateTimeWithSecondsSerializer() {
super(LocalDateTime.class);
}

@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartArray();
gen.writeNumber(value.getYear());
gen.writeNumber(value.getMonthValue());
gen.writeNumber(value.getDayOfMonth());
gen.writeNumber(value.getHour());
gen.writeNumber(value.getMinute());
gen.writeNumber(value.getSecond());
gen.writeEndArray();
}
}

public static class CustomEntityMapper implements EntityMapper {

private final ObjectMapper objectMapper;

public CustomEntityMapper() {

SimpleModule localDateTimeWithSecondsSerializerModule = new SimpleModule();
localDateTimeWithSecondsSerializerModule.addSerializer(new LocalDateTimeWithSecondsSerializer());

objectMapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
.registerModule(localDateTimeWithSecondsSerializerModule)
.registerModule(new Jdk8Module());

objectMapper.findAndRegisterModules();
}

@Override
public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object);
}

@Override
public T mapToObject(String source, Class clazz) throws IOException {
return objectMapper.readValue(source, clazz);
}
}
````

Thanks and cheers

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.