Skip to content

Commit 502e405

Browse files
author
eugenp
committed
jackson work
1 parent 5e46274 commit 502e405

3 files changed

Lines changed: 88 additions & 11 deletions

File tree

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

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ public final void whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseE
3636
assertThat(readValue.getStringValue(), equalTo("a"));
3737
}
3838

39+
@Test
40+
public final void givenNotAllFieldsHaveValuesInJson_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
41+
final String jsonAsString = "{\"stringValue\":\"a\",\"booleanValue\":true}";
42+
final ObjectMapper mapper = new ObjectMapper();
43+
44+
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
45+
46+
assertNotNull(readValue);
47+
assertThat(readValue.getStringValue(), equalTo("a"));
48+
}
49+
3950
@Test(expected = UnrecognizedPropertyException.class)
4051
public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException {
4152
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
@@ -85,4 +96,3 @@ public boolean handleUnknownProperty(final DeserializationContext ctxt, final Js
8596
}
8697

8798
}
88-

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99

1010
import org.baeldung.jackson.ignore.MyDto;
11+
import org.baeldung.jackson.ignore.MyDtoFieldNameChanged;
1112
import org.baeldung.jackson.ignore.MyDtoIgnoreField;
1213
import org.baeldung.jackson.ignore.MyDtoIgnoreFieldByName;
1314
import 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

Comments
 (0)