|
1 | 1 | package org.baeldung.jackson.test; |
2 | 2 |
|
| 3 | +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; |
| 4 | +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; |
3 | 5 | import static org.hamcrest.Matchers.containsString; |
4 | 6 | import static org.hamcrest.Matchers.not; |
5 | 7 | import static org.hamcrest.Matchers.notNullValue; |
6 | 8 | import static org.junit.Assert.assertThat; |
7 | 9 |
|
8 | 10 | import java.io.IOException; |
| 11 | +import java.io.StringWriter; |
9 | 12 | import java.util.List; |
10 | 13 |
|
| 14 | +import org.baeldung.jackson.dtos.Item; |
11 | 15 | import org.baeldung.jackson.dtos.MyDto; |
12 | 16 | import org.baeldung.jackson.dtos.MyDtoFieldNameChanged; |
13 | 17 | import org.baeldung.jackson.dtos.MyDtoNoAccessors; |
14 | 18 | import org.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility; |
15 | | -import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnum; |
16 | | -import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom; |
17 | | -import org.baeldung.jackson.dtos.withEnum.TypeEnum; |
18 | | -import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer; |
| 19 | +import org.baeldung.jackson.dtos.User; |
| 20 | +import org.baeldung.jackson.try1.ItemSerializer; |
19 | 21 | import org.junit.Test; |
20 | 22 |
|
21 | 23 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; |
22 | 24 | import com.fasterxml.jackson.annotation.PropertyAccessor; |
| 25 | +import com.fasterxml.jackson.core.JsonGenerationException; |
23 | 26 | import com.fasterxml.jackson.core.JsonParseException; |
| 27 | +import com.fasterxml.jackson.core.Version; |
24 | 28 | import com.fasterxml.jackson.databind.JsonMappingException; |
25 | 29 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
26 | 31 | import com.google.common.collect.Lists; |
27 | 32 |
|
28 | 33 | public class JacksonSerializationUnitTest { |
@@ -82,54 +87,35 @@ public final void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_t |
82 | 87 | assertThat(dtoAsString, containsString("booleanValue")); |
83 | 88 | } |
84 | 89 |
|
85 | | - // tests - enums |
86 | | - |
87 | | - @Test |
88 | | - public final void whenSerializingSimpleEnum_thenCorrect() throws JsonParseException, IOException { |
89 | | - final ObjectMapper mapper = new ObjectMapper(); |
90 | | - final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1); |
91 | | - |
92 | | - System.out.println(dtoAsString); |
93 | | - assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); |
94 | | - } |
95 | | - |
96 | | - @Test |
97 | | - public final void whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { |
98 | | - final ObjectMapper mapper = new ObjectMapper(); |
99 | | - final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnum("a", 1, true, TypeEnum.TYPE1)); |
100 | | - |
101 | | - System.out.println(dtoAsString); |
102 | | - assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); |
103 | | - } |
| 90 | + // tests - multiple entities to json |
104 | 91 |
|
105 | 92 | @Test |
106 | | - public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { |
107 | | - final ObjectMapper mapper = new ObjectMapper(); |
108 | | - final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1)); |
109 | | - |
110 | | - System.out.println(dtoAsString); |
111 | | - assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); |
112 | | - } |
| 93 | + public final void whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { |
| 94 | + final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false)); |
113 | 95 |
|
114 | | - @Test |
115 | | - public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException { |
116 | 96 | final ObjectMapper mapper = new ObjectMapper(); |
117 | | - final String json = mapper.writeValueAsString(new TypeEnum[] { TypeEnum.TYPE1, TypeEnum.TYPE2 }); |
| 97 | + final String dtosAsString = mapper.writeValueAsString(listOfDtos); |
118 | 98 |
|
119 | | - System.out.println(json); |
120 | | - assertThat(json, containsString("\"name\":\"Type A\"")); |
| 99 | + System.out.println(dtosAsString); |
121 | 100 | } |
122 | 101 |
|
123 | | - // tests - multiple entities to json |
| 102 | + // tests - custom serializer |
124 | 103 |
|
125 | 104 | @Test |
126 | | - public final void whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { |
127 | | - final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false)); |
| 105 | + public final void whenSerializingWithCustomSerializer_thenNoExceptions() throws JsonGenerationException, JsonMappingException, IOException { |
| 106 | + final Item myItem = new Item(Integer.parseInt(randomNumeric(2)), randomAlphabetic(8), new User(Integer.parseInt(randomNumeric(2)), randomAlphabetic(8))); |
128 | 107 |
|
129 | 108 | final ObjectMapper mapper = new ObjectMapper(); |
130 | | - final String dtosAsString = mapper.writeValueAsString(listOfDtos); |
131 | 109 |
|
132 | | - System.out.println(dtosAsString); |
| 110 | + final SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null)); |
| 111 | + simpleModule.addSerializer(Item.class, new ItemSerializer()); |
| 112 | + // simpleModule.addSerializer(User.class, new UserSerializer()); |
| 113 | + mapper.registerModule(simpleModule); |
| 114 | + |
| 115 | + final StringWriter writer = new StringWriter(); |
| 116 | + mapper.writeValue(writer, myItem); |
| 117 | + final String serialized = writer.toString(); |
| 118 | + System.out.println(serialized); |
133 | 119 | } |
134 | 120 |
|
135 | 121 | } |
0 commit comments