diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java index 23dd278ff2..feda080057 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java @@ -4,7 +4,6 @@ import java.util.*; import com.fasterxml.jackson.annotation.*; -import com.fasterxml.jackson.annotation.JsonFormat.Shape; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; @@ -114,32 +113,6 @@ static enum OK { protected String key; OK(String key) { this.key = key; } } - - // Types for [https://github.com/FasterXML/jackson-databind/issues/24] - // (Enums as JSON Objects) - - @JsonFormat(shape=JsonFormat.Shape.OBJECT) - static enum PoNUM { - A("a1"), B("b2"); - - @JsonProperty - protected final String value; - - private PoNUM(String v) { value = v; } - - public String getValue() { return value; } - } - - static class PoNUMContainer { - @JsonFormat(shape=Shape.NUMBER) - public OK text = OK.V1; - } - - @JsonFormat(shape=JsonFormat.Shape.ARRAY) // alias for 'number', as of 2.5 - static enum PoAsArray - { - A, B; - } @SuppressWarnings({ "rawtypes", "serial" }) static class LowerCasingEnumSerializer extends StdSerializer @@ -152,19 +125,6 @@ public void serialize(Enum value, JsonGenerator jgen, } } - // for [databind#572] - static class PoOverrideAsString - { - @JsonFormat(shape=Shape.STRING) - public PoNUM value = PoNUM.B; - } - - static class PoOverrideAsNumber - { - @JsonFormat(shape=Shape.NUMBER) - public PoNUM value = PoNUM.B; - } - static enum MyEnum594 { VALUE_WITH_A_REALLY_LONG_NAME_HERE("longValue"); @@ -348,22 +308,6 @@ public void testAnnotationsOnEnumCtor() throws Exception assertEquals(quote("V2"), MAPPER.writeValueAsString(NOT_OK2.V2)); } - // Tests for [issue#24] - - public void testEnumAsObjectValid() throws Exception { - assertEquals("{\"value\":\"a1\"}", MAPPER.writeValueAsString(PoNUM.A)); - } - - public void testEnumAsIndexViaAnnotations() throws Exception { - assertEquals("{\"text\":0}", MAPPER.writeValueAsString(new PoNUMContainer())); - } - - // As of 2.5, use of Shape.ARRAY is legal alias for "write as number" - public void testEnumAsObjectBroken() throws Exception - { - assertEquals("0", MAPPER.writeValueAsString(PoAsArray.A)); - } - // [Issue#227] public void testGenericEnumSerializer() throws Exception { @@ -375,15 +319,6 @@ public void testGenericEnumSerializer() throws Exception assertEquals(quote("b"), m.writeValueAsString(TestEnum.B)); } - // [databind#572] - public void testOverrideEnumAsString() throws Exception { - assertEquals("{\"value\":\"B\"}", MAPPER.writeValueAsString(new PoOverrideAsString())); - } - - public void testOverrideEnumAsNumber() throws Exception { - assertEquals("{\"value\":1}", MAPPER.writeValueAsString(new PoOverrideAsNumber())); - } - // [databind#594] public void testJsonValueForEnumMapKey() throws Exception { assertEquals(aposToQuotes("{'stuff':{'longValue':'foo'}}"), diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/EnumFormatShapeTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/EnumFormatShapeTest.java new file mode 100644 index 0000000000..c95f8e8d44 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/databind/struct/EnumFormatShapeTest.java @@ -0,0 +1,113 @@ +package com.fasterxml.jackson.databind.struct; + +import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.annotation.JsonFormat.Shape; + +import com.fasterxml.jackson.databind.*; + +/** + * Unit tests for verifying serialization of simple basic non-structured + * types; primitives (and/or their wrappers), Strings. + */ +public class EnumFormatShapeTest + extends BaseMapTest +{ + @JsonFormat(shape=JsonFormat.Shape.OBJECT) + static enum PoNUM { + A("a1"), B("b2"); + + @JsonProperty + protected final String value; + + private PoNUM(String v) { value = v; } + + public String getValue() { return value; } + } + + static enum OK { + V1("v1"); + protected String key; + OK(String key) { this.key = key; } + } + + static class PoNUMContainer { + @JsonFormat(shape=Shape.NUMBER) + public OK text = OK.V1; + } + + @JsonFormat(shape=JsonFormat.Shape.ARRAY) // alias for 'number', as of 2.5 + static enum PoAsArray + { + A, B; + } + + // for [databind#572] + static class PoOverrideAsString + { + @JsonFormat(shape=Shape.STRING) + public PoNUM value = PoNUM.B; + } + + static class PoOverrideAsNumber + { + @JsonFormat(shape=Shape.NUMBER) + public PoNUM value = PoNUM.B; + } + + // for [databind#1543] + @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT) + enum Color { + RED, + YELLOW, + GREEN + } + + static class ColorWrapper { + public final Color color; + + ColorWrapper(Color color) { + this.color = color; + } + } + + /* + /********************************************************** + /* Tests + /********************************************************** + */ + + private final ObjectMapper MAPPER = new ObjectMapper(); + + // Tests for JsonFormat.shape + + public void testEnumAsObjectValid() throws Exception { + assertEquals("{\"value\":\"a1\"}", MAPPER.writeValueAsString(PoNUM.A)); + } + + public void testEnumAsIndexViaAnnotations() throws Exception { + assertEquals("{\"text\":0}", MAPPER.writeValueAsString(new PoNUMContainer())); + } + + // As of 2.5, use of Shape.ARRAY is legal alias for "write as number" + public void testEnumAsObjectBroken() throws Exception + { + assertEquals("0", MAPPER.writeValueAsString(PoAsArray.A)); + } + + // [databind#572] + public void testOverrideEnumAsString() throws Exception { + assertEquals("{\"value\":\"B\"}", MAPPER.writeValueAsString(new PoOverrideAsString())); + } + + public void testOverrideEnumAsNumber() throws Exception { + assertEquals("{\"value\":1}", MAPPER.writeValueAsString(new PoOverrideAsNumber())); + } + + // for [databind#1543] + public void testEnumAsNumber() throws Exception { + assertEquals(String.valueOf(Color.GREEN.ordinal()), + MAPPER.writeValueAsString(Color.GREEN)); + assertEquals(String.format(aposToQuotes("{'color':'%s'}"), Color.GREEN.ordinal()), + MAPPER.writeValueAsString(new ColorWrapper(Color.GREEN))); + } +}