Skip to content

Commit e088098

Browse files
author
eugenp
committed
further jackson work
1 parent 62e0386 commit e088098

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.baeldung.jackson;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
5+
public class MyDtoIgnoreField {
6+
7+
private String stringValue;
8+
@JsonIgnore
9+
private int intValue;
10+
private boolean booleanValue;
11+
12+
public MyDtoIgnoreField() {
13+
super();
14+
}
15+
16+
// API
17+
18+
public String getStringValue() {
19+
return stringValue;
20+
}
21+
22+
public void setStringValue(final String stringValue) {
23+
this.stringValue = stringValue;
24+
}
25+
26+
public int getIntValue() {
27+
return intValue;
28+
}
29+
30+
public void setIntValue(final int intValue) {
31+
this.intValue = intValue;
32+
}
33+
34+
public boolean isBooleanValue() {
35+
return booleanValue;
36+
}
37+
38+
public void setBooleanValue(final boolean booleanValue) {
39+
this.booleanValue = booleanValue;
40+
}
41+
42+
}

jackson/src/test/java/org/baeldung/jackson/MyBooleanMixIn.java renamed to jackson/src/test/java/org/baeldung/jackson/MyMixInForString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import com.fasterxml.jackson.annotation.JsonIgnoreType;
44

55
@JsonIgnoreType
6-
public class MyBooleanMixIn {
6+
public class MyMixInForString {
77
//
88
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import java.io.IOException;
88
import java.util.List;
99

10-
import org.baeldung.jackson.MyBooleanMixIn;
10+
import org.baeldung.jackson.MyMixInForString;
1111
import org.baeldung.jackson.MyDto;
12+
import org.baeldung.jackson.MyDtoIgnoreField;
1213
import org.baeldung.jackson.MyDtoIgnoreFieldByName;
1314
import org.baeldung.jackson.MyDtoIncludeNonDefault;
1415
import org.junit.Test;
@@ -43,7 +44,7 @@ public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultVal
4344
}
4445

4546
@Test
46-
public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
47+
public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
4748
final ObjectMapper mapper = new ObjectMapper();
4849
final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName();
4950
dtoObject.setBooleanValue(true);
@@ -55,10 +56,22 @@ public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws J
5556
System.out.println(dtoAsString);
5657
}
5758

59+
@Test
60+
public final void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
61+
final ObjectMapper mapper = new ObjectMapper();
62+
final MyDtoIgnoreField dtoObject = new MyDtoIgnoreField();
63+
64+
final String dtoAsString = mapper.writeValueAsString(dtoObject);
65+
66+
assertThat(dtoAsString, not(containsString("intValue")));
67+
assertThat(dtoAsString, containsString("booleanValue"));
68+
System.out.println(dtoAsString);
69+
}
70+
5871
@Test
5972
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
6073
final ObjectMapper mapper = new ObjectMapper();
61-
mapper.addMixInAnnotations(String.class, MyBooleanMixIn.class);
74+
mapper.addMixInAnnotations(String.class, MyMixInForString.class);
6275
final MyDto dtoObject = new MyDto();
6376
dtoObject.setBooleanValue(true);
6477

0 commit comments

Comments
 (0)