|
1 | 1 | package org.baeldung.jackson; |
2 | 2 |
|
3 | | -import static org.hamcrest.Matchers.containsString; |
4 | 3 | import static org.hamcrest.Matchers.equalTo; |
5 | | -import static org.hamcrest.Matchers.not; |
6 | 4 | import static org.junit.Assert.assertNotNull; |
7 | 5 | import static org.junit.Assert.assertThat; |
8 | 6 |
|
9 | 7 | import java.io.IOException; |
10 | | -import java.util.List; |
11 | 8 |
|
12 | 9 | import org.junit.Test; |
13 | 10 |
|
14 | 11 | import com.fasterxml.jackson.core.JsonParseException; |
| 12 | +import com.fasterxml.jackson.core.JsonParser; |
| 13 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 14 | +import com.fasterxml.jackson.databind.DeserializationContext; |
15 | 15 | import com.fasterxml.jackson.databind.DeserializationFeature; |
| 16 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
16 | 17 | import com.fasterxml.jackson.databind.JsonMappingException; |
17 | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; |
18 | 20 | import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
19 | | -import com.google.common.collect.Lists; |
20 | 21 |
|
21 | | -public class JacksonUnitTest { |
22 | | - |
23 | | - // tests - single entity to json |
24 | | - |
25 | | - @Test |
26 | | - public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException { |
27 | | - final ObjectMapper mapper = new ObjectMapper(); |
28 | | - final String dtoAsString = mapper.writeValueAsString(new FooDto()); |
29 | | - |
30 | | - assertThat(dtoAsString, not(containsString("intValue"))); |
31 | | - System.out.println(dtoAsString); |
32 | | - } |
33 | | - |
34 | | - @Test |
35 | | - public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException { |
36 | | - final ObjectMapper mapper = new ObjectMapper(); |
37 | | - final FooDto dtoObject = new FooDto(); |
38 | | - dtoObject.setBooleanValue(true); |
39 | | - |
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); |
69 | | - } |
| 22 | +public class JacksonDeserializationUnitTest { |
70 | 23 |
|
71 | 24 | // tests - json to single entity |
72 | 25 |
|
@@ -120,14 +73,14 @@ public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenD |
120 | 73 | assertThat(readValue.getStringValue(), equalTo("a")); |
121 | 74 | assertThat(readValue.isBooleanValue(), equalTo(true)); |
122 | 75 | assertThat(readValue.getIntValue(), equalTo(1)); |
123 | | - } |
124 | 76 |
|
125 | | - // tests - json to multiple entities |
| 77 | + mapper.addHandler(new DeserializationProblemHandler() { |
| 78 | + @Override |
| 79 | + public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser jp, final JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName) throws IOException, JsonProcessingException { |
| 80 | + return super.handleUnknownProperty(ctxt, jp, deserializer, beanOrClass, propertyName); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
126 | 84 |
|
127 | 85 | } |
128 | 86 |
|
129 | | -/* |
130 | | -Article Ideas: |
131 | | -- Deserializing with a custom JsonParser |
132 | | -- Jackson Ignore: ignore specific fields at field level, at class level, |
133 | | - */ |
0 commit comments