Skip to content

Commit 2a2aac0

Browse files
committed
Bit more testing for #2871 wrt enums
1 parent cf0bcda commit 2a2aac0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/test/java/com/fasterxml/jackson/databind/ser/jdk/EnumSerializationTest.java

+32
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ protected enum EnumWithJsonProperty {
132132
A;
133133
}
134134

135+
// [databind#2871]: add `@JsonKey`
136+
protected enum EnumWithJsonKey {
137+
A("a"), B("b");
138+
private final String name;
139+
private EnumWithJsonKey(String n) {
140+
name = n;
141+
}
142+
143+
@Override
144+
public String toString() { return name; }
145+
146+
@JsonKey
147+
public String externalKey() { return "key:"+name; }
148+
149+
@JsonValue
150+
public String externalValue() { return "value:"+name; }
151+
}
152+
135153
/*
136154
/**********************************************************************
137155
/* Test methods
@@ -298,6 +316,20 @@ public void testEnumsWithJsonPropertyAsKey() throws Exception
298316
input.put(EnumWithJsonProperty.A, "b");
299317
assertEquals("{\"aleph\":\"b\"}", MAPPER.writeValueAsString(input));
300318
}
319+
320+
// [databind#2871]
321+
public void testEnumWithJsonKey() throws Exception
322+
{
323+
// First with EnumMap
324+
EnumMap<EnumWithJsonKey, EnumWithJsonKey> input1 = new EnumMap<>(EnumWithJsonKey.class);
325+
input1.put(EnumWithJsonKey.A, EnumWithJsonKey.B);
326+
assertEquals(a2q("{'key:a':'value:b'}"), MAPPER.writeValueAsString(input1));
327+
328+
// Then regular Map with Enums
329+
Map<EnumWithJsonKey, EnumWithJsonKey> input2
330+
= Collections.singletonMap(EnumWithJsonKey.A, EnumWithJsonKey.B);
331+
assertEquals(a2q("{'key:a':'value:b'}"), MAPPER.writeValueAsString(input2));
332+
}
301333
}
302334

303335
// [JACKSON-757], non-inner enum

0 commit comments

Comments
 (0)