java-time: java.time on Jackson 3 vs 2, @JsonFormat, time zones, Spring Boot 4.2 spring.jackson.*

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b4HkWoC7hLzk61vZa8
This commit is contained in:
2026-12-03 23:41:03 +05:30
co-authored by Claude Sonnet 5.5
parent f5ed5d1605
commit 26f64bc184
42 changed files with 1505 additions and 0 deletions
@@ -0,0 +1,66 @@
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) { }
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());
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());
}
}
}