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:
+45
@@ -0,0 +1,45 @@
|
||||
package com.ankurm.objectmapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** The same write + read with Jackson 2's ObjectMapper and Jackson 3's JsonMapper. */
|
||||
public class P01TwoMappersSideBySide {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Article article = new Article(1L, "Jackson Deep Dive", List.of("java", "json"));
|
||||
|
||||
// ---- Jackson 2: package com.fasterxml.jackson.databind ----
|
||||
com.fasterxml.jackson.databind.ObjectMapper m2 = new com.fasterxml.jackson.databind.ObjectMapper();
|
||||
String json2 = m2.writeValueAsString(article);
|
||||
|
||||
// ---- Jackson 3: package tools.jackson.databind.json ----
|
||||
tools.jackson.databind.json.JsonMapper m3 = tools.jackson.databind.json.JsonMapper.builder().build();
|
||||
String json3 = m3.writeValueAsString(article);
|
||||
|
||||
System.out.println("Jackson 2 " + m2.version() + " -> " + json2);
|
||||
System.out.println("Jackson 3 " + m3.version() + " -> " + json3);
|
||||
System.out.println("identical JSON: " + json2.equals(json3));
|
||||
System.out.println();
|
||||
|
||||
// Reading back is the same call in both.
|
||||
Article back2 = m2.readValue(json2, Article.class);
|
||||
Article back3 = m3.readValue(json3, Article.class);
|
||||
System.out.println("Jackson 2 read title: " + back2.getTitle());
|
||||
System.out.println("Jackson 3 read title: " + back3.getTitle());
|
||||
System.out.println();
|
||||
|
||||
// Pretty printing: Jackson 3 takes the feature on the builder, or per call on a writer.
|
||||
String pretty3 = m3.writerWithDefaultPrettyPrinter().writeValueAsString(article);
|
||||
System.out.println(pretty3);
|
||||
System.out.println();
|
||||
|
||||
// Which classes are these, really?
|
||||
System.out.println("m2 class: " + m2.getClass().getName());
|
||||
System.out.println("m3 class: " + m3.getClass().getName());
|
||||
System.out.println("m3 superclass: " + m3.getClass().getSuperclass().getName());
|
||||
System.out.println("m3 is-a tools.jackson.databind.ObjectMapper: "
|
||||
+ (m3 instanceof tools.jackson.databind.ObjectMapper));
|
||||
// The annotation package is shared between the two majors.
|
||||
System.out.println("@JsonInclude lives in: "
|
||||
+ com.fasterxml.jackson.annotation.JsonInclude.class.getPackageName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user