Files

71 lines
3.8 KiB
Java

package com.ankurm.javatime;
import java.time.Month;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.cfg.DateTimeFeature;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.module.SimpleModule;
import tools.jackson.databind.ser.std.StdSerializer;
/** java.time.Month changed from a name to a number, and the number can be off by one. */
public class T11MonthTrap {
record Report(Month month) { }
record Annotated(@JsonFormat(shape = JsonFormat.Shape.STRING) Month month) { }
public static void main(String[] args) throws Exception {
ObjectMapper j2 = new ObjectMapper().registerModule(new JavaTimeModule());
JsonMapper j3 = JsonMapper.builder().build();
JsonMapper zeroBased = JsonMapper.builder().disable(DateTimeFeature.ONE_BASED_MONTHS).build();
JsonMapper names = JsonMapper.builder()
.withConfigOverride(Month.class,
o -> o.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
.build();
Report november = new Report(Month.NOVEMBER);
System.out.println("value sent : " + november.month());
System.out.println("Jackson 2 writes : " + j2.writeValueAsString(november));
String fromJ3 = j3.writeValueAsString(november);
System.out.println("Jackson 3 defaults write : " + fromJ3);
System.out.println(" Jackson 3 reads it : " + j3.readValue(fromJ3, Report.class).month());
System.out.println(" Jackson 2 reads it : " + j2.readValue(fromJ3, Report.class).month());
String compat = zeroBased.writeValueAsString(november);
ObjectMapper j2oneBased = com.fasterxml.jackson.databind.json.JsonMapper.builder()
.addModule(new JavaTimeModule()
.enable(com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature.ONE_BASED_MONTHS))
.build();
System.out.println(" Jackson 2 + ONE_BASED_MONTHS reads : " + j2oneBased.readValue(fromJ3, Report.class).month());
System.out.println("Jackson 3, ONE_BASED_MONTHS off : " + compat);
System.out.println(" Jackson 3 defaults read it : " + j3.readValue(compat, Report.class).month());
System.out.println(" Jackson 2 reads it : " + j2.readValue(compat, Report.class).month());
String named = names.writeValueAsString(november);
System.out.println("Jackson 3, Month override STRING : " + named);
System.out.println(" Jackson 3 reads it : " + names.readValue(named, Report.class).month());
System.out.println(" Jackson 2 reads it : " + j2.readValue(named, Report.class).month());
System.out.println("Jackson 3, @JsonFormat(shape=STRING) : "
+ j3.writeValueAsString(new Annotated(Month.NOVEMBER)));
SimpleModule module = new SimpleModule().addSerializer(Month.class, new MonthNameSerializer());
System.out.println("Jackson 3, MonthNameSerializer : "
+ JsonMapper.builder().addModule(module).build().writeValueAsString(november));
}
/** Works on every 3.x line tried here, including the one Spring Boot 4.2.0 manages. */
static final class MonthNameSerializer extends StdSerializer<Month> {
MonthNameSerializer() {
super(Month.class);
}
@Override
public void serialize(Month value, JsonGenerator gen, SerializationContext context) {
gen.writeString(value.name());
}
}
}