diff --git a/jackson-simple/src/main/java/com/baeldung/jackson/annotation/GeneralBean.java b/jackson-simple/src/main/java/com/baeldung/jackson/annotation/GeneralBean.java new file mode 100644 index 0000000000..a8333f54ae --- /dev/null +++ b/jackson-simple/src/main/java/com/baeldung/jackson/annotation/GeneralBean.java @@ -0,0 +1,26 @@ +package com.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonValue; + +public class GeneralBean { + Integer id; + + @JsonValue + String name; + + public GeneralBean() { + } + + public GeneralBean(Integer id, String name) { + this.id = id; + this.name = name; + } + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } +} diff --git a/jackson-simple/src/main/java/com/baeldung/jackson/annotation/dtos/withEnum/TypeEnumWithValue.java b/jackson-simple/src/main/java/com/baeldung/jackson/annotation/dtos/withEnum/TypeEnumWithValue.java new file mode 100644 index 0000000000..2c8718dfd8 --- /dev/null +++ b/jackson-simple/src/main/java/com/baeldung/jackson/annotation/dtos/withEnum/TypeEnumWithValue.java @@ -0,0 +1,23 @@ +package com.baeldung.jackson.annotation.dtos.withEnum; + +import com.fasterxml.jackson.annotation.JsonValue; + +public enum TypeEnumWithValue { + TYPE1(1, "Type A"), TYPE2(2, "Type 2"); + + private Integer id; + + @JsonValue + private String name; + + + TypeEnumWithValue(int id, String name) { + this.id = id; + this.name = name; + } + + //@JsonValue + public String getName() { + return name; + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/JacksonAnnotationUnitTest.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/JacksonAnnotationUnitTest.java index bbbb79b0a8..9fb982842f 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/JacksonAnnotationUnitTest.java +++ b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/JacksonAnnotationUnitTest.java @@ -20,6 +20,7 @@ import com.baeldung.jackson.annotation.bidirection.UserWithIdentity; import com.baeldung.jackson.annotation.bidirection.UserWithRef; import com.baeldung.jackson.annotation.date.EventWithFormat; import com.baeldung.jackson.annotation.date.EventWithSerializer; +import com.baeldung.jackson.annotation.dtos.withEnum.TypeEnumWithValue; import com.baeldung.jackson.annotation.ignore.MyMixInForIgnoreType; import com.baeldung.jackson.annotation.dtos.withEnum.DistanceEnumWithValue; import com.baeldung.jackson.annotation.exception.UserWithRoot; @@ -95,6 +96,7 @@ public class JacksonAnnotationUnitTest { assertThat(enumAsString, is("1609.34")); } + @Test public void whenSerializingUsingJsonSerialize_thenCorrect() throws JsonProcessingException, ParseException { final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); @@ -107,6 +109,19 @@ public class JacksonAnnotationUnitTest { assertThat(result, containsString(toParse)); } + @Test + public void whenSerializingUsingJsonValueAnnotatedField_thenCorrect() throws JsonProcessingException { + final String enumValue = new ObjectMapper().writeValueAsString(TypeEnumWithValue.TYPE1); + assertThat(enumValue, is("\"Type A\"")); + } + + @Test + public void whenSerializingUsingJsonValueAnnotatedFieldInPojo_thenCorrect() throws JsonProcessingException { + GeneralBean bean1 = new GeneralBean(1, "Bean 1"); + final String bean1AsString = new ObjectMapper().writeValueAsString(bean1); + assertThat(bean1AsString, is("\"Bean 1\"")); + } + // ========================= Deserializing annotations ============================ @Test @@ -118,6 +133,7 @@ public class JacksonAnnotationUnitTest { assertEquals("My bean", bean.name); } + @Test public void whenDeserializingUsingJsonInject_thenCorrect() throws IOException { final String json = "{\"name\":\"My bean\"}"; @@ -161,6 +177,23 @@ public class JacksonAnnotationUnitTest { assertEquals("20-12-2014 02:30:00", df.format(event.eventDate)); } + @Test + public void whenDeserializingUsingJsonValue_thenCorrect() throws JsonProcessingException { + final String str = "\"Type A\""; + TypeEnumWithValue te = new ObjectMapper().readerFor(TypeEnumWithValue.class) + .readValue(str); + assertThat(te, is(TypeEnumWithValue.TYPE1)); + } + + @Test(expected = Exception.class) + public void whenDeserializingUsingJsonValueAnnotatedFieldInPojo_thenGetException() throws JsonProcessingException { + GeneralBean bean1 = new GeneralBean(1, "Bean 1"); + final String bean1AsString = new ObjectMapper().writeValueAsString(bean1); + GeneralBean bean = new ObjectMapper().readerFor(GeneralBean.class) + .readValue(bean1AsString); + assertThat(bean.getName(), is(bean1.getName())); + } + // ========================= Inclusion annotations ============================ @Test