Skip to content

Commit b1b7a17

Browse files
authored
add JsonMapper builderWithJackson2Defaults() (#5004)
1 parent a8e9291 commit b1b7a17

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

src/main/java/tools/jackson/databind/DeserializationFeature.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public enum DeserializationFeature implements ConfigFeature
318318
* <p>
319319
* NOTE: only <b>single</b> wrapper Array is allowed: if multiple attempted, exception
320320
* will be thrown.
321-
*
321+
* <p>
322322
* Feature is disabled by default.
323323
*/
324324
UNWRAP_SINGLE_VALUE_ARRAYS(false),
@@ -330,7 +330,7 @@ public enum DeserializationFeature implements ConfigFeature
330330
* a single property with expected root name. If not, a
331331
* {@link DatabindException} is thrown; otherwise value of the wrapped property
332332
* will be deserialized as if it was the root value.
333-
*<p>
333+
* <p>
334334
* Feature is disabled by default.
335335
*/
336336
UNWRAP_ROOT_VALUE(false),

src/main/java/tools/jackson/databind/cfg/MapperBuilder.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,25 @@ public B configure(DatatypeFeature feature, boolean state) {
798798
return _this();
799799
}
800800

801+
/**
802+
* The builder returned uses default settings more closely
803+
* matching the default configs used in Jackson 2.x versions.
804+
* <p>
805+
* This method is still a work in progress and may not yet fully replicate the
806+
* default settings of Jackson 2.x.
807+
* </p>
808+
*/
809+
public B configureForJackson2() {
810+
return enable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
811+
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
812+
.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
813+
.disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
814+
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
815+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
816+
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
817+
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
818+
}
819+
801820
/*
802821
/**********************************************************************
803822
/* Changing features: parser, generator

src/main/java/tools/jackson/databind/json/JsonMapper.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,31 @@ public JsonMapper(Builder b) {
142142
/**********************************************************************
143143
*/
144144

145-
public static JsonMapper.Builder builder() {
145+
public static Builder builder() {
146146
return new Builder(new JsonFactory());
147147
}
148148

149149
public static Builder builder(JsonFactory streamFactory) {
150150
return new Builder(streamFactory);
151151
}
152152

153+
/**
154+
* Modifies the settings of this builder to more closely match the default configs used
155+
* in Jackson 2.x versions.
156+
* <p>
157+
* This method is still a work in progress and may not yet fully replicate the
158+
* default settings of Jackson 2.x.
159+
* </p>
160+
*/
161+
public static Builder builderWithJackson2Defaults() {
162+
return builder(JsonFactory.builderWithJackson2Defaults().build())
163+
.configureForJackson2();
164+
}
165+
166+
153167
@SuppressWarnings("unchecked")
154168
@Override
155-
public JsonMapper.Builder rebuild() {
169+
public Builder rebuild() {
156170
return new Builder((Builder.StateImpl)_savedBuilderState);
157171
}
158172

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tools.jackson.databind.json;
2+
3+
import org.junit.jupiter.api.Test;
4+
import tools.jackson.core.StreamReadFeature;
5+
import tools.jackson.core.json.JsonFactory;
6+
import tools.jackson.core.json.JsonWriteFeature;
7+
import tools.jackson.databind.DeserializationFeature;
8+
import tools.jackson.databind.ObjectMapper;
9+
import tools.jackson.databind.SerializationFeature;
10+
import tools.jackson.databind.testutil.DatabindTestUtil;
11+
12+
import static org.junit.jupiter.api.Assertions.assertFalse;
13+
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
15+
// Test(s) to verify behaviors in JsonMapper.Builder
16+
public class JsonMapperBuilderTest extends DatabindTestUtil
17+
{
18+
19+
@Test
20+
public void testBuilderWithJackson2Defaults()
21+
{
22+
ObjectMapper mapper = JsonMapper.builderWithJackson2Defaults().build();
23+
JsonFactory jsonFactory = (JsonFactory) mapper.tokenStreamFactory();
24+
assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_DOUBLE_PARSER));
25+
assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_BIG_NUMBER_PARSER));
26+
assertFalse(jsonFactory.isEnabled(JsonWriteFeature.ESCAPE_FORWARD_SLASHES));
27+
assertFalse(jsonFactory.isEnabled(JsonWriteFeature.COMBINE_UNICODE_SURROGATES_IN_UTF8));
28+
assertTrue(mapper.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
29+
assertTrue(mapper.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS));
30+
assertTrue(mapper.isEnabled(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS));
31+
assertFalse(mapper.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING));
32+
assertTrue(mapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
33+
assertFalse(mapper.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES));
34+
assertFalse(mapper.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS));
35+
assertFalse(mapper.isEnabled(DeserializationFeature.READ_ENUMS_USING_TO_STRING));
36+
}
37+
}

0 commit comments

Comments
 (0)