Java-14972: changes made for adding new example using field annoted @… (#13746)

* Java-19389: changes made for adding new example using field annoted @jsonValue

* Java-14972: Changes made for renaming the test methods and removing commented code

* dev-14972 : Changes made for incorporating review comments

* dev-14972: changes made for adding GeneralBean class and test cases of jsonValue against the GeneralBean class
This commit is contained in:
Bipin kumar 2023-04-05 21:56:04 +05:30 committed by GitHub
parent d1249befd1
commit 9e5fe2d7ac
3 changed files with 82 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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