using @JsonValue with enums and jackson
This commit is contained in:
parent
8a13a58c04
commit
4bb514dc09
@ -2,7 +2,6 @@ package org.baeldung.jackson.dtos.withEnum;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonValue;
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
|
|
||||||
public enum TypeEnumWithValue {
|
public enum TypeEnumWithValue {
|
||||||
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
|
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@ import org.baeldung.jackson.dtos.withEnum.TypeEnum;
|
|||||||
import org.baeldung.jackson.dtos.withEnum.TypeEnumSimple;
|
import org.baeldung.jackson.dtos.withEnum.TypeEnumSimple;
|
||||||
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer;
|
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer;
|
||||||
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithValue;
|
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithValue;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
@ -24,7 +23,7 @@ public class JacksonSerializationEnumsUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public final void whenSerializingASimpleEnum_thenCorrect() throws JsonParseException, IOException {
|
public final void whenSerializingASimpleEnum_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final String dtoAsString = mapper.writeValueAsString(TypeEnumSimple.TYPE1.name());
|
final String dtoAsString = mapper.writeValueAsString(TypeEnumSimple.TYPE1);
|
||||||
System.out.println(dtoAsString);
|
System.out.println(dtoAsString);
|
||||||
|
|
||||||
assertThat(dtoAsString, containsString("TYPE1"));
|
assertThat(dtoAsString, containsString("TYPE1"));
|
||||||
@ -33,10 +32,10 @@ public class JacksonSerializationEnumsUnitTest {
|
|||||||
// tests - enum with main value
|
// tests - enum with main value
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore("https://github.com/FasterXML/jackson-databind/issues/47")
|
// @Ignore("https://github.com/FasterXML/jackson-databind/issues/47")
|
||||||
public final void whenSerializingAEnumWithValue_thenCorrect() throws JsonParseException, IOException {
|
public final void whenSerializingAEnumWithValue_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final String dtoAsString = mapper.writeValueAsString(TypeEnumWithValue.TYPE1.name());
|
final String dtoAsString = mapper.writeValueAsString(TypeEnumWithValue.TYPE1);
|
||||||
System.out.println(dtoAsString);
|
System.out.println(dtoAsString);
|
||||||
|
|
||||||
assertThat(dtoAsString, containsString("Type A"));
|
assertThat(dtoAsString, containsString("Type A"));
|
||||||
@ -47,7 +46,7 @@ public class JacksonSerializationEnumsUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public final void whenSerializingAnEnum_thenCorrect() throws JsonParseException, IOException {
|
public final void whenSerializingAnEnum_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1.name());
|
final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1);
|
||||||
|
|
||||||
System.out.println(dtoAsString);
|
System.out.println(dtoAsString);
|
||||||
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
|
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user