|
9 | 9 | import java.io.IOException; |
10 | 10 |
|
11 | 11 | import org.baeldung.jackson.field.MyDtoAccessLevel; |
12 | | -import org.baeldung.jackson.field.MyDtoCustomizedPropertyName; |
13 | 12 | import org.baeldung.jackson.field.MyDtoGetter; |
14 | 13 | import org.baeldung.jackson.field.MyDtoGetterImplicitDeserialization; |
15 | 14 | import org.baeldung.jackson.field.MyDtoSetter; |
16 | | -import org.baeldung.jackson.field.User; |
17 | 15 | import org.junit.Test; |
18 | 16 |
|
19 | | -import com.fasterxml.jackson.core.JsonParseException; |
| 17 | +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; |
| 18 | +import com.fasterxml.jackson.annotation.PropertyAccessor; |
20 | 19 | import com.fasterxml.jackson.core.JsonProcessingException; |
21 | 20 | import com.fasterxml.jackson.databind.JsonMappingException; |
22 | 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
@@ -89,59 +88,18 @@ public final void givenDifferentAccessLevels_whenSetterAdded_thenStillNotSeriali |
89 | 88 | } |
90 | 89 |
|
91 | 90 | @Test |
92 | | - public final void givenCustomizedPropertyName_whenFieldAnnotated_thenPropertyNameCustomizedOnSerialization() throws JsonProcessingException, JsonMappingException, IOException { |
| 91 | + public final void givenDifferentAccessLevels_whenSetVisibility_thenSerializable() throws JsonProcessingException, JsonMappingException, IOException { |
93 | 92 | final ObjectMapper mapper = new ObjectMapper(); |
| 93 | + mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); |
| 94 | + mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); |
94 | 95 |
|
95 | | - final MyDtoCustomizedPropertyName dtoObject = new MyDtoCustomizedPropertyName(); |
| 96 | + final MyDtoAccessLevel dtoObject = new MyDtoAccessLevel(); |
96 | 97 |
|
97 | 98 | final String dtoAsString = mapper.writeValueAsString(dtoObject); |
98 | 99 | assertThat(dtoAsString, containsString("stringValue")); |
99 | 100 | assertThat(dtoAsString, containsString("intValue")); |
100 | | - assertThat(dtoAsString, containsString("BOOLEANVALUE")); |
101 | | - assertThat(dtoAsString, not(containsString("booleanValue"))); |
| 101 | + assertThat(dtoAsString, containsString("booleanValue")); |
102 | 102 | System.out.println(dtoAsString); |
103 | 103 | } |
104 | 104 |
|
105 | | - @Test |
106 | | - public final void givenCustomizedPropertyName_whenFieldAnnotated_thenPropertyNameCustomizedOnDeserialization() throws JsonProcessingException, JsonMappingException, IOException { |
107 | | - final String jsonAsString = "{\"stringValue\":\"dtoString\",\"intValue\":1,\"BOOLEANVALUE\":\"true\"}"; |
108 | | - final ObjectMapper mapper = new ObjectMapper(); |
109 | | - |
110 | | - final MyDtoCustomizedPropertyName dtoObject = mapper.readValue(jsonAsString, MyDtoCustomizedPropertyName.class); |
111 | | - |
112 | | - assertNotNull(dtoObject); |
113 | | - assertThat(dtoObject.getStringValue(), equalTo("dtoString")); |
114 | | - assertThat(dtoObject.getIntValue(), equalTo(1)); |
115 | | - assertThat(dtoObject.getBooleanValue(), equalTo(true)); |
116 | | - } |
117 | | - |
118 | | - @Test |
119 | | - public final void givenFieldIsIgnoredOnlyAtSerialization_whenUserIsSerialized_thenIgnored() throws JsonProcessingException { |
120 | | - final ObjectMapper mapper = new ObjectMapper(); |
121 | | - |
122 | | - final User userObject = new User(); |
123 | | - userObject.setId(1); |
124 | | - userObject.setName("theUser"); |
125 | | - userObject.setPassword("thePassword"); |
126 | | - |
127 | | - final String userAsString = mapper.writeValueAsString(userObject); |
128 | | - assertThat(userAsString, containsString("id")); |
129 | | - assertThat(userAsString, containsString("name")); |
130 | | - assertThat(userAsString, not(containsString("password"))); |
131 | | - System.out.println(userAsString); |
132 | | - } |
133 | | - |
134 | | - @Test |
135 | | - public final void givenFieldIsIgnoredOnlyAtSerialization_whenUserIsDeserialized_thenCorrect() throws JsonParseException, JsonMappingException, IOException { |
136 | | - final String jsonAsString = "{\"id\":1,\"name\":\"theUser\",\"password\":\"thePassword\"}"; |
137 | | - final ObjectMapper mapper = new ObjectMapper(); |
138 | | - |
139 | | - final User userObject = mapper.readValue(jsonAsString, User.class); |
140 | | - |
141 | | - assertNotNull(userObject); |
142 | | - assertThat(userObject.getId(), equalTo(1)); |
143 | | - assertThat(userObject.getName(), equalTo("theUser")); |
144 | | - assertThat(userObject.getPassword(), equalTo("thePassword")); |
145 | | - } |
146 | | - |
147 | 105 | } |
0 commit comments