From ee37eb8a31350f755927cfe79dce608f49fd5990 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 24 Dec 2013 18:37:39 +0200 Subject: [PATCH] enum work --- .../baeldung/jackson/dtos/MyDtoWithEnum.java | 57 +++++++++++++++++++ .../jackson/dtos/MyDtoWithEnumCustom.java | 57 +++++++++++++++++++ .../org/baeldung/jackson/dtos/TypeEnum.java | 35 ++++++++++++ .../dtos/TypeEnumWithCustomSerializer.java | 35 ++++++++++++ .../baeldung/jackson/dtos/TypeSerializer.java | 22 +++++++ .../test/JacksonSerializationUnitTest.java | 43 +++++++++++++- 6 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithEnum.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithEnumCustom.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/dtos/TypeEnum.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/dtos/TypeEnumWithCustomSerializer.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/dtos/TypeSerializer.java diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithEnum.java b/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithEnum.java new file mode 100644 index 0000000000..015e286307 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithEnum.java @@ -0,0 +1,57 @@ +package org.baeldung.jackson.dtos; + +public class MyDtoWithEnum { + + private String stringValue; + private int intValue; + private boolean booleanValue; + private TypeEnum type; + + public MyDtoWithEnum() { + super(); + } + + public MyDtoWithEnum(final String stringValue, final int intValue, final boolean booleanValue, final TypeEnum type) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + this.type = type; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + public TypeEnum getType() { + return type; + } + + public void setType(final TypeEnum type) { + this.type = type; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithEnumCustom.java b/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithEnumCustom.java new file mode 100644 index 0000000000..825dbb2577 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithEnumCustom.java @@ -0,0 +1,57 @@ +package org.baeldung.jackson.dtos; + +public class MyDtoWithEnumCustom { + + private String stringValue; + private int intValue; + private boolean booleanValue; + private TypeEnumWithCustomSerializer type; + + public MyDtoWithEnumCustom() { + super(); + } + + public MyDtoWithEnumCustom(final String stringValue, final int intValue, final boolean booleanValue, final TypeEnumWithCustomSerializer type) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + this.type = type; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + public TypeEnumWithCustomSerializer getType() { + return type; + } + + public void setType(final TypeEnumWithCustomSerializer type) { + this.type = type; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/TypeEnum.java b/jackson/src/test/java/org/baeldung/jackson/dtos/TypeEnum.java new file mode 100644 index 0000000000..8d2358666a --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/dtos/TypeEnum.java @@ -0,0 +1,35 @@ +package org.baeldung.jackson.dtos; + +import com.fasterxml.jackson.annotation.JsonFormat; + +@JsonFormat(shape = JsonFormat.Shape.OBJECT) +public enum TypeEnum { + TYPE1(1, "Type A"), TYPE2(2, "Type 2"); + + private Integer id; + private String name; + + private TypeEnum(final Integer id, final String name) { + this.id = id; + this.name = name; + } + + // API + + public Integer getId() { + return id; + } + + public void setId(final Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/TypeEnumWithCustomSerializer.java b/jackson/src/test/java/org/baeldung/jackson/dtos/TypeEnumWithCustomSerializer.java new file mode 100644 index 0000000000..479c1ba46e --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/dtos/TypeEnumWithCustomSerializer.java @@ -0,0 +1,35 @@ +package org.baeldung.jackson.dtos; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +@JsonSerialize(using = TypeSerializer.class) +public enum TypeEnumWithCustomSerializer { + TYPE1(1, "Type A"), TYPE2(2, "Type 2"); + + private Integer id; + private String name; + + private TypeEnumWithCustomSerializer(final Integer id, final String name) { + this.id = id; + this.name = name; + } + + // API + + public Integer getId() { + return id; + } + + public void setId(final Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/TypeSerializer.java b/jackson/src/test/java/org/baeldung/jackson/dtos/TypeSerializer.java new file mode 100644 index 0000000000..da1fc4e4a9 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/dtos/TypeSerializer.java @@ -0,0 +1,22 @@ +package org.baeldung.jackson.dtos; + +import java.io.IOException; + +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 TypeSerializer extends JsonSerializer { + + @Override + public void serialize(final TypeEnumWithCustomSerializer value, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException { + generator.writeStartObject(); + generator.writeFieldName("id"); + generator.writeNumber(value.getId()); + generator.writeFieldName("name"); + generator.writeString(value.getName()); + generator.writeEndObject(); + } + +} \ No newline at end of file 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 2c1b9e78fa..1a6014c0e7 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java @@ -12,6 +12,10 @@ 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.MyDtoWithEnum; +import org.baeldung.jackson.dtos.MyDtoWithEnumCustom; +import org.baeldung.jackson.dtos.TypeEnum; +import org.baeldung.jackson.dtos.TypeEnumWithCustomSerializer; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; @@ -21,7 +25,6 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; - public class JacksonSerializationUnitTest { // tests - single entity to json @@ -77,6 +80,44 @@ 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