enum work

This commit is contained in:
eugenp 2013-12-24 18:37:39 +02:00
parent b76928dcf5
commit ee37eb8a31
6 changed files with 248 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<TypeEnumWithCustomSerializer> {
@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();
}
}

View File

@ -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