### Import Statement Ordering Example Source: https://github.com/fasterxml/jackson/blob/main/contribution/jackson-coding-style-guide.md Demonstrates the recommended order for import statements: JDK, third-party, Jackson core types, and component-specific types. Static imports follow the same ordering. ```Java import java.io.*; // JDK imports import java.util.*; import org.junit.*; // General 3rd party imports import com.fasterxml.jackson.annotation.*; // Jackson core types: annotations import com.fasterxml.jackson.core.*; // Jackson core types: core import com.fasterxml.jackson.databind.*; // Jackson core types: databind import com.fasterxml.jackson.other.modules.*; // and some Component-specific imports (if any) // same mechanism for static imports ...static import statements... ``` -------------------------------- ### Gradle Dependency Management with Jackson BOM Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Configure your Gradle project to use the Jackson BOM for version management. This example demonstrates how to import the BOM and declare Jackson modules without explicit versions. ```gradle plugins { id 'java' } repositories { mavenCentral() } dependencies { implementation platform("tools.jackson:jackson-bom:3.0.0") // Now declare Jackson modules WITHOUT versions implementation "tools.jackson.core:jackson-databind" } ``` -------------------------------- ### Configure ObjectMapper with Builder and Modules Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Example of constructing a JSON-handling ObjectMapper using the builder pattern, including adding modules and configuring streaming JSON escaping. ```java final JsonMapper mapper = JsonMapper.builder() // format-specific builders .addModule(new JodaModule()) // to use Joda date/time types .enable(JsonWriteFeature.ESCAPE_NON_ASCII) // configure streaming JSON-escaping .build(); ``` -------------------------------- ### Maven Dependency Management with Jackson BOM Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Use the Jackson BOM to manage compatible versions of Jackson modules in your Maven project. This example shows how to import the BOM and declare jackson-databind without specifying a version. ```xml tools.jackson jackson-bom 3.0.0 import pom tools.jackson.core jackson-databind ``` -------------------------------- ### Configure RecyclerPool for TokenStreamFactory Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Explicitly configure the recycler pool on the factory builder, for example, using JsonRecyclerPools.threadLocalPool(), to align with specific performance characteristics. ```java JsonFactory f = JsonFactory.builder() .recyclerPool(JsonRecyclerPools.threadLocalPool()) .build(); JsonMapper mapper = JsonMapper.builder(f).build(); ``` -------------------------------- ### Reconfigure ObjectMapper using Rebuild Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Demonstrates how to create a re-configured instance of an existing ObjectMapper by using its rebuild() method and applying new settings. ```java JsonMapper mapper2 = mapper.rebuild() .enable(SerializationFeature.INDENT_OUTPUT) .build(); ``` -------------------------------- ### Configure Serialization Inclusion with Builder Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Illustrates how to configure serialization inclusion (e.g., NON_NULL) using the builder pattern, specifically for property and content inclusion. ```java ObjectMapper mapper = JsonMapper.builder() .changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(JsonInclude.Include.NON_NULL)) .changeDefaultPropertyInclusion(incl -> incl.withContentInclusion(JsonInclude.Include.NON_NULL)) .build(); ``` -------------------------------- ### Configure Date Format and Time Zone with Builder Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Shows the new builder-based approach for setting the default date format and time zone for an ObjectMapper, replacing older setter methods. ```java ObjectMapper mapper = JsonMapper.builder() .defaultDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")) .defaultTimeZone(TimeZone.getDefault()) .build(); ``` -------------------------------- ### Instantiate YAMLMapper Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Construct format-specific mappers like YAMLMapper by passing a YAMLFactory instance or using the default constructor. ```java new YAMLMapper(new YAMLFactory()); ``` ```java new YAMLMapper(); ``` ```java new YAMLMapper(YAMLFactory().builder() // configure .build()); ``` -------------------------------- ### Configure TokenStreamFactory Features Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Use JsonFactory.builder() to configure features like disabling auto-close for the target stream. ```java JsonFactory f = JsonFactory.builder() .disable(StreamWriteFeature.AUTO_CLOSE_TARGET) .build(); ``` -------------------------------- ### Configure Default Serialization/Deserialization Views Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Use MapperBuilder.defaultSerializationView() and defaultDeserializationView() to set default views for serialization and deserialization. ```java ObjectMapper mapper = JsonMapper.builder() .defaultSerializationView(Views.Public.class) .defaultDeserializationView(Views.Public.class) .build(); ``` -------------------------------- ### Reconfigure TokenStreamFactory Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Use rebuild() on an existing TokenStreamFactory to create a new builder for further configuration, such as enabling strict duplicate detection. ```java JsonFactory f2 = f.rebuild() .enable(StreamWriteFeature.STRICT_DUPLICATE_DETECTION) .build(); ``` -------------------------------- ### Configure Buffer Recycling Pool in Jackson 3 Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md To revert to Jackson 2's thread-local buffer recycling behavior or to use a non-recycling pool, explicitly configure the RecyclerPool when building the JsonFactory. This is useful if profiling indicates overhead from the default deque-based pool. ```java JsonFactory factory = JsonFactory.builder() .recyclerPool(JsonRecyclerPools.threadLocalPool()) .build(); JsonMapper mapper = JsonMapper.builder(factory) .build(); ``` ```java JsonFactory factory = JsonFactory.builder() .recyclerPool(JsonRecyclerPools.nonRecyclingPool()) .build(); ``` -------------------------------- ### Configure ObjectMapper Field Visibility Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Use JsonMapper.builder().changeDefaultVisibility() to configure field visibility, setting it to NONE. ```java ObjectMapper mapper = JsonMapper.builder() .changeDefaultVisibility(vc -> vc.withFieldVisibility(JsonAutoDetect.Visibility.NONE)) .build(); ``` -------------------------------- ### Configure ObjectMapper Default Typing Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Use JsonMapper.builder().activateDefaultTypingAsProperty() to configure default typing. The type validator is built using BasicPolymorphicTypeValidator.builder(). ```java ObjectMapper mapper = JsonMapper.builder() .activateDefaultTypingAsProperty(typeValidator, DefaultTyping.NON_CONCRETE_AND_ARRAYS, "@class") .build(); ``` ```java var typeValidator = BasicPolymorphicTypeValidator.builder() .allowIfSubType("my.package.base.name.") .allowIfSubType("java.util.concurrent.") .allowIfSubTypeIsArray() // ... .build(); ``` -------------------------------- ### ObjectCodec Removal and Replacement Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md The `com.fasterxml.jackson.core.ObjectCodec` interface has been removed from Jackson 3.0. It is replaced by two new interfaces: `tools.jackson.core.ObjectReadContext` and `tools.jackson.core.ObjectWriteContext`. ```java // com.fasterxml.jackson.core.ObjectCodec is removed in Jackson 3.0. // Replaced by tools.jackson.core.ObjectReadContext and tools.jackson.core.ObjectWriteContext. ``` -------------------------------- ### Configure Java 8 Date/Time Handling with DateTimeFeature Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Configure Java 8 Date/Time handling in Jackson 3.x using the new DateTimeFeature enumeration. This replaces module-specific JavaTimeFeature or renamed DeserializationFeature/SerializationFeature. ```java ObjectMapper MAPPER = JsonMapper.builder() .enable(DateTimeFeature.WRITE_DATES_WITH_ZONE_ID) .build(); ``` -------------------------------- ### Pass Customized TokenStreamFactory to ObjectMapper Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md Pass a pre-configured TokenStreamFactory instance to the JsonMapper.builder() constructor when creating an ObjectMapper. ```java JsonFactory f = JsonFactory.builder() // configure .build(); ObjectMapper mapper = JsonMapper.builder(f) // configure .build(); ``` -------------------------------- ### Format Detection Functionality Removal Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md The auto-detection of data format functionality (e.g., `DataFormatDetector` in `jackson-core`) has been dropped in Jackson 3.0 due to complexity and lack of usage. No direct replacement is provided. ```java // Format detection functionality (e.g., DataFormatDetector) is removed in Jackson 3.0. ``` -------------------------------- ### JsonGenerator getCodec() Replacement Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md In Jackson 3.0, `JsonGenerator.getCodec()` is replaced by `objectWriteContext()`. This method provides access to the serialization context. ```java // In Jackson 3.0, JsonGenerator.getCodec() is replaced with objectWriteContext() // Example usage would involve obtaining the context from a JsonGenerator instance. ``` -------------------------------- ### JsonParser getCodec() Replacement Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md The `JsonParser.getCodec()` method has been replaced with `objectReadContext()` in Jackson 3.0. This provides access to the deserialization context. ```java // In Jackson 3.0, JsonParser.getCodec() is replaced with objectReadContext() // Example usage would involve obtaining the context from a JsonParser instance. ``` -------------------------------- ### ObjectMapper copy() Removal Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md The `ObjectMapper.copy()` method has been removed in Jackson 3.0. Creating differently configured instances should now be done using the builder pattern. ```java // In Jackson 3.0, ObjectMapper.copy() is removed. // Use the builder pattern to create new ObjectMapper instances with specific configurations. ``` -------------------------------- ### ObjectMapper canDeserialize/canSerialize Removal Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md The `ObjectMapper.canDeserialize()` and `ObjectMapper.canSerialize()` methods have been removed in Jackson 3.0 as they could not be reliably implemented. ```java // ObjectMapper.canDeserialize() and ObjectMapper.canSerialize() are removed in Jackson 3.0. ``` -------------------------------- ### MappingJsonFactory Removal Source: https://github.com/fasterxml/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md The `com.fasterxml.jackson.databind.MappingJsonFactory` class has been removed in Jackson 3.0. Its functionality is no longer needed or supported in the same way. ```java // com.fasterxml.jackson.databind.MappingJsonFactory is removed in Jackson 3.0. ```