我有一个自定义序列化程序,可以为我序列化LocalDateTime。
public class LocalDateTimeJsonSerializer extends JsonSerializer<LocalDateTime> {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
@Override
public void serialize(LocalDateTime date, JsonGenerator generator, SerializerProvider arg2) throws IOException, JsonProcessingException {
final String dateString = date.format(FORMATTER);
generator.writeString(dateString);
}
}
我的问题是我的测试无法正常工作,就像我的jsonGenerator在序列化后仍然为空
@Test
public void serialises_LocalDateTime() throws JsonProcessingException, IOException {
Writer jsonWriter = new StringWriter();
JsonGenerator jsonGenerator = new JsonFactory().createGenerator(jsonWriter);
SerializerProvider serializerProvider = new ObjectMapper().getSerializerProvider();
LocalDateTimeJsonSerializer ldt = new LocalDateTimeJsonSerializer();
ldt.serialize(LocalDateTime.of(2000, Month.JANUARY, 1, 0, 0), jsonGenerator, serializerProvider);
jsonGenerator.flush();
//assertEquals(jsonWriter.toString(), "2000-01-01T00:00:00");
}
..任何想法?