88import java .util .List ;
99
1010import org .baeldung .jackson .ignore .MyDto ;
11+ import org .baeldung .jackson .ignore .MyDtoFieldNameChanged ;
1112import org .baeldung .jackson .ignore .MyDtoIgnoreField ;
1213import org .baeldung .jackson .ignore .MyDtoIgnoreFieldByName ;
1314import org .baeldung .jackson .ignore .MyDtoIncludeNonDefault ;
@@ -32,7 +33,7 @@ public class JacksonSerializationUnitTest {
3233 // tests - single entity to json
3334
3435 @ Test
35- public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect () throws JsonParseException , IOException {
36+ public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasOnlyDefaultValues_whenSerializing_thenCorrect () throws JsonParseException , IOException {
3637 final ObjectMapper mapper = new ObjectMapper ();
3738 final String dtoAsString = mapper .writeValueAsString (new MyDtoIncludeNonDefault ());
3839
@@ -41,7 +42,7 @@ public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultVa
4142 }
4243
4344 @ Test
44- public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect () throws JsonParseException , IOException {
45+ public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasNonDefaultValue_whenSerializing_thenCorrect () throws JsonParseException , IOException {
4546 final ObjectMapper mapper = new ObjectMapper ();
4647 final MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault ();
4748 dtoObject .setBooleanValue (true );
@@ -52,6 +53,21 @@ public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultVal
5253 System .out .println (dtoAsString );
5354 }
5455
56+ @ Test
57+ public final void givenNameOfFieldIsChanged_whenSerializing_thenCorrect () throws JsonParseException , IOException {
58+ final ObjectMapper mapper = new ObjectMapper ();
59+ final MyDtoFieldNameChanged dtoObject = new MyDtoFieldNameChanged ();
60+ dtoObject .setStringValue ("a" );
61+
62+ final String dtoAsString = mapper .writeValueAsString (dtoObject );
63+
64+ assertThat (dtoAsString , not (containsString ("stringValue" )));
65+ assertThat (dtoAsString , containsString ("strVal" ));
66+ System .out .println (dtoAsString );
67+ }
68+
69+ // ignore
70+
5571 @ Test
5672 public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect () throws JsonParseException , IOException {
5773 final ObjectMapper mapper = new ObjectMapper ();
@@ -110,17 +126,18 @@ public final void givenTypeHasFilterThatIgnoresFieldByName_whenDtoIsSerialized_t
110126 }
111127
112128 @ Test
113- public final void givenTypeHasFilterThatIgnoresIntsOver10_whenDtoIsSerialized_thenCorrect () throws JsonParseException , IOException {
129+ public final void givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect () throws JsonParseException , IOException {
114130 final PropertyFilter theFilter = new SimpleBeanPropertyFilter () {
115131 @ Override
116132 public final void serializeAsField (final Object pojo , final JsonGenerator jgen , final SerializerProvider provider , final PropertyWriter writer ) throws Exception {
117133 if (include (writer )) {
118- if (writer .getName ().equals ("intValue" )) {
119- final int intValue = ((MyDtoWithFilter ) pojo ).getIntValue ();
120- if (intValue < 10 ) {
121- writer .serializeAsField (pojo , jgen , provider );
122- }
123- } else {
134+ if (!writer .getName ().equals ("intValue" )) {
135+ writer .serializeAsField (pojo , jgen , provider );
136+ return ;
137+ }
138+
139+ final int intValue = ((MyDtoWithFilter ) pojo ).getIntValue ();
140+ if (intValue >= 0 ) {
124141 writer .serializeAsField (pojo , jgen , provider );
125142 }
126143 } else if (!jgen .canOmitFields ()) { // since 2.3
@@ -141,7 +158,7 @@ protected final boolean include(final PropertyWriter writer) {
141158 final FilterProvider filters = new SimpleFilterProvider ().addFilter ("myFilter" , theFilter );
142159
143160 final MyDtoWithFilter dtoObject = new MyDtoWithFilter ();
144- dtoObject .setIntValue (12 );
161+ dtoObject .setIntValue (- 1 );
145162
146163 final ObjectMapper mapper = new ObjectMapper ();
147164 final String dtoAsString = mapper .writer (filters ).writeValueAsString (dtoObject );
0 commit comments