From 74ba48f5e58c9772d8cb239b555571d8067dac76 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sun, 4 Jan 2015 21:47:57 +0200 Subject: [PATCH] Add jackson annotations test --- .../jackson/annotation/BeanWithCreator.java | 16 + .../annotation/BeanWithCustomAnnotation.java | 37 ++ .../jackson/annotation/BeanWithFilter.java | 18 + .../jackson/annotation/BeanWithGetter.java | 31 ++ .../jackson/annotation/BeanWithIgnore.java | 20 + .../jackson/annotation/BeanWithInject.java | 18 + .../jackson/annotation/ExtendableBean.java | 31 ++ .../baeldung/jackson/annotation/MyBean.java | 21 + .../jackson/annotation/PrivateBean.java | 19 + .../baeldung/jackson/annotation/RawBean.java | 19 + .../jackson/annotation/UnwrappedUser.java | 34 ++ .../annotation/UserWithIgnoreType.java | 34 ++ .../org/baeldung/jackson/annotation/Zoo.java | 60 +++ .../jackson/test/JacksonAnnotationTest.java | 367 ++++++++++++++++++ 14 files changed, 725 insertions(+) create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCreator.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCustomAnnotation.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithFilter.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithGetter.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithIgnore.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithInject.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/ExtendableBean.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/MyBean.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/PrivateBean.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/RawBean.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/UnwrappedUser.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/UserWithIgnoreType.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/annotation/Zoo.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/test/JacksonAnnotationTest.java diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCreator.java b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCreator.java new file mode 100644 index 0000000000..d6153032a7 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCreator.java @@ -0,0 +1,16 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + + +public class BeanWithCreator { + public int id; + public String name; + + @JsonCreator + public BeanWithCreator(@JsonProperty("id") final int id, @JsonProperty("theName") final String name) { + this.id = id; + this.name = name; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCustomAnnotation.java b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCustomAnnotation.java new file mode 100644 index 0000000000..3b468f474f --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCustomAnnotation.java @@ -0,0 +1,37 @@ +package org.baeldung.jackson.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.Date; + +import org.baeldung.jackson.annotation.BeanWithCustomAnnotation.CustomAnnotation; + +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@CustomAnnotation +public class BeanWithCustomAnnotation { + public int id; + public String name; + public Date dateCreated; + + public BeanWithCustomAnnotation() { + + } + + public BeanWithCustomAnnotation(final int id, final String name, final Date dateCreated) { + this.id = id; + this.name = name; + this.dateCreated = dateCreated; + } + + @Retention(RetentionPolicy.RUNTIME) + @JacksonAnnotationsInside + @JsonInclude(Include.NON_NULL) + @JsonPropertyOrder({ "name", "id", "dateCreated" }) + public @interface CustomAnnotation { + + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithFilter.java b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithFilter.java new file mode 100644 index 0000000000..a0627757b6 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithFilter.java @@ -0,0 +1,18 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonFilter; + +@JsonFilter("myFilter") +public class BeanWithFilter { + public int id; + public String name; + + public BeanWithFilter() { + + } + + public BeanWithFilter(final int id, final String name) { + this.id = id; + this.name = name; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithGetter.java b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithGetter.java new file mode 100644 index 0000000000..6585018564 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithGetter.java @@ -0,0 +1,31 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonGetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; + +public class BeanWithGetter { + public int id; + private String name; + + public BeanWithGetter() { + + } + + public BeanWithGetter(final int id, final String name) { + this.id = id; + this.name = name; + } + + @JsonProperty("name") + @JsonSetter("name") + public void setTheName(final String name) { + this.name = name; + } + + @JsonProperty("name") + @JsonGetter("name") + public String getTheName() { + return name; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithIgnore.java b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithIgnore.java new file mode 100644 index 0000000000..4f0866bdc0 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithIgnore.java @@ -0,0 +1,20 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties({ "id" }) +public class BeanWithIgnore { + @JsonIgnore + public int id; + public String name; + + public BeanWithIgnore() { + + } + + public BeanWithIgnore(final int id, final String name) { + this.id = id; + this.name = name; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithInject.java b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithInject.java new file mode 100644 index 0000000000..aa8da1da4f --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithInject.java @@ -0,0 +1,18 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JacksonInject; + +public class BeanWithInject { + @JacksonInject + public int id; + public String name; + + public BeanWithInject() { + + } + + public BeanWithInject(final int id, final String name) { + this.id = id; + this.name = name; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/ExtendableBean.java b/jackson/src/test/java/org/baeldung/jackson/annotation/ExtendableBean.java new file mode 100644 index 0000000000..c8e5f453ec --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/ExtendableBean.java @@ -0,0 +1,31 @@ +package org.baeldung.jackson.annotation; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; + +public class ExtendableBean { + public String name; + private Map properties; + + public ExtendableBean() { + properties = new HashMap(); + } + + public ExtendableBean(final String name) { + this.name = name; + properties = new HashMap(); + } + + @JsonAnySetter + public void add(final String key, final String value) { + properties.put(key, value); + } + + @JsonAnyGetter + public Map getProperties() { + return properties; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/MyBean.java b/jackson/src/test/java/org/baeldung/jackson/annotation/MyBean.java new file mode 100644 index 0000000000..651d7c2da0 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/MyBean.java @@ -0,0 +1,21 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(Include.NON_NULL) +@JsonPropertyOrder({ "name", "id" }) +public class MyBean { + public int id; + public String name; + + public MyBean() { + + } + + public MyBean(final int id, final String name) { + this.id = id; + this.name = name; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/PrivateBean.java b/jackson/src/test/java/org/baeldung/jackson/annotation/PrivateBean.java new file mode 100644 index 0000000000..472fa84a26 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/PrivateBean.java @@ -0,0 +1,19 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; + +@JsonAutoDetect(fieldVisibility = Visibility.ANY) +public class PrivateBean { + private int id; + private String name; + + public PrivateBean() { + + } + + public PrivateBean(final int id, final String name) { + this.id = id; + this.name = name; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/RawBean.java b/jackson/src/test/java/org/baeldung/jackson/annotation/RawBean.java new file mode 100644 index 0000000000..a481fbf02e --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/RawBean.java @@ -0,0 +1,19 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonRawValue; + +public class RawBean { + public String name; + + @JsonRawValue + public String json; + + public RawBean() { + + } + + public RawBean(final String name, final String json) { + this.name = name; + this.json = json; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/UnwrappedUser.java b/jackson/src/test/java/org/baeldung/jackson/annotation/UnwrappedUser.java new file mode 100644 index 0000000000..4c761a57e0 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/UnwrappedUser.java @@ -0,0 +1,34 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonUnwrapped; + +public class UnwrappedUser { + public int id; + + @JsonUnwrapped + public Name name; + + public UnwrappedUser() { + + } + + public UnwrappedUser(final int id, final Name name) { + this.id = id; + this.name = name; + } + + public static class Name { + public String firstName; + public String lastName; + + public Name() { + + } + + public Name(final String firstName, final String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/UserWithIgnoreType.java b/jackson/src/test/java/org/baeldung/jackson/annotation/UserWithIgnoreType.java new file mode 100644 index 0000000000..eaf4cb7b74 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/UserWithIgnoreType.java @@ -0,0 +1,34 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonIgnoreType; + +public class UserWithIgnoreType { + public int id; + + public Name name; + + public UserWithIgnoreType() { + + } + + public UserWithIgnoreType(final int id, final Name name) { + this.id = id; + this.name = name; + } + + @JsonIgnoreType + public static class Name { + public String firstName; + public String lastName; + + public Name() { + + } + + public Name(final String firstName, final String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/Zoo.java b/jackson/src/test/java/org/baeldung/jackson/annotation/Zoo.java new file mode 100644 index 0000000000..3c7682f7a9 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/annotation/Zoo.java @@ -0,0 +1,60 @@ +package org.baeldung.jackson.annotation; + +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeInfo.As; +import com.fasterxml.jackson.annotation.JsonTypeName; + +public class Zoo { + public Animal animal; + + public Zoo() { + + } + + public Zoo(final Animal animal) { + this.animal = animal; + } + + + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "type") + @JsonSubTypes({ + @JsonSubTypes.Type(value = Dog.class, name = "dog"), + @JsonSubTypes.Type(value = Cat.class, name = "cat") + }) + public static class Animal { + public String name; + + public Animal() { + } + + public Animal(final String name) { + this.name = name; + } + } + + @JsonTypeName("dog") + public static class Dog extends Animal { + public double barkVolume; + + public Dog() { + } + + public Dog(final String name) { + this.name = name; + } + } + + @JsonTypeName("cat") + public static class Cat extends Animal { + boolean likesCream; + public int lives; + + public Cat() { + } + + public Cat(final String name) { + this.name = name; + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonAnnotationTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonAnnotationTest.java new file mode 100644 index 0000000000..2bdce9b446 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonAnnotationTest.java @@ -0,0 +1,367 @@ +package org.baeldung.jackson.test; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.baeldung.jackson.annotation.BeanWithCreator; +import org.baeldung.jackson.annotation.BeanWithCustomAnnotation; +import org.baeldung.jackson.annotation.BeanWithFilter; +import org.baeldung.jackson.annotation.BeanWithGetter; +import org.baeldung.jackson.annotation.BeanWithIgnore; +import org.baeldung.jackson.annotation.BeanWithInject; +import org.baeldung.jackson.annotation.ExtendableBean; +import org.baeldung.jackson.annotation.MyBean; +import org.baeldung.jackson.annotation.PrivateBean; +import org.baeldung.jackson.annotation.RawBean; +import org.baeldung.jackson.annotation.UnwrappedUser; +import org.baeldung.jackson.annotation.UserWithIgnoreType; +import org.baeldung.jackson.annotation.Zoo; +import org.baeldung.jackson.bidirection.ItemWithIdentity; +import org.baeldung.jackson.bidirection.ItemWithRef; +import org.baeldung.jackson.bidirection.UserWithIdentity; +import org.baeldung.jackson.bidirection.UserWithRef; +import org.baeldung.jackson.date.EventWithFormat; +import org.baeldung.jackson.date.EventWithSerializer; +import org.baeldung.jackson.dtos.MyMixInForString; +import org.baeldung.jackson.dtos.User; +import org.baeldung.jackson.dtos.withEnum.TypeEnumWithValue; +import org.baeldung.jackson.exception.UserWithRoot; +import org.baeldung.jackson.jsonview.Item; +import org.baeldung.jackson.jsonview.Views; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.InjectableValues; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.ser.FilterProvider; +import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; +import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; + + +public class JacksonAnnotationTest { + + @Test + public void whenSerializingUsingJsonAnyGetter_thenCorrect() throws JsonProcessingException { + final ExtendableBean bean = new ExtendableBean("My bean"); + bean.add("attr1", "val1"); + bean.add("attr2", "val2"); + + final String result = new ObjectMapper().writeValueAsString(bean); + assertThat(result, containsString("attr1")); + assertThat(result, containsString("val1")); + } + + @Test + public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException { + final BeanWithGetter bean = new BeanWithGetter(1, "My bean"); + + final String result = new ObjectMapper().writeValueAsString(bean); + assertThat(result, containsString("My bean")); + assertThat(result, containsString("1")); + } + + @Test + public void whenSerializingUsingJsonPropertyOrder_thenCorrect() throws JsonProcessingException { + final MyBean bean = new MyBean(1, "My bean"); + + final String result = new ObjectMapper().writeValueAsString(bean); + assertThat(result, containsString("My bean")); + assertThat(result, containsString("1")); + } + + @Test + public void whenSerializingUsingJsonRawValue_thenCorrect() throws JsonProcessingException { + final RawBean bean = new RawBean("My bean", "{\"attr\":false}"); + + final String result = new ObjectMapper().writeValueAsString(bean); + assertThat(result, containsString("My bean")); + assertThat(result, containsString("{\"attr\":false}")); + } + + @Test + public void whenSerializingUsingJsonRootName_thenCorrect() throws JsonProcessingException { + final UserWithRoot user = new UserWithRoot(1, "John"); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + final String result = mapper.writeValueAsString(user); + + assertThat(result, containsString("John")); + assertThat(result, containsString("user")); + } + + @Test + public void whenSerializingUsingJsonValue_thenCorrect() throws JsonParseException, IOException { + final String enumAsString = new ObjectMapper().writeValueAsString(TypeEnumWithValue.TYPE1); + + assertThat(enumAsString, is("\"Type A\"")); + } + + @Test + public void whenSerializingUsingJsonSerialize_thenCorrect() throws JsonProcessingException, ParseException { + final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + + final String toParse = "20-12-2014 02:30:00"; + final Date date = df.parse(toParse); + final EventWithSerializer event = new EventWithSerializer("party", date); + + final String result = new ObjectMapper().writeValueAsString(event); + assertThat(result, containsString(toParse)); + } + + // ========================= Deserializing annotations ============================ + + @Test + public void whenDeserializingUsingJsonCreator_thenCorrect() throws JsonProcessingException, IOException { + final String json = "{\"id\":1,\"theName\":\"My bean\"}"; + + final BeanWithCreator bean = new ObjectMapper().reader(BeanWithCreator.class).readValue(json); + assertEquals("My bean", bean.name); + } + + @Test + public void whenDeserializingUsingJsonInject_thenCorrect() throws JsonProcessingException, IOException { + final String json = "{\"name\":\"My bean\"}"; + final InjectableValues inject = new InjectableValues.Std().addValue(int.class, 1); + + final BeanWithInject bean = new ObjectMapper().reader(inject).withType(BeanWithInject.class).readValue(json); + assertEquals("My bean", bean.name); + assertEquals(1, bean.id); + } + + @Test + public void whenDeserializingUsingJsonAnySetter_thenCorrect() throws JsonProcessingException, IOException { + final String json = "{\"name\":\"My bean\",\"attr2\":\"val2\",\"attr1\":\"val1\"}"; + + final ExtendableBean bean = new ObjectMapper().reader(ExtendableBean.class).readValue(json); + assertEquals("My bean", bean.name); + assertEquals("val2", bean.getProperties().get("attr2")); + } + + @Test + public void whenDeserializingUsingJsonSetter_thenCorrect() throws JsonProcessingException, IOException { + final String json = "{\"id\":1,\"name\":\"My bean\"}"; + + final BeanWithGetter bean = new ObjectMapper().reader(BeanWithGetter.class).readValue(json); + assertEquals("My bean", bean.getTheName()); + } + + @Test + public void whenDeserializingUsingJsonDeserialize_thenCorrect() throws JsonProcessingException, IOException { + final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014 02:30:00\"}"; + + final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + + final EventWithSerializer event = new ObjectMapper().reader(EventWithSerializer.class).readValue(json); + assertEquals("20-12-2014 02:30:00", df.format(event.eventDate)); + } + + // ========================= Inclusion annotations ============================ + + @Test + public void whenSerializingUsingJsonIgnoreProperties_thenCorrect() throws JsonProcessingException, IOException { + final BeanWithIgnore bean = new BeanWithIgnore(1, "My bean"); + + final String result = new ObjectMapper().writeValueAsString(bean); + assertThat(result, containsString("My bean")); + assertThat(result, not(containsString("id"))); + } + + @Test + public void whenSerializingUsingJsonIgnore_thenCorrect() throws JsonProcessingException, IOException { + final BeanWithIgnore bean = new BeanWithIgnore(1, "My bean"); + + final String result = new ObjectMapper().writeValueAsString(bean); + assertThat(result, containsString("My bean")); + assertThat(result, not(containsString("id"))); + } + + @Test + public void whenSerializingUsingJsonIgnoreType_thenCorrect() throws JsonProcessingException, ParseException { + final UserWithIgnoreType.Name name = new UserWithIgnoreType.Name("John", "Doe"); + final UserWithIgnoreType user = new UserWithIgnoreType(1, name); + + final String result = new ObjectMapper().writeValueAsString(user); + + assertThat(result, containsString("1")); + assertThat(result, not(containsString("name"))); + assertThat(result, not(containsString("John"))); + } + + @Test + public void whenSerializingUsingJsonInclude_thenCorrect() throws JsonProcessingException, IOException { + final MyBean bean = new MyBean(1, null); + + final String result = new ObjectMapper().writeValueAsString(bean); + assertThat(result, containsString("1")); + assertThat(result, not(containsString("name"))); + } + + @Test + public void whenSerializingUsingJsonAutoDetect_thenCorrect() throws JsonProcessingException, IOException { + final PrivateBean bean = new PrivateBean(1, "My bean"); + + final String result = new ObjectMapper().writeValueAsString(bean); + assertThat(result, containsString("1")); + assertThat(result, containsString("My bean")); + } + + // ========================= Polymorphic annotations ============================ + + @Test + public void whenSerializingPolymorphic_thenCorrect() throws JsonProcessingException, IOException { + final Zoo.Dog dog = new Zoo.Dog("lacy"); + final Zoo zoo = new Zoo(dog); + + final String result = new ObjectMapper().writeValueAsString(zoo); + + assertThat(result, containsString("type")); + assertThat(result, containsString("dog")); + } + + @Test + public void whenDeserializingPolymorphic_thenCorrect() throws JsonProcessingException, IOException { + final String json = "{\"animal\":{\"name\":\"lacy\",\"type\":\"cat\"}}"; + + final Zoo zoo = new ObjectMapper().reader().withType(Zoo.class).readValue(json); + + assertEquals("lacy", zoo.animal.name); + assertEquals(Zoo.Cat.class, zoo.animal.getClass()); + } + // ========================= General annotations ============================ + + @Test + public void whenUsingJsonProperty_thenCorrect() throws IOException { + final BeanWithGetter bean = new BeanWithGetter(1, "My bean"); + + final String result = new ObjectMapper().writeValueAsString(bean); + assertThat(result, containsString("My bean")); + assertThat(result, containsString("1")); + + final BeanWithGetter resultBean = new ObjectMapper().reader(BeanWithGetter.class).readValue(result); + assertEquals("My bean", resultBean.getTheName()); + } + + @Test + public void whenSerializingUsingJsonFormat_thenCorrect() throws JsonProcessingException, ParseException { + final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); + + final String toParse = "20-12-2014 02:30:00"; + final Date date = df.parse(toParse); + final EventWithFormat event = new EventWithFormat("party", date); + + final String result = new ObjectMapper().writeValueAsString(event); + assertThat(result, containsString(toParse)); + } + + @Test + public void whenSerializingUsingJsonUnwrapped_thenCorrect() throws JsonProcessingException, ParseException { + final UnwrappedUser.Name name = new UnwrappedUser.Name("John", "Doe"); + final UnwrappedUser user = new UnwrappedUser(1, name); + + final String result = new ObjectMapper().writeValueAsString(user); + assertThat(result, containsString("John")); + assertThat(result, not(containsString("name"))); + } + + @Test + public void whenSerializingUsingJsonView_thenCorrect() throws JsonProcessingException { + final Item item = new Item(2, "book", "John"); + + final String result = new ObjectMapper().writerWithView(Views.Public.class).writeValueAsString(item); + + assertThat(result, containsString("book")); + assertThat(result, containsString("2")); + assertThat(result, not(containsString("John"))); + } + + @Test + public void whenSerializingUsingJacksonReferenceAnnotation_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 whenSerializingUsingJsonIdentityInfo_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 whenSerializingUsingJsonFilter_thenCorrect() throws JsonProcessingException { + final BeanWithFilter bean = new BeanWithFilter(1, "My bean"); + + final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name")); + + final String result = new ObjectMapper().writer(filters).writeValueAsString(bean); + + assertThat(result, containsString("My bean")); + assertThat(result, not(containsString("id"))); + } + + // ========================= + @Test + public void whenSerializingUsingCustomAnnotation_thenCorrect() throws JsonProcessingException { + final BeanWithCustomAnnotation bean = new BeanWithCustomAnnotation(1, "My bean", null); + + final String result = new ObjectMapper().writeValueAsString(bean); + + assertThat(result, containsString("My bean")); + assertThat(result, containsString("1")); + assertThat(result, not(containsString("dateCreated"))); + } + + @Test + public void whenSerializingUsingMixInAnnotation_thenCorrect() throws JsonProcessingException { + final User user = new User(1, "John"); + + String result = new ObjectMapper().writeValueAsString(user); + assertThat(result, containsString("John")); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.addMixInAnnotations(String.class, MyMixInForString.class); + + result = mapper.writeValueAsString(user); + assertThat(result, not(containsString("John"))); + } + + @Test + public void whenDisablingAllAnnotations_thenAllDisabled() throws JsonProcessingException, IOException { + final MyBean bean = new MyBean(1, null); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.disable(MapperFeature.USE_ANNOTATIONS); + + final String result = mapper.writeValueAsString(bean); + assertThat(result, containsString("1")); + assertThat(result, containsString("name")); + } + +}