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,39 @@
|
||||
=== Broken1MutateAfterBuild.java ===
|
||||
Broken1MutateAfterBuild.java:9: error: cannot find symbol
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
^
|
||||
Broken1MutateAfterBuild.java:10: error: cannot find symbol
|
||||
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
|
||||
^
|
||||
Broken1MutateAfterBuild.java:11: error: cannot find symbol
|
||||
mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||
^
|
||||
3 errors
|
||||
|
||||
=== Broken2ReadValueUrl.java ===
|
||||
Broken2ReadValueUrl.java:7: error: no suitable method found for readValue(URL,Class<Object>)
|
||||
Object o = mapper.readValue(new java.net.URL("https://example.com/a.json"), Object.class);
|
||||
^
|
||||
1 error
|
||||
|
||||
=== Broken3CatchIOException.java ===
|
||||
Broken3CatchIOException.java:10: error: exception IOException is never thrown in body of corresponding try statement
|
||||
} catch (IOException e) {
|
||||
^
|
||||
1 error
|
||||
|
||||
=== Broken4JavaTimeModule.java ===
|
||||
Broken4JavaTimeModule.java:1: error: package tools.jackson.datatype.jsr310 does not exist
|
||||
import tools.jackson.datatype.jsr310.JavaTimeModule;
|
||||
^
|
||||
Broken4JavaTimeModule.java:7: error: cannot find symbol
|
||||
JsonMapper mapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
|
||||
^
|
||||
2 errors
|
||||
|
||||
=== Broken5WriteDatesAsTimestamps.java ===
|
||||
Broken5WriteDatesAsTimestamps.java:8: error: cannot find symbol
|
||||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build();
|
||||
^
|
||||
1 error
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
Jackson 2 2.23.0 -> {"articleId":1,"title":"Jackson Deep Dive","tags":["java","json"],"subtitle":null}
|
||||
Jackson 3 3.3.0 -> {"articleId":1,"subtitle":null,"tags":["java","json"],"title":"Jackson Deep Dive"}
|
||||
identical JSON: false
|
||||
|
||||
Jackson 2 read title: Jackson Deep Dive
|
||||
Jackson 3 read title: Jackson Deep Dive
|
||||
|
||||
{
|
||||
"articleId" : 1,
|
||||
"subtitle" : null,
|
||||
"tags" : [ "java", "json" ],
|
||||
"title" : "Jackson Deep Dive"
|
||||
}
|
||||
|
||||
m2 class: com.fasterxml.jackson.databind.ObjectMapper
|
||||
m3 class: tools.jackson.databind.json.JsonMapper
|
||||
m3 superclass: tools.jackson.databind.ObjectMapper
|
||||
m3 is-a tools.jackson.databind.ObjectMapper: true
|
||||
@JsonInclude lives in: com.fasterxml.jackson.annotation
|
||||
@@ -0,0 +1,16 @@
|
||||
== public mutator methods on the mapper class ==
|
||||
Jackson 2 ObjectMapper : 47 distinct names
|
||||
Jackson 3 ObjectMapper : 1 distinct names
|
||||
Jackson 3 JsonMapper : 1 distinct names
|
||||
the one Jackson 3 name that matched: [registeredModules] (a getter, not a setter)
|
||||
|
||||
== Jackson 2: reconfigure after first use ==
|
||||
before : {"articleId":1,"title":"Jackson Deep Dive","tags":["java"],"subtitle":null}
|
||||
after setSerializationInclusion(NON_NULL) : {"articleId":1,"title":"Jackson Deep Dive","tags":["java"],"subtitle":null}
|
||||
fresh mapper, same setting : {"articleId":1,"title":"Jackson Deep Dive","tags":["java"]}
|
||||
|
||||
== Jackson 3: rebuild() derives a new mapper ==
|
||||
original : {"articleId":1,"subtitle":null,"tags":["java"],"title":"Jackson Deep Dive"}
|
||||
rebuilt NON_NULL : {"articleId":1,"tags":["java"],"title":"Jackson Deep Dive"}
|
||||
original again : {"articleId":1,"subtitle":null,"tags":["java"],"title":"Jackson Deep Dive"}
|
||||
same instance? : false
|
||||
@@ -0,0 +1,27 @@
|
||||
writeValueAsString : {"articleId":1,"subtitle":null,"tags":["java","json"],"title":"Jackson Deep Dive"}
|
||||
writeValueAsBytes : 82 bytes
|
||||
writeValue(Path) : {"articleId":1,"subtitle":null,"tags":["java","json"],"title":"Jackson Deep Dive"}
|
||||
writeValue(Stream) : 82 bytes
|
||||
|
||||
read String : Jackson Deep Dive
|
||||
read byte[] : Jackson Deep Dive
|
||||
read File : Jackson Deep Dive
|
||||
read Path : Jackson Deep Dive
|
||||
read InputStream : Jackson Deep Dive
|
||||
read Reader : Jackson Deep Dive
|
||||
|
||||
List<Article> size : 2, element class: Article
|
||||
List.class element : java.util.LinkedHashMap
|
||||
|
||||
tree title : Jackson Deep Dive
|
||||
tree tags[1] : json
|
||||
convertValue to Map : {articleId=1, subtitle=null, tags=[java, json], title=Jackson Deep Dive}
|
||||
convertValue to bean : 9 / From a Map
|
||||
|
||||
readerFor(Article) : Jackson Deep Dive
|
||||
pretty writer : {\n "k" : 1\n}
|
||||
|
||||
Jackson 2 readValue(URL, Class) exists : true
|
||||
Jackson 3 readValue(URL, Class) exists : false
|
||||
Jackson 3 readTree(URL) exists : false
|
||||
non-ASCII byte[] round trip equal : true (bytes are UTF-8 by default: true)
|
||||
@@ -0,0 +1,25 @@
|
||||
== default value of a few features (enabledByDefault) ==
|
||||
feature Jackson2 Jackson3
|
||||
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES true false
|
||||
SerializationFeature.FAIL_ON_EMPTY_BEANS true false
|
||||
SerializationFeature.INDENT_OUTPUT false false
|
||||
WRITE_DATES_AS_TIMESTAMPS (J2: SerializationFeature, J3: DateTimeFeature) true false
|
||||
DeserializationFeature.FAIL_ON_TRAILING_TOKENS false true
|
||||
MapperFeature.SORT_PROPERTIES_ALPHABETICALLY false true
|
||||
|
||||
Jackson 2 default : UnrecognizedPropertyException
|
||||
Jackson 3 default : accepted unknown field, title=T
|
||||
Jackson 3 strict : UnrecognizedPropertyException
|
||||
builderWithJackson2Defaults : UnrecognizedPropertyException (Jackson 2 behaviour back)
|
||||
|
||||
order J2 default : {"articleId":1,"title":"T","tags":["a"],"subtitle":"S"}
|
||||
order J3 default : {"articleId":1,"subtitle":"S","tags":["a"],"title":"T"}
|
||||
order J3 sorting disabled : {"articleId":1,"title":"T","tags":["a"],"subtitle":"S"}
|
||||
order J3 Jackson2Defaults : {"articleId":1,"title":"T","tags":["a"],"subtitle":"S"}
|
||||
|
||||
{
|
||||
"article_id" : 1,
|
||||
"tags" : [ "java", "json" ],
|
||||
"title" : "Jackson Deep Dive"
|
||||
}
|
||||
round trip articleId = 1
|
||||
@@ -0,0 +1,8 @@
|
||||
Jackson 2, no module : InvalidDefinitionException
|
||||
Java 8 date/time type `java.time.LocalDate` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (or disable `MapperFeature.REQUIRE_HANDLERS_FOR_JAVA8_TIMES`) (through reference chain: java.util.LinkedHashMap["published"])
|
||||
Jackson 2 + JavaTimeModule : {"published":[2026,11,17],"at":1794876420.000000000}
|
||||
Jackson 2 + module + disable(TIMESTAMPS): {"published":"2026-11-17","at":"2026-11-17T00:47:00Z"}
|
||||
Jackson 3, plain JsonMapper : {"published":"2026-11-17","at":"2026-11-17T00:47:00Z"}
|
||||
Jackson 3 read LocalDate : 2026-11-17
|
||||
class NOT on classpath : tools.jackson.datatype.jsr310.JavaTimeModule
|
||||
class present on classpath : com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
||||
@@ -0,0 +1,17 @@
|
||||
== class hierarchy ==
|
||||
J2 JsonProcessingException extends com.fasterxml.jackson.core.JacksonException -> extends java.io.IOException
|
||||
J3 JacksonException extends java.lang.RuntimeException
|
||||
|
||||
== malformed JSON ==
|
||||
J2 com.fasterxml.jackson.core.JsonParseException
|
||||
Unexpected character ('}' (code 125)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
|
||||
at line 1, column 27
|
||||
J3 tools.jackson.core.exc.StreamReadException
|
||||
Unexpected character ('}' (code 125)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
|
||||
at line 1, column 27
|
||||
|
||||
== wrong value type ==
|
||||
J2 InvalidFormatException
|
||||
J3 InvalidFormatException (path: [articleId])
|
||||
|
||||
stream + lambda parsed 2 articles with no try/catch
|
||||
@@ -0,0 +1,5 @@
|
||||
8 threads x 20000 round trips on ONE JsonMapper, wrong results: 0
|
||||
|
||||
round 1: shared 3 ms, new mapper per call 878 ms (2000 reads each)
|
||||
round 2: shared 1 ms, new mapper per call 323 ms (2000 reads each)
|
||||
round 3: shared 1 ms, new mapper per call 217 ms (2000 reads each)
|
||||
Reference in New Issue
Block a user