Skip to content

Commit ee37eb8

Browse files
author
eugenp
committed
enum work
1 parent b76928d commit ee37eb8

6 files changed

Lines changed: 248 additions & 1 deletion

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.baeldung.jackson.dtos;
2+
3+
public class MyDtoWithEnum {
4+
5+
private String stringValue;
6+
private int intValue;
7+
private boolean booleanValue;
8+
private TypeEnum type;
9+
10+
public MyDtoWithEnum() {
11+
super();
12+
}
13+
14+
public MyDtoWithEnum(final String stringValue, final int intValue, final boolean booleanValue, final TypeEnum type) {
15+
super();
16+
17+
this.stringValue = stringValue;
18+
this.intValue = intValue;
19+
this.booleanValue = booleanValue;
20+
this.type = type;
21+
}
22+
23+
// API
24+
25+
public String getStringValue() {
26+
return stringValue;
27+
}
28+
29+
public void setStringValue(final String stringValue) {
30+
this.stringValue = stringValue;
31+
}
32+
33+
public int getIntValue() {
34+
return intValue;
35+
}
36+
37+
public void setIntValue(final int intValue) {
38+
this.intValue = intValue;
39+
}
40+
41+
public boolean isBooleanValue() {
42+
return booleanValue;
43+
}
44+
45+
public void setBooleanValue(final boolean booleanValue) {
46+
this.booleanValue = booleanValue;
47+
}
48+
49+
public TypeEnum getType() {
50+
return type;
51+
}
52+
53+
public void setType(final TypeEnum type) {
54+
this.type = type;
55+
}
56+
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.baeldung.jackson.dtos;
2+
3+
public class MyDtoWithEnumCustom {
4+
5+
private String stringValue;
6+
private int intValue;
7+
private boolean booleanValue;
8+
private TypeEnumWithCustomSerializer type;
9+
10+
public MyDtoWithEnumCustom() {
11+
super();
12+
}
13+
14+
public MyDtoWithEnumCustom(final String stringValue, final int intValue, final boolean booleanValue, final TypeEnumWithCustomSerializer type) {
15+
super();
16+
17+
this.stringValue = stringValue;
18+
this.intValue = intValue;
19+
this.booleanValue = booleanValue;
20+
this.type = type;
21+
}
22+
23+
// API
24+
25+
public String getStringValue() {
26+
return stringValue;
27+
}
28+
29+
public void setStringValue(final String stringValue) {
30+
this.stringValue = stringValue;
31+
}
32+
33+
public int getIntValue() {
34+
return intValue;
35+
}
36+
37+
public void setIntValue(final int intValue) {
38+
this.intValue = intValue;
39+
}
40+
41+
public boolean isBooleanValue() {
42+
return booleanValue;
43+
}
44+
45+
public void setBooleanValue(final boolean booleanValue) {
46+
this.booleanValue = booleanValue;
47+
}
48+
49+
public TypeEnumWithCustomSerializer getType() {
50+
return type;
51+
}
52+
53+
public void setType(final TypeEnumWithCustomSerializer type) {
54+
this.type = type;
55+
}
56+
57+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.baeldung.jackson.dtos;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
5+
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
6+
public enum TypeEnum {
7+
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
8+
9+
private Integer id;
10+
private String name;
11+
12+
private TypeEnum(final Integer id, final String name) {
13+
this.id = id;
14+
this.name = name;
15+
}
16+
17+
// API
18+
19+
public Integer getId() {
20+
return id;
21+
}
22+
23+
public void setId(final Integer id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(final String name) {
32+
this.name = name;
33+
}
34+
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.baeldung.jackson.dtos;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
4+
5+
@JsonSerialize(using = TypeSerializer.class)
6+
public enum TypeEnumWithCustomSerializer {
7+
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
8+
9+
private Integer id;
10+
private String name;
11+
12+
private TypeEnumWithCustomSerializer(final Integer id, final String name) {
13+
this.id = id;
14+
this.name = name;
15+
}
16+
17+
// API
18+
19+
public Integer getId() {
20+
return id;
21+
}
22+
23+
public void setId(final Integer id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(final String name) {
32+
this.name = name;
33+
}
34+
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.jackson.dtos;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.core.JsonGenerator;
6+
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.databind.JsonSerializer;
8+
import com.fasterxml.jackson.databind.SerializerProvider;
9+
10+
public class TypeSerializer extends JsonSerializer<TypeEnumWithCustomSerializer> {
11+
12+
@Override
13+
public void serialize(final TypeEnumWithCustomSerializer value, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException {
14+
generator.writeStartObject();
15+
generator.writeFieldName("id");
16+
generator.writeNumber(value.getId());
17+
generator.writeFieldName("name");
18+
generator.writeString(value.getName());
19+
generator.writeEndObject();
20+
}
21+
22+
}

‎jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import org.baeldung.jackson.dtos.MyDtoFieldNameChanged;
1313
import org.baeldung.jackson.dtos.MyDtoNoAccessors;
1414
import org.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility;
15+
import org.baeldung.jackson.dtos.MyDtoWithEnum;
16+
import org.baeldung.jackson.dtos.MyDtoWithEnumCustom;
17+
import org.baeldung.jackson.dtos.TypeEnum;
18+
import org.baeldung.jackson.dtos.TypeEnumWithCustomSerializer;
1519
import org.junit.Test;
1620

1721
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
@@ -21,7 +25,6 @@
2125
import com.fasterxml.jackson.databind.ObjectMapper;
2226
import com.google.common.collect.Lists;
2327

24-
2528
public class JacksonSerializationUnitTest {
2629

2730
// tests - single entity to json
@@ -77,6 +80,44 @@ public final void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_t
7780
assertThat(dtoAsString, containsString("booleanValue"));
7881
}
7982

83+
// tests - enums
84+
85+
@Test
86+
public final void whenSerializingSimpleEnum_thenCorrect() throws JsonParseException, IOException {
87+
final ObjectMapper mapper = new ObjectMapper();
88+
final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1);
89+
90+
System.out.println(dtoAsString);
91+
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
92+
}
93+
94+
@Test
95+
public final void whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException {
96+
final ObjectMapper mapper = new ObjectMapper();
97+
final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnum("a", 1, true, TypeEnum.TYPE1));
98+
99+
System.out.println(dtoAsString);
100+
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
101+
}
102+
103+
@Test
104+
public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException {
105+
final ObjectMapper mapper = new ObjectMapper();
106+
final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1));
107+
108+
System.out.println(dtoAsString);
109+
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
110+
}
111+
112+
@Test
113+
public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException {
114+
final ObjectMapper mapper = new ObjectMapper();
115+
final String json = mapper.writeValueAsString(new TypeEnum[] { TypeEnum.TYPE1, TypeEnum.TYPE2 });
116+
117+
System.out.println(json);
118+
assertThat(json, containsString("\"name\":\"Type A\""));
119+
}
120+
80121
// tests - multiple entities to json
81122

82123
@Test

0 commit comments

Comments
 (0)