diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumWithValue.java b/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumWithValue.java new file mode 100644 index 0000000000..2754fe9600 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumWithValue.java @@ -0,0 +1,37 @@ +package org.baeldung.jackson.dtos.withEnum; + +import com.fasterxml.jackson.annotation.JsonValue; + + +public enum TypeEnumWithValue { + TYPE1(1, "Type A"), TYPE2(2, "Type 2"); + + private Integer id; + + private String name; + + private TypeEnumWithValue(final Integer id, final String name) { + this.id = id; + this.name = name; + } + + // API + + public Integer getId() { + return id; + } + + public void setId(final Integer id) { + this.id = id; + } + + @JsonValue + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java index bdf8c888fb..316da46e91 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java @@ -10,6 +10,8 @@ import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom; import org.baeldung.jackson.dtos.withEnum.TypeEnum; import org.baeldung.jackson.dtos.withEnum.TypeEnumSimple; import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer; +import org.baeldung.jackson.dtos.withEnum.TypeEnumWithValue; +import org.junit.Ignore; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; @@ -17,7 +19,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonSerializationEnumsUnitTest { - // tests - enums + // tests - simple enum @Test public final void whenSerializingASimpleEnum_thenCorrect() throws JsonParseException, IOException { @@ -28,6 +30,20 @@ public class JacksonSerializationEnumsUnitTest { assertThat(dtoAsString, containsString("TYPE1")); } + // tests - enum with main value + + @Test + @Ignore("https://github.com/FasterXML/jackson-databind/issues/47") + public final void whenSerializingAEnumWithValue_thenCorrect() throws JsonParseException, IOException { + final ObjectMapper mapper = new ObjectMapper(); + final String dtoAsString = mapper.writeValueAsString(TypeEnumWithValue.TYPE1.name()); + System.out.println(dtoAsString); + + assertThat(dtoAsString, containsString("Type A")); + } + + // tests - enum + @Test public final void whenSerializingAnEnum_thenCorrect() throws JsonParseException, IOException { final ObjectMapper mapper = new ObjectMapper(); @@ -46,15 +62,6 @@ public class JacksonSerializationEnumsUnitTest { assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); } - @Test - public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { - final ObjectMapper mapper = new ObjectMapper(); - final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1)); - - System.out.println(dtoAsString); - assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); - } - @Test public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException { final ObjectMapper mapper = new ObjectMapper(); @@ -64,4 +71,15 @@ public class JacksonSerializationEnumsUnitTest { assertThat(json, containsString("\"name\":\"Type A\"")); } + // tests - enum with custom serializer + + @Test + public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { + final ObjectMapper mapper = new ObjectMapper(); + final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1)); + + System.out.println(dtoAsString); + assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); + } + } diff --git a/jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java b/jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java new file mode 100644 index 0000000000..2eb24928d6 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java @@ -0,0 +1,20 @@ +package org.baeldung.jackson.test; + +import org.baeldung.jackson.sandbox.JacksonPrettyPrintUnitTest; +import org.baeldung.jackson.sandbox.SandboxTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({// @formatter:off + JacksonCollectionDeserializationUnitTest.class + ,JacksonSerializationEnumsUnitTest.class + ,JacksonDeserializationUnitTest.class + ,JacksonDeserializationUnitTest.class + ,JacksonPrettyPrintUnitTest.class + ,JacksonSerializationIgnoreUnitTest.class + ,JacksonSerializationUnitTest.class + ,SandboxTest.class +}) // @formatter:on +public class UnitTestSuite { +} \ No newline at end of file