diff --git a/jackson/pom.xml b/jackson/pom.xml index 14e0da23f9..8de8dfd5d8 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -27,6 +27,12 @@ 4.0 + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/Item.java b/jackson/src/test/java/org/baeldung/jackson/dtos/Item.java new file mode 100644 index 0000000000..db834a0733 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/dtos/Item.java @@ -0,0 +1,28 @@ +package org.baeldung.jackson.dtos; + +public class Item { + public final int id; + public final String itemNr; + public final User createdBy; + + public Item(final int id, final String itemNr, final User createdBy) { + this.id = id; + this.itemNr = itemNr; + this.createdBy = createdBy; + } + + // API + + public int getId() { + return id; + } + + public String getItemNr() { + return itemNr; + } + + public User getCreatedBy() { + return createdBy; + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/User.java b/jackson/src/test/java/org/baeldung/jackson/dtos/User.java new file mode 100644 index 0000000000..c2e70ab8e9 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/dtos/User.java @@ -0,0 +1,22 @@ +package org.baeldung.jackson.dtos; + +public class User { + public final int id; + public final String name; + + public User(final int id, final String name) { + this.id = id; + this.name = name; + } + + // API + + public int getId() { + return id; + } + + public String getName() { + return name; + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java new file mode 100644 index 0000000000..a19b6831bd --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java @@ -0,0 +1,57 @@ +package org.baeldung.jackson.test; + +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; + +import java.io.IOException; + +import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnum; +import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom; +import org.baeldung.jackson.dtos.withEnum.TypeEnum; +import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JacksonSerializationEnumsUnitTest { + + // tests - enums + + @Test + public final void whenSerializingSimpleEnum_thenCorrect() throws JsonParseException, IOException { + final ObjectMapper mapper = new ObjectMapper(); + final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1); + + System.out.println(dtoAsString); + assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); + } + + @Test + public final void whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { + final ObjectMapper mapper = new ObjectMapper(); + final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnum("a", 1, true, TypeEnum.TYPE1)); + + System.out.println(dtoAsString); + assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); + } + + @Test + public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { + final ObjectMapper mapper = new ObjectMapper(); + final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1)); + + System.out.println(dtoAsString); + assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); + } + + @Test + public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException { + final ObjectMapper mapper = new ObjectMapper(); + final String json = mapper.writeValueAsString(new TypeEnum[] { TypeEnum.TYPE1, TypeEnum.TYPE2 }); + + System.out.println(json); + assertThat(json, containsString("\"name\":\"Type A\"")); + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java index f510bd93da..ad7aa1b296 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java @@ -1,28 +1,33 @@ package org.baeldung.jackson.test; +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; import java.io.IOException; +import java.io.StringWriter; import java.util.List; +import org.baeldung.jackson.dtos.Item; import org.baeldung.jackson.dtos.MyDto; import org.baeldung.jackson.dtos.MyDtoFieldNameChanged; import org.baeldung.jackson.dtos.MyDtoNoAccessors; import org.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility; -import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnum; -import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom; -import org.baeldung.jackson.dtos.withEnum.TypeEnum; -import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer; +import org.baeldung.jackson.dtos.User; +import org.baeldung.jackson.try1.ItemSerializer; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.Version; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; import com.google.common.collect.Lists; public class JacksonSerializationUnitTest { @@ -82,44 +87,6 @@ public class JacksonSerializationUnitTest { assertThat(dtoAsString, containsString("booleanValue")); } - // tests - enums - - @Test - public final void whenSerializingSimpleEnum_thenCorrect() throws JsonParseException, IOException { - final ObjectMapper mapper = new ObjectMapper(); - final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1); - - System.out.println(dtoAsString); - assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); - } - - @Test - public final void whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { - final ObjectMapper mapper = new ObjectMapper(); - final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnum("a", 1, true, TypeEnum.TYPE1)); - - System.out.println(dtoAsString); - assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); - } - - @Test - public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { - final ObjectMapper mapper = new ObjectMapper(); - final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1)); - - System.out.println(dtoAsString); - assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); - } - - @Test - public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException { - final ObjectMapper mapper = new ObjectMapper(); - final String json = mapper.writeValueAsString(new TypeEnum[] { TypeEnum.TYPE1, TypeEnum.TYPE2 }); - - System.out.println(json); - assertThat(json, containsString("\"name\":\"Type A\"")); - } - // tests - multiple entities to json @Test @@ -132,4 +99,23 @@ public class JacksonSerializationUnitTest { System.out.println(dtosAsString); } + // tests - custom serializer + + @Test + public final void whenSerializingWithCustomSerializer_thenNoExceptions() throws JsonGenerationException, JsonMappingException, IOException { + final Item myItem = new Item(Integer.parseInt(randomNumeric(2)), randomAlphabetic(8), new User(Integer.parseInt(randomNumeric(2)), randomAlphabetic(8))); + + final ObjectMapper mapper = new ObjectMapper(); + + final SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null)); + simpleModule.addSerializer(Item.class, new ItemSerializer()); + // simpleModule.addSerializer(User.class, new UserSerializer()); + mapper.registerModule(simpleModule); + + final StringWriter writer = new StringWriter(); + mapper.writeValue(writer, myItem); + final String serialized = writer.toString(); + System.out.println(serialized); + } + } diff --git a/jackson/src/test/java/org/baeldung/jackson/try1/ItemSerializer.java b/jackson/src/test/java/org/baeldung/jackson/try1/ItemSerializer.java new file mode 100644 index 0000000000..42cdfe92be --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/try1/ItemSerializer.java @@ -0,0 +1,23 @@ +package org.baeldung.jackson.try1; + +import java.io.IOException; + +import org.baeldung.jackson.dtos.Item; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +public class ItemSerializer extends JsonSerializer { + + @Override + public final void serialize(final Item value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { + jgen.writeStartObject(); + jgen.writeNumberField("id", value.id); + jgen.writeStringField("itemNr", value.itemNr); + jgen.writeNumberField("createdBy", value.createdBy.id); + jgen.writeEndObject(); + } + +} \ No newline at end of file