com.fasterxml.jackson.datatype.jsr310.deser.key.YearMothKeyDeserializer Maven / Gradle / Ivy
package com.fasterxml.jackson.datatype.jsr310.deser.key;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.YEAR;
import java.io.IOException;
import java.time.DateTimeException;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;
import com.fasterxml.jackson.databind.DeserializationContext;
public class YearMothKeyDeserializer extends Jsr310KeyDeserializer {
public static final YearMothKeyDeserializer INSTANCE = new YearMothKeyDeserializer();
// parser copied from YearMonth
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.toFormatter();
private YearMothKeyDeserializer() {
// singleton
}
@Override
protected YearMonth deserialize(String key, DeserializationContext ctxt) throws IOException {
try {
return YearMonth.parse(key, FORMATTER);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, YearMonth.class, e, key);
}
}
}