diff --git a/release-notes/VERSION b/release-notes/VERSION index 4e08b98fa6..4f3d3bbfa7 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -60,6 +60,8 @@ Versions: 3.x (for earlier see VERSION-2.x) #4552: Change `MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS` default to `false` for 3.0 #4566: Enable `DeserializationFeature.READ_ENUMS_USING_TO_STRING` by default (3.0) (contributed by Joo-Hyuk K) +#4567: Enable `SerializationFeature.WRITE_ENUMS_USING_TO_STRING` by default (3.0) + (contributed by Joo-Hyuk K) - Remove `MappingJsonFactory` - Add context parameter for `TypeSerializer` contextualization (`forProperty()`) - Default for `JsonNodeFeature.STRIP_TRAILING_BIGDECIMAL_ZEROES` changed to `false` for 3.0 diff --git a/src/main/java/tools/jackson/databind/SerializationFeature.java b/src/main/java/tools/jackson/databind/SerializationFeature.java index bea75e4a4c..d713cb12cb 100644 --- a/src/main/java/tools/jackson/databind/SerializationFeature.java +++ b/src/main/java/tools/jackson/databind/SerializationFeature.java @@ -276,9 +276,9 @@ public enum SerializationFeature implements ConfigFeature * Note: this feature should usually have same value * as {@link DeserializationFeature#READ_ENUMS_USING_TO_STRING}. *

- * Feature is disabled by default. + * Feature is enabled by default. */ - WRITE_ENUMS_USING_TO_STRING(false), + WRITE_ENUMS_USING_TO_STRING(true), /** * Feature that determines whether Java Enum values are serialized diff --git a/src/main/java/tools/jackson/databind/jdk14/JDK14Util.java b/src/main/java/tools/jackson/databind/jdk14/JDK14Util.java index 1931796773..bbfff902dd 100644 --- a/src/main/java/tools/jackson/databind/jdk14/JDK14Util.java +++ b/src/main/java/tools/jackson/databind/jdk14/JDK14Util.java @@ -1,15 +1,8 @@ package tools.jackson.databind.jdk14; import java.lang.reflect.Method; -import java.util.Collections; import java.util.List; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonCreator.Mode; - -import tools.jackson.databind.AnnotationIntrospector; -import tools.jackson.databind.BeanDescription; -import tools.jackson.databind.DeserializationContext; import tools.jackson.databind.PropertyName; import tools.jackson.databind.cfg.MapperConfig; import tools.jackson.databind.introspect.AnnotatedClass; diff --git a/src/test/java/tools/jackson/databind/deser/creators/EnumCreatorTest.java b/src/test/java/tools/jackson/databind/deser/creators/EnumCreatorTest.java index 6988e49d87..2fd56e479a 100644 --- a/src/test/java/tools/jackson/databind/deser/creators/EnumCreatorTest.java +++ b/src/test/java/tools/jackson/databind/deser/creators/EnumCreatorTest.java @@ -343,7 +343,10 @@ public void testNoArgEnumCreator() throws Exception @Test public void testEnumCreators1291() throws Exception { - ObjectMapper mapper = jsonMapperBuilder().disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build(); + ObjectMapper mapper = jsonMapperBuilder() + .disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING) + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) + .build(); String json = mapper.writeValueAsString(Enum1291.V2); Enum1291 result = mapper.readValue(json, Enum1291.class); assertSame(Enum1291.V2, result); diff --git a/src/test/java/tools/jackson/databind/deser/enums/EnumMapDeserializationTest.java b/src/test/java/tools/jackson/databind/deser/enums/EnumMapDeserializationTest.java index bbd620fdfd..33900c23de 100644 --- a/src/test/java/tools/jackson/databind/deser/enums/EnumMapDeserializationTest.java +++ b/src/test/java/tools/jackson/databind/deser/enums/EnumMapDeserializationTest.java @@ -312,7 +312,9 @@ public void testCustomEnumAsRootMapKey() throws Exception map.put(MyEnum2457.A, "1"); map.put(MyEnum2457.B, "2"); assertEquals(a2q("{'A':'1','B':'2'}"), - MAPPER.writeValueAsString(map)); + MAPPER.writer() + .without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) + .writeValueAsString(map)); // But should be able to override assertEquals(a2q("{'"+MyEnum2457.A.toString()+"':'1','"+MyEnum2457.B.toString()+"':'2'}"), @@ -335,7 +337,9 @@ public void testCustomEnumAsRootMapKeyMixin() throws Exception map.put(MyEnum2457Base.A, "1"); map.put(MyEnum2457Base.B, "2"); assertEquals(a2q("{'a_mixin':'1','b_mixin':'2'}"), - mixinMapper.writeValueAsString(map)); + mixinMapper.writer() + .without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) + .writeValueAsString(map)); // But should be able to override assertEquals(a2q("{'"+MyEnum2457Base.A.toString()+"':'1','"+MyEnum2457Base.B.toString()+"':'2'}"), diff --git a/src/test/java/tools/jackson/databind/deser/enums/EnumWithNullToString4355Test.java b/src/test/java/tools/jackson/databind/deser/enums/EnumWithNullToString4355Test.java index f6b0916b8f..42d458df65 100644 --- a/src/test/java/tools/jackson/databind/deser/enums/EnumWithNullToString4355Test.java +++ b/src/test/java/tools/jackson/databind/deser/enums/EnumWithNullToString4355Test.java @@ -27,7 +27,8 @@ public String toString() { } } - private final ObjectMapper MAPPER = newJsonMapper(); + private final ObjectMapper MAPPER = jsonMapperBuilder() + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING).build(); // [databind#4355] @Test diff --git a/src/test/java/tools/jackson/databind/jsontype/deftyping/TestDefaultForObject.java b/src/test/java/tools/jackson/databind/jsontype/deftyping/TestDefaultForObject.java index 4cc016227d..9471aba4af 100644 --- a/src/test/java/tools/jackson/databind/jsontype/deftyping/TestDefaultForObject.java +++ b/src/test/java/tools/jackson/databind/jsontype/deftyping/TestDefaultForObject.java @@ -107,7 +107,9 @@ public Validity validateBaseType(DatabindContext ctxt, JavaType baseType) { */ private final ObjectMapper MAPPER = jsonMapperBuilder() - .disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).build(); + .disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING) + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) + .build(); /** * Unit test that verifies that a bean is stored with type information, @@ -250,6 +252,7 @@ public void testEnumAsObject() throws Exception // and then with it ObjectMapper m = jsonMapperBuilder() .disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING) + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) .activateDefaultTyping(NoCheckSubTypeValidator.instance) .build(); String json = m.writeValueAsString(input); diff --git a/src/test/java/tools/jackson/databind/ser/EnumAsMapKeyTest.java b/src/test/java/tools/jackson/databind/ser/EnumAsMapKeyTest.java index 154cdda891..f66daaff35 100644 --- a/src/test/java/tools/jackson/databind/ser/EnumAsMapKeyTest.java +++ b/src/test/java/tools/jackson/databind/ser/EnumAsMapKeyTest.java @@ -96,7 +96,8 @@ public TypeContainer(Type type, int value) { /********************************************************************** */ - private final ObjectMapper MAPPER = newJsonMapper(); + private final ObjectMapper MAPPER = jsonMapperBuilder() + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING).build(); @Test public void testMapWithEnumKeys() throws Exception diff --git a/src/test/java/tools/jackson/databind/ser/enums/EnumSerializationMixinTest.java b/src/test/java/tools/jackson/databind/ser/enums/EnumSerializationMixinTest.java index a27e3c1e2b..3dffcf506d 100644 --- a/src/test/java/tools/jackson/databind/ser/enums/EnumSerializationMixinTest.java +++ b/src/test/java/tools/jackson/databind/ser/enums/EnumSerializationMixinTest.java @@ -58,6 +58,7 @@ public String toString() { @Test public void testSerialization() throws Exception { ObjectMapper mixinMapper = jsonMapperBuilder() + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) .addMixIn(EnumBaseA.class, EnumMixinA.class).build(); // equal name(), different toString() value diff --git a/src/test/java/tools/jackson/databind/ser/jdk/EnumNamingSerializationTest.java b/src/test/java/tools/jackson/databind/ser/jdk/EnumNamingSerializationTest.java index 283da2d198..60c95ee6ce 100644 --- a/src/test/java/tools/jackson/databind/ser/jdk/EnumNamingSerializationTest.java +++ b/src/test/java/tools/jackson/databind/ser/jdk/EnumNamingSerializationTest.java @@ -22,7 +22,7 @@ public class EnumNamingSerializationTest extends DatabindTestUtil { /********************************************************** */ - final ObjectMapper MAPPER = newJsonMapper(); + final ObjectMapper MAPPER = jsonMapperBuilder().disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build(); @EnumNaming(EnumNamingStrategies.CamelCaseStrategy.class) static enum EnumFlavorA { @@ -117,7 +117,9 @@ public void testDesrEnumWithEnumMap() throws Exception { EnumMap enums = new EnumMap(EnumSauceB.class); enums.put(EnumSauceB.MAYO_NEZZ, "value"); - String str = MAPPER.writeValueAsString(enums); + String str = MAPPER.writer() + .without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) + .writeValueAsString(enums); assertEquals(a2q("{'mayoNezz':'value'}"), str); } diff --git a/src/test/java/tools/jackson/databind/ser/jdk/EnumSerializationTest.java b/src/test/java/tools/jackson/databind/ser/jdk/EnumSerializationTest.java index cbce9b29f5..f3b00e094d 100644 --- a/src/test/java/tools/jackson/databind/ser/jdk/EnumSerializationTest.java +++ b/src/test/java/tools/jackson/databind/ser/jdk/EnumSerializationTest.java @@ -161,7 +161,8 @@ private EnumWithJsonKey(String n) { /********************************************************************** */ - private final ObjectMapper MAPPER = newJsonMapper(); + private final ObjectMapper MAPPER = jsonMapperBuilder() + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING).build(); @Test public void testSimple() throws Exception @@ -257,7 +258,9 @@ public void testToStringEnumWithEnumMap() throws Exception public void testAsIndex() throws Exception { // By default, serialize using name - ObjectMapper m = newJsonMapper(); + ObjectMapper m = jsonMapperBuilder() + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) + .build(); assertFalse(m.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX)); assertEquals(q("B"), m.writeValueAsString(TestEnum.B)); @@ -293,7 +296,9 @@ public void testGenericEnumSerializer() throws Exception @Test public void testEnumMapSerDefault() throws Exception { - final ObjectMapper mapper = newJsonMapper(); + final ObjectMapper mapper = jsonMapperBuilder() + .disable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) + .build(); EnumMap m = new EnumMap(LC749Enum.class); m.put(LC749Enum.A, "value"); assertEquals("{\"A\":\"value\"}", mapper.writeValueAsString(m));