[improve-jackson-annotation] BAEL-6308 JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES (#13807)
This commit is contained in:
parent
88a97dc6c2
commit
3aaf85dcca
|
@ -1,26 +1,89 @@
|
||||||
package com.baeldung.jackson.format;
|
package com.baeldung.jackson.format;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import com.baeldung.jackson.domain.Person;
|
public class User {
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Jay Sridhar
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class User extends Person {
|
|
||||||
private String firstName;
|
private String firstName;
|
||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
|
||||||
private Date createdDate;
|
private Date createdDate;
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
}
|
||||||
|
|
||||||
public User(String firstName, String lastName) {
|
public User(String firstName, String lastName) {
|
||||||
super(firstName, lastName);
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
this.createdDate = new Date();
|
this.createdDate = new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreatedDate() {
|
||||||
|
return createdDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ", locale = "en_GB")
|
||||||
|
public Date getCurrentDate() {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
|
||||||
|
public Date getDateNum() {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
|
||||||
|
class UserIgnoreCase {
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
|
||||||
|
private Date createdDate;
|
||||||
|
|
||||||
|
public UserIgnoreCase() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserIgnoreCase(String firstName, String lastName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
this.createdDate = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
public Date getCreatedDate() {
|
public Date getCreatedDate() {
|
||||||
return createdDate;
|
return createdDate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,25 @@
|
||||||
package com.baeldung.jackson.format;
|
package com.baeldung.jackson.format;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static io.restassured.path.json.JsonPath.from;
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import static io.restassured.path.json.JsonPath.from;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
import static org.assertj.core.api.Assertions.from;
|
||||||
import static org.assertj.core.data.Percentage.withPercentage;
|
import static org.assertj.core.data.Percentage.withPercentage;
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Jay Sridhar
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class JsonFormatUnitTest {
|
public class JsonFormatUnitTest {
|
||||||
|
|
||||||
|
private static final String JSON_STRING = "{\"FIRSTNAME\":\"John\",\"lastname\":\"Smith\",\"cReAtEdDaTe\":\"2016-12-18@07:53:34.740+0000\"}";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSerializedDateFormat_thenCorrect() throws JsonProcessingException {
|
public void whenSerializedDateFormat_thenCorrect() throws JsonProcessingException {
|
||||||
|
|
||||||
|
@ -32,6 +33,24 @@ public class JsonFormatUnitTest {
|
||||||
// Expected to be close to current time
|
// Expected to be close to current time
|
||||||
long now = new Date().getTime();
|
long now = new Date().getTime();
|
||||||
assertThat(from(result).getLong("dateNum")).isCloseTo(now, withPercentage(10.0));
|
assertThat(from(result).getLong("dateNum")).isCloseTo(now, withPercentage(10.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenDeserializeJsonStrToUserObject_thenFail() {
|
||||||
|
assertThatThrownBy(() -> new ObjectMapper().readValue(JSON_STRING, User.class)).isInstanceOf(UnrecognizedPropertyException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenDeserializeJsonStrToUserIgnoreCaseObject_thenSuccess() throws JsonProcessingException, ParseException {
|
||||||
|
UserIgnoreCase result = new ObjectMapper().readValue(JSON_STRING, UserIgnoreCase.class);
|
||||||
|
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSzz");
|
||||||
|
Date expectedDate = fmt.parse("2016-12-18T07:53:34.740+0000");
|
||||||
|
|
||||||
|
assertThat(result)
|
||||||
|
.isNotNull()
|
||||||
|
.returns("John", from(UserIgnoreCase::getFirstName))
|
||||||
|
.returns("Smith", from(UserIgnoreCase::getLastName))
|
||||||
|
.returns(expectedDate, from(UserIgnoreCase::getCreatedDate));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue