Skip to content

Commit a8bdd33

Browse files
authored
Enable SerializationFeature. WRITE_ENUMS_USING_TO_STRING by default (#4567)
1 parent 1dface0 commit a8bdd33

File tree

11 files changed

+35
-20
lines changed

11 files changed

+35
-20
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Versions: 3.x (for earlier see VERSION-2.x)
6060
#4552: Change `MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS` default to `false` for 3.0
6161
#4566: Enable `DeserializationFeature.READ_ENUMS_USING_TO_STRING` by default (3.0)
6262
(contributed by Joo-Hyuk K)
63+
#4567: Enable `SerializationFeature.WRITE_ENUMS_USING_TO_STRING` by default (3.0)
64+
(contributed by Joo-Hyuk K)
6365
- Remove `MappingJsonFactory`
6466
- Add context parameter for `TypeSerializer` contextualization (`forProperty()`)
6567
- Default for `JsonNodeFeature.STRIP_TRAILING_BIGDECIMAL_ZEROES` changed to `false` for 3.0

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ public enum SerializationFeature implements ConfigFeature
276276
* Note: this feature should usually have same value
277277
* as {@link DeserializationFeature#READ_ENUMS_USING_TO_STRING}.
278278
*<p>
279-
* Feature is disabled by default.
279+
* Feature is enabled by default.
280280
*/
281-
WRITE_ENUMS_USING_TO_STRING(false),
281+
WRITE_ENUMS_USING_TO_STRING(true),
282282

283283
/**
284284
* Feature that determines whether Java Enum values are serialized

src/main/java/tools/jackson/databind/jdk14/JDK14Util.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
package tools.jackson.databind.jdk14;
22

33
import java.lang.reflect.Method;
4-
import java.util.Collections;
54
import java.util.List;
65

7-
import com.fasterxml.jackson.annotation.JsonCreator;
8-
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
9-
10-
import tools.jackson.databind.AnnotationIntrospector;
11-
import tools.jackson.databind.BeanDescription;
12-
import tools.jackson.databind.DeserializationContext;
136
import tools.jackson.databind.PropertyName;
147
import tools.jackson.databind.cfg.MapperConfig;
158
import tools.jackson.databind.introspect.AnnotatedClass;

src/test/java/tools/jackson/databind/deser/creators/EnumCreatorTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,10 @@ public void testNoArgEnumCreator() throws Exception
343343
@Test
344344
public void testEnumCreators1291() throws Exception
345345
{
346-
ObjectMapper mapper = jsonMapperBuilder().disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build();
346+
ObjectMapper mapper = jsonMapperBuilder()
347+
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
348+
.disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
349+
.build();
347350
String json = mapper.writeValueAsString(Enum1291.V2);
348351
Enum1291 result = mapper.readValue(json, Enum1291.class);
349352
assertSame(Enum1291.V2, result);

src/test/java/tools/jackson/databind/deser/enums/EnumMapDeserializationTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ public void testCustomEnumAsRootMapKey() throws Exception
312312
map.put(MyEnum2457.A, "1");
313313
map.put(MyEnum2457.B, "2");
314314
assertEquals(a2q("{'A':'1','B':'2'}"),
315-
MAPPER.writeValueAsString(map));
315+
MAPPER.writer()
316+
.without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
317+
.writeValueAsString(map));
316318

317319
// But should be able to override
318320
assertEquals(a2q("{'"+MyEnum2457.A.toString()+"':'1','"+MyEnum2457.B.toString()+"':'2'}"),
@@ -335,7 +337,9 @@ public void testCustomEnumAsRootMapKeyMixin() throws Exception
335337
map.put(MyEnum2457Base.A, "1");
336338
map.put(MyEnum2457Base.B, "2");
337339
assertEquals(a2q("{'a_mixin':'1','b_mixin':'2'}"),
338-
mixinMapper.writeValueAsString(map));
340+
mixinMapper.writer()
341+
.without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
342+
.writeValueAsString(map));
339343

340344
// But should be able to override
341345
assertEquals(a2q("{'"+MyEnum2457Base.A.toString()+"':'1','"+MyEnum2457Base.B.toString()+"':'2'}"),

src/test/java/tools/jackson/databind/deser/enums/EnumWithNullToString4355Test.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public String toString() {
2727
}
2828
}
2929

30-
private final ObjectMapper MAPPER = newJsonMapper();
30+
private final ObjectMapper MAPPER = jsonMapperBuilder()
31+
.disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING).build();
3132

3233
// [databind#4355]
3334
@Test

src/test/java/tools/jackson/databind/jsontype/deftyping/TestDefaultForObject.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ public Validity validateBaseType(DatabindContext ctxt, JavaType baseType) {
107107
*/
108108

109109
private final ObjectMapper MAPPER = jsonMapperBuilder()
110-
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build();
110+
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
111+
.disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
112+
.build();
111113

112114
/**
113115
* Unit test that verifies that a bean is stored with type information,
@@ -250,6 +252,7 @@ public void testEnumAsObject() throws Exception
250252
// and then with it
251253
ObjectMapper m = jsonMapperBuilder()
252254
.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
255+
.disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
253256
.activateDefaultTyping(NoCheckSubTypeValidator.instance)
254257
.build();
255258
String json = m.writeValueAsString(input);

src/test/java/tools/jackson/databind/ser/EnumAsMapKeyTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ public TypeContainer(Type type, int value) {
9696
/**********************************************************************
9797
*/
9898

99-
private final ObjectMapper MAPPER = newJsonMapper();
99+
private final ObjectMapper MAPPER = jsonMapperBuilder()
100+
.disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING).build();
100101

101102
@Test
102103
public void testMapWithEnumKeys() throws Exception

src/test/java/tools/jackson/databind/ser/enums/EnumSerializationMixinTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public String toString() {
5858
@Test
5959
public void testSerialization() throws Exception {
6060
ObjectMapper mixinMapper = jsonMapperBuilder()
61+
.disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
6162
.addMixIn(EnumBaseA.class, EnumMixinA.class).build();
6263

6364
// equal name(), different toString() value

src/test/java/tools/jackson/databind/ser/jdk/EnumNamingSerializationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class EnumNamingSerializationTest extends DatabindTestUtil {
2222
/**********************************************************
2323
*/
2424

25-
final ObjectMapper MAPPER = newJsonMapper();
25+
final ObjectMapper MAPPER = jsonMapperBuilder().disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build();
2626

2727
@EnumNaming(EnumNamingStrategies.CamelCaseStrategy.class)
2828
static enum EnumFlavorA {
@@ -117,7 +117,9 @@ public void testDesrEnumWithEnumMap() throws Exception {
117117
EnumMap<EnumSauceB, String> enums = new EnumMap<EnumSauceB, String>(EnumSauceB.class);
118118
enums.put(EnumSauceB.MAYO_NEZZ, "value");
119119

120-
String str = MAPPER.writeValueAsString(enums);
120+
String str = MAPPER.writer()
121+
.without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
122+
.writeValueAsString(enums);
121123

122124
assertEquals(a2q("{'mayoNezz':'value'}"), str);
123125
}

0 commit comments

Comments
 (0)