objectmapper-jsonmapper: Jackson 2.23.0 vs 3.3.0 side by side (ObjectMapper/JsonMapper guide rewrite)
Seven runnable programs P01-P07 plus five compile-failure files, JDK 25, no docs/ folder. Output in objectmapper-jsonmapper/output/ is regenerated by scripts/run-all.sh. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b75Ao6imWN5bj7FHVw
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import tools.jackson.databind.SerializationFeature;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/** Jackson 2 habit: build a mapper, then configure it. On Jackson 3 none of these methods exist. */
|
||||
public class Broken1MutateAfterBuild {
|
||||
public static void main(String[] args) {
|
||||
JsonMapper mapper = JsonMapper.builder().build();
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
|
||||
mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/** Jackson 2 allowed readValue(URL, Class). Jackson 3 has no URL overload. */
|
||||
public class Broken2ReadValueUrl {
|
||||
public static void main(String[] args) throws Exception {
|
||||
JsonMapper mapper = JsonMapper.builder().build();
|
||||
Object o = mapper.readValue(new java.net.URL("https://example.com/a.json"), Object.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import java.io.IOException;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/** Jackson 2 habit: wrap the mapper in try/catch (IOException). Jackson 3 throws nothing checked. */
|
||||
public class Broken3CatchIOException {
|
||||
public static void main(String[] args) {
|
||||
JsonMapper mapper = JsonMapper.builder().build();
|
||||
try {
|
||||
mapper.readValue("{}", Object.class);
|
||||
} catch (IOException e) {
|
||||
System.out.println("never reached");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import tools.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/** There is no JavaTimeModule under tools.jackson: java.time support is built into jackson-databind 3. */
|
||||
public class Broken4JavaTimeModule {
|
||||
public static void main(String[] args) {
|
||||
JsonMapper mapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import tools.jackson.databind.SerializationFeature;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/** WRITE_DATES_AS_TIMESTAMPS moved from SerializationFeature to DateTimeFeature in Jackson 3. */
|
||||
public class Broken5WriteDatesAsTimestamps {
|
||||
public static void main(String[] args) {
|
||||
JsonMapper mapper = JsonMapper.builder()
|
||||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user