diff --git a/src/main/java/tools/jackson/databind/DeserializationFeature.java b/src/main/java/tools/jackson/databind/DeserializationFeature.java index ceac1f54d8..c6a7ca206f 100644 --- a/src/main/java/tools/jackson/databind/DeserializationFeature.java +++ b/src/main/java/tools/jackson/databind/DeserializationFeature.java @@ -318,7 +318,7 @@ public enum DeserializationFeature implements ConfigFeature *

* NOTE: only single wrapper Array is allowed: if multiple attempted, exception * will be thrown. - * + *

* Feature is disabled by default. */ UNWRAP_SINGLE_VALUE_ARRAYS(false), @@ -330,7 +330,7 @@ public enum DeserializationFeature implements ConfigFeature * a single property with expected root name. If not, a * {@link DatabindException} is thrown; otherwise value of the wrapped property * will be deserialized as if it was the root value. - *

+ *

* Feature is disabled by default. */ UNWRAP_ROOT_VALUE(false), diff --git a/src/main/java/tools/jackson/databind/cfg/MapperBuilder.java b/src/main/java/tools/jackson/databind/cfg/MapperBuilder.java index 06d812fd04..bdfe968a82 100644 --- a/src/main/java/tools/jackson/databind/cfg/MapperBuilder.java +++ b/src/main/java/tools/jackson/databind/cfg/MapperBuilder.java @@ -798,6 +798,25 @@ public B configure(DatatypeFeature feature, boolean state) { return _this(); } + /** + * The builder returned uses default settings more closely + * matching the default configs used in Jackson 2.x versions. + *

+ * This method is still a work in progress and may not yet fully replicate the + * default settings of Jackson 2.x. + *

+ */ + public B configureForJackson2() { + return enable(SerializationFeature.FAIL_ON_EMPTY_BEANS) + .enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS) + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) + .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) + .disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + .disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); + } + /* /********************************************************************** /* Changing features: parser, generator diff --git a/src/main/java/tools/jackson/databind/json/JsonMapper.java b/src/main/java/tools/jackson/databind/json/JsonMapper.java index bb942a337c..7bdb122bf1 100644 --- a/src/main/java/tools/jackson/databind/json/JsonMapper.java +++ b/src/main/java/tools/jackson/databind/json/JsonMapper.java @@ -142,7 +142,7 @@ public JsonMapper(Builder b) { /********************************************************************** */ - public static JsonMapper.Builder builder() { + public static Builder builder() { return new Builder(new JsonFactory()); } @@ -150,9 +150,23 @@ public static Builder builder(JsonFactory streamFactory) { return new Builder(streamFactory); } + /** + * Modifies the settings of this builder to more closely match the default configs used + * in Jackson 2.x versions. + *

+ * This method is still a work in progress and may not yet fully replicate the + * default settings of Jackson 2.x. + *

+ */ + public static Builder builderWithJackson2Defaults() { + return builder(JsonFactory.builderWithJackson2Defaults().build()) + .configureForJackson2(); + } + + @SuppressWarnings("unchecked") @Override - public JsonMapper.Builder rebuild() { + public Builder rebuild() { return new Builder((Builder.StateImpl)_savedBuilderState); } diff --git a/src/test/java/tools/jackson/databind/json/JsonMapperBuilderTest.java b/src/test/java/tools/jackson/databind/json/JsonMapperBuilderTest.java new file mode 100644 index 0000000000..7d2563966d --- /dev/null +++ b/src/test/java/tools/jackson/databind/json/JsonMapperBuilderTest.java @@ -0,0 +1,37 @@ +package tools.jackson.databind.json; + +import org.junit.jupiter.api.Test; +import tools.jackson.core.StreamReadFeature; +import tools.jackson.core.json.JsonFactory; +import tools.jackson.core.json.JsonWriteFeature; +import tools.jackson.databind.DeserializationFeature; +import tools.jackson.databind.ObjectMapper; +import tools.jackson.databind.SerializationFeature; +import tools.jackson.databind.testutil.DatabindTestUtil; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +// Test(s) to verify behaviors in JsonMapper.Builder +public class JsonMapperBuilderTest extends DatabindTestUtil +{ + + @Test + public void testBuilderWithJackson2Defaults() + { + ObjectMapper mapper = JsonMapper.builderWithJackson2Defaults().build(); + JsonFactory jsonFactory = (JsonFactory) mapper.tokenStreamFactory(); + assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_DOUBLE_PARSER)); + assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_BIG_NUMBER_PARSER)); + assertFalse(jsonFactory.isEnabled(JsonWriteFeature.ESCAPE_FORWARD_SLASHES)); + assertFalse(jsonFactory.isEnabled(JsonWriteFeature.COMBINE_UNICODE_SURROGATES_IN_UTF8)); + assertTrue(mapper.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)); + assertTrue(mapper.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)); + assertTrue(mapper.isEnabled(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)); + assertFalse(mapper.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)); + assertTrue(mapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); + assertFalse(mapper.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)); + assertFalse(mapper.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)); + assertFalse(mapper.isEnabled(DeserializationFeature.READ_ENUMS_USING_TO_STRING)); + } +}