Skip to content

Commit 6f9026a

Browse files
author
eugenp
committed
jackson work
1 parent 88fe235 commit 6f9026a

4 files changed

Lines changed: 243 additions & 8 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.JsonIgnoreProperties;
4+
5+
@JsonIgnoreProperties(value = { "intValue" })
6+
public class BarDto {
7+
8+
private String stringValue;
9+
private int intValue;
10+
private boolean booleanValue;
11+
12+
public BarDto() {
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+
}
Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,133 @@
11
package org.baeldung.jackson;
22

3+
import static org.hamcrest.Matchers.containsString;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.not;
6+
import static org.junit.Assert.assertNotNull;
7+
import static org.junit.Assert.assertThat;
8+
39
import java.io.IOException;
10+
import java.util.List;
411

512
import org.junit.Test;
613

714
import com.fasterxml.jackson.core.JsonParseException;
15+
import com.fasterxml.jackson.databind.DeserializationFeature;
16+
import com.fasterxml.jackson.databind.JsonMappingException;
817
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
19+
import com.google.common.collect.Lists;
920

1021
public class JacksonUnitTest {
1122

12-
// tests
23+
// tests - single entity to json
1324

1425
@Test
15-
public final void whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException {
26+
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException {
1627
final ObjectMapper mapper = new ObjectMapper();
17-
final String fooDtoAsString = mapper.writeValueAsString(new FooDto());
18-
System.out.println(fooDtoAsString);
28+
final String dtoAsString = mapper.writeValueAsString(new FooDto());
29+
30+
assertThat(dtoAsString, not(containsString("intValue")));
31+
System.out.println(dtoAsString);
1932
}
2033

2134
@Test
22-
public final void whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException {
35+
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException {
2336
final ObjectMapper mapper = new ObjectMapper();
2437
final FooDto dtoObject = new FooDto();
2538
dtoObject.setBooleanValue(true);
2639

27-
final String fooDtoAsString = mapper.writeValueAsString(dtoObject);
28-
System.out.println(fooDtoAsString);
40+
final String dtoAsString = mapper.writeValueAsString(dtoObject);
41+
42+
assertThat(dtoAsString, containsString("booleanValue"));
43+
System.out.println(dtoAsString);
44+
}
45+
46+
@Test
47+
public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
48+
final ObjectMapper mapper = new ObjectMapper();
49+
final BarDto dtoObject = new BarDto();
50+
dtoObject.setBooleanValue(true);
51+
52+
final String dtoAsString = mapper.writeValueAsString(dtoObject);
53+
54+
assertThat(dtoAsString, not(containsString("intValue")));
55+
assertThat(dtoAsString, containsString("booleanValue"));
56+
System.out.println(dtoAsString);
57+
}
58+
59+
// tests - multiple entities to json
60+
61+
@Test
62+
public final void whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
63+
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
64+
65+
final ObjectMapper mapper = new ObjectMapper();
66+
final String dtosAsString = mapper.writeValueAsString(listOfDtos);
67+
68+
System.out.println(dtosAsString);
2969
}
3070

71+
// tests - json to single entity
72+
73+
@Test
74+
public final void whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
75+
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true}";
76+
final ObjectMapper mapper = new ObjectMapper();
77+
78+
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
79+
80+
assertNotNull(readValue);
81+
assertThat(readValue.getStringValue(), equalTo("a"));
82+
}
83+
84+
@Test(expected = UnrecognizedPropertyException.class)
85+
public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException {
86+
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
87+
final ObjectMapper mapper = new ObjectMapper();
88+
89+
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
90+
91+
assertNotNull(readValue);
92+
assertThat(readValue.getStringValue(), equalTo("a"));
93+
assertThat(readValue.isBooleanValue(), equalTo(true));
94+
assertThat(readValue.getIntValue(), equalTo(1));
95+
}
96+
97+
@Test
98+
public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
99+
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
100+
final ObjectMapper mapper = new ObjectMapper();
101+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
102+
103+
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
104+
105+
assertNotNull(readValue);
106+
assertThat(readValue.getStringValue(), equalTo("a"));
107+
assertThat(readValue.isBooleanValue(), equalTo(true));
108+
assertThat(readValue.getIntValue(), equalTo(1));
109+
}
110+
111+
@Test
112+
public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
113+
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
114+
final ObjectMapper mapper = new ObjectMapper();
115+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
116+
117+
final MyDtoIgnoreUnkown readValue = mapper.readValue(jsonAsString, MyDtoIgnoreUnkown.class);
118+
119+
assertNotNull(readValue);
120+
assertThat(readValue.getStringValue(), equalTo("a"));
121+
assertThat(readValue.isBooleanValue(), equalTo(true));
122+
assertThat(readValue.getIntValue(), equalTo(1));
123+
}
124+
125+
// tests - json to multiple entities
126+
31127
}
32128

33129
/*
34130
Article Ideas:
35131
- Deserializing with a custom JsonParser
36-
132+
- Jackson Ignore: ignore specific fields at field level, at class level,
37133
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.baeldung.jackson;
2+
3+
public class MyDto {
4+
5+
private String stringValue;
6+
private int intValue;
7+
private boolean booleanValue;
8+
9+
public MyDto() {
10+
super();
11+
}
12+
13+
public MyDto(final String stringValue, final int intValue, final boolean booleanValue) {
14+
super();
15+
16+
this.stringValue = stringValue;
17+
this.intValue = intValue;
18+
this.booleanValue = booleanValue;
19+
}
20+
21+
// API
22+
23+
public String getStringValue() {
24+
return stringValue;
25+
}
26+
27+
public void setStringValue(final String stringValue) {
28+
this.stringValue = stringValue;
29+
}
30+
31+
public int getIntValue() {
32+
return intValue;
33+
}
34+
35+
public void setIntValue(final int intValue) {
36+
this.intValue = intValue;
37+
}
38+
39+
public boolean isBooleanValue() {
40+
return booleanValue;
41+
}
42+
43+
public void setBooleanValue(final boolean booleanValue) {
44+
this.booleanValue = booleanValue;
45+
}
46+
47+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.baeldung.jackson;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
@JsonIgnoreProperties(ignoreUnknown = true)
6+
public class MyDtoIgnoreUnkown {
7+
8+
private String stringValue;
9+
private int intValue;
10+
private boolean booleanValue;
11+
12+
public MyDtoIgnoreUnkown() {
13+
super();
14+
}
15+
16+
public MyDtoIgnoreUnkown(final String stringValue, final int intValue, final boolean booleanValue) {
17+
super();
18+
19+
this.stringValue = stringValue;
20+
this.intValue = intValue;
21+
this.booleanValue = booleanValue;
22+
}
23+
24+
// API
25+
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+
}

0 commit comments

Comments
 (0)