Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable SerializationFeature.WRITE_ENUMS_USING_TO_STRING by default (3.0) #4567

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*<p>
* 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
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/tools/jackson/databind/jdk14/JDK14Util.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'}"),
Expand All @@ -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'}"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -117,7 +117,9 @@ public void testDesrEnumWithEnumMap() throws Exception {
EnumMap<EnumSauceB, String> enums = new EnumMap<EnumSauceB, String>(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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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<LC749Enum, String> m = new EnumMap<LC749Enum, String>(LC749Enum.class);
m.put(LC749Enum.A, "value");
assertEquals("{\"A\":\"value\"}", mapper.writeValueAsString(m));
Expand Down