Skip to content

Commit b8fbbc3

Browse files
author
Eugen
committed
Merge pull request eugenp#27 from rachelshu/master
Updated the tutorial examples: remove change name example, and
2 parents 979cd6e + 15123d8 commit b8fbbc3

File tree

3 files changed

+7
-149
lines changed

3 files changed

+7
-149
lines changed

jackson/src/test/java/org/baeldung/jackson/field/MyDtoCustomizedPropertyName.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

jackson/src/test/java/org/baeldung/jackson/field/User.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
import java.io.IOException;
1010

1111
import org.baeldung.jackson.field.MyDtoAccessLevel;
12-
import org.baeldung.jackson.field.MyDtoCustomizedPropertyName;
1312
import org.baeldung.jackson.field.MyDtoGetter;
1413
import org.baeldung.jackson.field.MyDtoGetterImplicitDeserialization;
1514
import org.baeldung.jackson.field.MyDtoSetter;
16-
import org.baeldung.jackson.field.User;
1715
import org.junit.Test;
1816

19-
import com.fasterxml.jackson.core.JsonParseException;
17+
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
18+
import com.fasterxml.jackson.annotation.PropertyAccessor;
2019
import com.fasterxml.jackson.core.JsonProcessingException;
2120
import com.fasterxml.jackson.databind.JsonMappingException;
2221
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -89,59 +88,18 @@ public final void givenDifferentAccessLevels_whenSetterAdded_thenStillNotSeriali
8988
}
9089

9190
@Test
92-
public final void givenCustomizedPropertyName_whenFieldAnnotated_thenPropertyNameCustomizedOnSerialization() throws JsonProcessingException, JsonMappingException, IOException {
91+
public final void givenDifferentAccessLevels_whenSetVisibility_thenSerializable() throws JsonProcessingException, JsonMappingException, IOException {
9392
final ObjectMapper mapper = new ObjectMapper();
93+
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
94+
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
9495

95-
final MyDtoCustomizedPropertyName dtoObject = new MyDtoCustomizedPropertyName();
96+
final MyDtoAccessLevel dtoObject = new MyDtoAccessLevel();
9697

9798
final String dtoAsString = mapper.writeValueAsString(dtoObject);
9899
assertThat(dtoAsString, containsString("stringValue"));
99100
assertThat(dtoAsString, containsString("intValue"));
100-
assertThat(dtoAsString, containsString("BOOLEANVALUE"));
101-
assertThat(dtoAsString, not(containsString("booleanValue")));
101+
assertThat(dtoAsString, containsString("booleanValue"));
102102
System.out.println(dtoAsString);
103103
}
104104

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-
147105
}

0 commit comments

Comments
 (0)