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:
@@ -0,0 +1,50 @@
|
||||
package com.ankurm.javatime;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/** Jackson 2 needs a module (and a flag) for java.time. Jackson 3 needs neither. */
|
||||
public class T01NoModuleNeeded {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Meeting meeting = Samples.meeting();
|
||||
|
||||
ObjectMapper plain2 = new ObjectMapper();
|
||||
System.out.println("== Jackson " + plain2.version() + ", plain ObjectMapper");
|
||||
try {
|
||||
plain2.writeValueAsString(meeting);
|
||||
} catch (Exception e) {
|
||||
System.out.println(Samples.firstLine(e));
|
||||
}
|
||||
|
||||
ObjectMapper module2 = new ObjectMapper().registerModule(new JavaTimeModule());
|
||||
System.out.println();
|
||||
System.out.println("== Jackson 2 + JavaTimeModule");
|
||||
System.out.println(module2.writeValueAsString(meeting));
|
||||
|
||||
JsonMapper m3 = JsonMapper.builder().build();
|
||||
System.out.println();
|
||||
System.out.println("== Jackson " + m3.version() + ", JsonMapper.builder().build()");
|
||||
String json = m3.writeValueAsString(meeting);
|
||||
System.out.println(json);
|
||||
System.out.println("registered modules : " + m3.registeredModules().size());
|
||||
System.out.println("read back : " + m3.readValue(json, Meeting.class));
|
||||
|
||||
System.out.println();
|
||||
for (String name : new String[] {
|
||||
"com.fasterxml.jackson.datatype.jsr310.JavaTimeModule",
|
||||
"tools.jackson.datatype.jsr310.JavaTimeModule",
|
||||
"tools.jackson.databind.ext.javatime.JavaTimeInitializer",
|
||||
"tools.jackson.databind.ext.javatime.ser.InstantSerializer"}) {
|
||||
boolean found;
|
||||
try {
|
||||
Class.forName(name);
|
||||
found = true;
|
||||
} catch (ClassNotFoundException e) {
|
||||
found = false;
|
||||
}
|
||||
System.out.println((found ? "present : " : "absent : ") + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user