[BAEL-13511] - Create jackson-modules parent for all related modules
This commit is contained in:
+133
@@ -0,0 +1,133 @@
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.jackson.advancedannotations.AppendBeans.BeanWithAppend;
|
||||
import com.baeldung.jackson.advancedannotations.AppendBeans.BeanWithoutAppend;
|
||||
import com.baeldung.jackson.advancedannotations.IdentityReferenceBeans.BeanWithIdentityReference;
|
||||
import com.baeldung.jackson.advancedannotations.IdentityReferenceBeans.BeanWithoutIdentityReference;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
|
||||
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
|
||||
|
||||
public class AdvancedAnnotationsUnitTest {
|
||||
@Test
|
||||
public void whenNotUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
BeanWithoutIdentityReference bean = new BeanWithoutIdentityReference(1, "Bean Without Identity Reference Annotation");
|
||||
String jsonString = mapper.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, containsString("Bean Without Identity Reference Annotation"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
BeanWithIdentityReference bean = new BeanWithIdentityReference(1, "Bean With Identity Reference Annotation");
|
||||
String jsonString = mapper.writeValueAsString(bean);
|
||||
|
||||
assertEquals("1", jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotUsingJsonAppendAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
BeanWithoutAppend bean = new BeanWithoutAppend(2, "Bean Without Append Annotation");
|
||||
ObjectWriter writer = mapper.writerFor(BeanWithoutAppend.class)
|
||||
.withAttribute("version", "1.0");
|
||||
String jsonString = writer.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, not(containsString("version")));
|
||||
assertThat(jsonString, not(containsString("1.0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonAppendAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
BeanWithAppend bean = new BeanWithAppend(2, "Bean With Append Annotation");
|
||||
ObjectWriter writer = mapper.writerFor(BeanWithAppend.class)
|
||||
.withAttribute("version", "1.0");
|
||||
String jsonString = writer.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, containsString("version"));
|
||||
assertThat(jsonString, containsString("1.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonNamingAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
NamingBean bean = new NamingBean(3, "Naming Bean");
|
||||
String jsonString = mapper.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, containsString("bean_name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonPropertyDescriptionAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
SchemaFactoryWrapper wrapper = new SchemaFactoryWrapper();
|
||||
mapper.acceptJsonFormatVisitor(PropertyDescriptionBean.class, wrapper);
|
||||
JsonSchema jsonSchema = wrapper.finalSchema();
|
||||
String jsonString = mapper.writeValueAsString(jsonSchema);
|
||||
System.out.println(jsonString);
|
||||
assertThat(jsonString, containsString("This is a description of the name property"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonPOJOBuilderAnnotation_thenCorrect() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String jsonString = "{\"id\":5,\"name\":\"POJO Builder Bean\"}";
|
||||
POJOBuilderBean bean = mapper.readValue(jsonString, POJOBuilderBean.class);
|
||||
|
||||
assertEquals(5, bean.getIdentity());
|
||||
assertEquals("POJO Builder Bean", bean.getBeanName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonTypeIdAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.enableDefaultTyping(DefaultTyping.NON_FINAL);
|
||||
TypeIdBean bean = new TypeIdBean(6, "Type Id Bean");
|
||||
String jsonString = mapper.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, containsString("Type Id Bean"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonTypeIdResolverAnnotation_thenCorrect() throws IOException {
|
||||
TypeIdResolverStructure.FirstBean bean1 = new TypeIdResolverStructure.FirstBean(1, "Bean 1");
|
||||
TypeIdResolverStructure.LastBean bean2 = new TypeIdResolverStructure.LastBean(2, "Bean 2");
|
||||
|
||||
List<TypeIdResolverStructure.AbstractBean> beans = new ArrayList<>();
|
||||
beans.add(bean1);
|
||||
beans.add(bean2);
|
||||
|
||||
TypeIdResolverStructure.BeanContainer serializedContainer = new TypeIdResolverStructure.BeanContainer();
|
||||
serializedContainer.setBeans(beans);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String jsonString = mapper.writeValueAsString(serializedContainer);
|
||||
assertThat(jsonString, containsString("bean1"));
|
||||
assertThat(jsonString, containsString("bean2"));
|
||||
|
||||
TypeIdResolverStructure.BeanContainer deserializedContainer = mapper.readValue(jsonString, TypeIdResolverStructure.BeanContainer.class);
|
||||
List<TypeIdResolverStructure.AbstractBean> beanList = deserializedContainer.getBeans();
|
||||
assertThat(beanList.get(0), instanceOf(TypeIdResolverStructure.FirstBean.class));
|
||||
assertThat(beanList.get(1), instanceOf(TypeIdResolverStructure.LastBean.class));
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonAppend;
|
||||
|
||||
public class AppendBeans {
|
||||
public static class BeanWithoutAppend {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public BeanWithoutAppend(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonAppend(attrs = { @JsonAppend.Attr(value = "version") })
|
||||
public static class BeanWithAppend {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public BeanWithAppend(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityReference;
|
||||
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
||||
|
||||
public class IdentityReferenceBeans {
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
|
||||
public static class BeanWithoutIdentityReference {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public BeanWithoutIdentityReference(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
|
||||
@JsonIdentityReference(alwaysAsId = true)
|
||||
public static class BeanWithIdentityReference {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public BeanWithIdentityReference(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
|
||||
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
|
||||
public class NamingBean {
|
||||
private int id;
|
||||
private String beanName;
|
||||
|
||||
public NamingBean(int id, String beanName) {
|
||||
this.id = id;
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
|
||||
|
||||
@JsonDeserialize(builder = POJOBuilderBean.BeanBuilder.class)
|
||||
public class POJOBuilderBean {
|
||||
private int identity;
|
||||
private String beanName;
|
||||
|
||||
@JsonPOJOBuilder(buildMethodName = "createBean", withPrefix = "construct")
|
||||
public static class BeanBuilder {
|
||||
private int idValue;
|
||||
private String nameValue;
|
||||
|
||||
public BeanBuilder constructId(int id) {
|
||||
idValue = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BeanBuilder constructName(String name) {
|
||||
nameValue = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public POJOBuilderBean createBean() {
|
||||
return new POJOBuilderBean(idValue, nameValue);
|
||||
}
|
||||
}
|
||||
|
||||
public POJOBuilderBean(int identity, String beanName) {
|
||||
this.identity = identity;
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public int getIdentity() {
|
||||
return identity;
|
||||
}
|
||||
|
||||
public void setIdentity(int identity) {
|
||||
this.identity = identity;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
|
||||
|
||||
public class PropertyDescriptionBean {
|
||||
private int id;
|
||||
@JsonPropertyDescription("This is a description of the name property")
|
||||
private String name;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeId;
|
||||
|
||||
public class TypeIdBean {
|
||||
private int id;
|
||||
@JsonTypeId
|
||||
private String name;
|
||||
|
||||
public TypeIdBean(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
|
||||
import com.fasterxml.jackson.databind.DatabindContext;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
|
||||
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
|
||||
|
||||
public class TypeIdResolverStructure {
|
||||
public static class BeanContainer {
|
||||
private List<AbstractBean> beans;
|
||||
|
||||
public List<AbstractBean> getBeans() {
|
||||
return beans;
|
||||
}
|
||||
|
||||
public void setBeans(List<AbstractBean> beans) {
|
||||
this.beans = beans;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type")
|
||||
@JsonTypeIdResolver(BeanIdResolver.class)
|
||||
public static class AbstractBean {
|
||||
private int id;
|
||||
|
||||
protected AbstractBean() {
|
||||
}
|
||||
|
||||
protected AbstractBean(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
public static class FirstBean extends AbstractBean {
|
||||
String firstName;
|
||||
|
||||
public FirstBean() {
|
||||
}
|
||||
|
||||
public FirstBean(int id, String name) {
|
||||
super(id);
|
||||
setFirstName(name);
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String name) {
|
||||
firstName = name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class LastBean extends AbstractBean {
|
||||
String lastName;
|
||||
|
||||
public LastBean() {
|
||||
}
|
||||
|
||||
public LastBean(int id, String name) {
|
||||
super(id);
|
||||
setLastName(name);
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String name) {
|
||||
lastName = name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BeanIdResolver extends TypeIdResolverBase {
|
||||
private JavaType superType;
|
||||
|
||||
@Override
|
||||
public void init(JavaType baseType) {
|
||||
superType = baseType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Id getMechanism() {
|
||||
return Id.NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String idFromValue(Object obj) {
|
||||
return idFromValueAndType(obj, obj.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String idFromValueAndType(Object obj, Class<?> subType) {
|
||||
String typeId = null;
|
||||
switch (subType.getSimpleName()) {
|
||||
case "FirstBean":
|
||||
typeId = "bean1";
|
||||
break;
|
||||
case "LastBean":
|
||||
typeId = "bean2";
|
||||
}
|
||||
return typeId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaType typeFromId(DatabindContext context, String id) {
|
||||
Class<?> subType = null;
|
||||
switch (id) {
|
||||
case "bean1":
|
||||
subType = FirstBean.class;
|
||||
break;
|
||||
case "bean2":
|
||||
subType = LastBean.class;
|
||||
}
|
||||
return context.constructSpecializedType(superType, subType);
|
||||
}
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package com.baeldung.jackson.bidirection;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.bidirection.jsonview.Views;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JacksonBidirectionRelationUnitTest {
|
||||
|
||||
@Test(expected = JsonMappingException.class)
|
||||
public void givenBidirectionRelation_whenSerializing_thenException() throws JsonProcessingException {
|
||||
final User user = new User(1, "John");
|
||||
final Item item = new Item(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
new ObjectMapper().writeValueAsString(item);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithRef user = new UserWithRef(1, "John");
|
||||
final ItemWithRef item = new ItemWithRef(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("userItems")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingJsonIdentityInfo_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithIdentity user = new UserWithIdentity(1, "John");
|
||||
final ItemWithIdentity item = new ItemWithIdentity(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, containsString("userItems"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingJsonIgnore_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithIgnore user = new UserWithIgnore(1, "John");
|
||||
final ItemWithIgnore item = new ItemWithIgnore(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("userItems")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingCustomSerializer_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithSerializer user = new UserWithSerializer(1, "John");
|
||||
final ItemWithSerializer item = new ItemWithSerializer(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, containsString("userItems"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenDeserializingUsingIdentity_thenCorrect() throws JsonProcessingException, IOException {
|
||||
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
|
||||
|
||||
final ItemWithIdentity item = new ObjectMapper().readerFor(ItemWithIdentity.class)
|
||||
.readValue(json);
|
||||
|
||||
assertEquals(2, item.id);
|
||||
assertEquals("book", item.itemName);
|
||||
assertEquals("John", item.owner.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException {
|
||||
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
|
||||
|
||||
final ItemWithSerializer item = new ObjectMapper().readerFor(ItemWithSerializer.class)
|
||||
.readValue(json);
|
||||
assertEquals(2, item.id);
|
||||
assertEquals("book", item.itemName);
|
||||
assertEquals("John", item.owner.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingPublicJsonView_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithView user = new UserWithView(1, "John");
|
||||
final ItemWithView item = new ItemWithView(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writerWithView(Views.Public.class)
|
||||
.writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("userItems")));
|
||||
}
|
||||
|
||||
@Test(expected = JsonMappingException.class)
|
||||
public void givenBidirectionRelation_whenUsingInternalJsonView_thenException() throws JsonProcessingException {
|
||||
final UserWithView user = new UserWithView(1, "John");
|
||||
final ItemWithView item = new ItemWithView(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
new ObjectMapper().writerWithView(Views.Internal.class)
|
||||
.writeValueAsString(item);
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.jackson.format;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.data.Percentage.withPercentage;
|
||||
|
||||
/**
|
||||
* @author Jay Sridhar
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonFormatUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializedDateFormat_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
User user = new User("Jay", "Sridhar");
|
||||
|
||||
String result = new ObjectMapper().writeValueAsString(user);
|
||||
|
||||
// Expected to match: "2016-12-19@09:34:42.628+0000"
|
||||
assertThat(from(result).getString("createdDate")).matches("\\d{4}\\-\\d{2}\\-\\d{2}@\\d{2}:\\d{2}:\\d{2}\\.\\d{3}\\+\\d{4}");
|
||||
|
||||
// Expected to be close to current time
|
||||
long now = new Date().getTime();
|
||||
assertThat(from(result).getLong("dateNum")).isCloseTo(now, withPercentage(10.0));
|
||||
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.jackson.jsonview;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.jsonview.Item;
|
||||
import com.baeldung.jackson.jsonview.MyBeanSerializerModifier;
|
||||
import com.baeldung.jackson.jsonview.User;
|
||||
import com.baeldung.jackson.jsonview.Views;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
|
||||
import com.fasterxml.jackson.databind.ser.SerializerFactory;
|
||||
|
||||
public class JacksonJsonViewUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUseJsonViewToSerialize_thenCorrect() throws JsonProcessingException {
|
||||
final User user = new User(1, "John");
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
|
||||
|
||||
final String result = mapper.writerWithView(Views.Public.class)
|
||||
.writeValueAsString(user);
|
||||
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsePublicView_thenOnlyPublicSerialized() throws JsonProcessingException {
|
||||
final Item item = new Item(2, "book", "John");
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String result = mapper.writerWithView(Views.Public.class)
|
||||
.writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("2"));
|
||||
|
||||
assertThat(result, not(containsString("John")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseInternalView_thenAllSerialized() throws JsonProcessingException {
|
||||
final Item item = new Item(2, "book", "John");
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String result = mapper.writerWithView(Views.Internal.class)
|
||||
.writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("2"));
|
||||
|
||||
assertThat(result, containsString("John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseJsonViewToDeserialize_thenCorrect() throws IOException {
|
||||
final String json = "{\"id\":1,\"name\":\"John\"}";
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final User user = mapper.readerWithView(Views.Public.class)
|
||||
.forType(User.class)
|
||||
.readValue(json);
|
||||
assertEquals(1, user.getId());
|
||||
assertEquals("John", user.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseCustomJsonViewToSerialize_thenCorrect() throws JsonProcessingException {
|
||||
final User user = new User(1, "John");
|
||||
final SerializerFactory serializerFactory = BeanSerializerFactory.instance.withSerializerModifier(new MyBeanSerializerModifier());
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializerFactory(serializerFactory);
|
||||
|
||||
final String result = mapper.writerWithView(Views.Public.class)
|
||||
.writeValueAsString(user);
|
||||
assertThat(result, containsString("JOHN"));
|
||||
assertThat(result, containsString("1"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user