Skip to content

Commit 134248a

Browse files
author
eugenp
committed
jackson work
1 parent 6f9026a commit 134248a

2 files changed

Lines changed: 79 additions & 60 deletions

File tree

jackson/src/test/java/org/baeldung/jackson/JacksonUnitTest.java renamed to jackson/src/test/java/org/baeldung/jackson/JacksonDeserializationUnitTest.java

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,25 @@
11
package org.baeldung.jackson;
22

3-
import static org.hamcrest.Matchers.containsString;
43
import static org.hamcrest.Matchers.equalTo;
5-
import static org.hamcrest.Matchers.not;
64
import static org.junit.Assert.assertNotNull;
75
import static org.junit.Assert.assertThat;
86

97
import java.io.IOException;
10-
import java.util.List;
118

129
import org.junit.Test;
1310

1411
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;
1515
import com.fasterxml.jackson.databind.DeserializationFeature;
16+
import com.fasterxml.jackson.databind.JsonDeserializer;
1617
import com.fasterxml.jackson.databind.JsonMappingException;
1718
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
1820
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
19-
import com.google.common.collect.Lists;
2021

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 {
7023

7124
// tests - json to single entity
7225

@@ -120,14 +73,14 @@ public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenD
12073
assertThat(readValue.getStringValue(), equalTo("a"));
12174
assertThat(readValue.isBooleanValue(), equalTo(true));
12275
assertThat(readValue.getIntValue(), equalTo(1));
123-
}
12476

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+
}
12684

12785
}
12886

129-
/*
130-
Article Ideas:
131-
- Deserializing with a custom JsonParser
132-
- Jackson Ignore: ignore specific fields at field level, at class level,
133-
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.baeldung.jackson;
2+
3+
import static org.hamcrest.Matchers.containsString;
4+
import static org.hamcrest.Matchers.not;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.io.IOException;
8+
import java.util.List;
9+
10+
import org.junit.Test;
11+
12+
import com.fasterxml.jackson.core.JsonParseException;
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
import com.google.common.collect.Lists;
15+
16+
public class JacksonSerializationUnitTest {
17+
18+
// tests - single entity to json
19+
20+
@Test
21+
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException {
22+
final ObjectMapper mapper = new ObjectMapper();
23+
final String dtoAsString = mapper.writeValueAsString(new FooDto());
24+
25+
assertThat(dtoAsString, not(containsString("intValue")));
26+
System.out.println(dtoAsString);
27+
}
28+
29+
@Test
30+
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException {
31+
final ObjectMapper mapper = new ObjectMapper();
32+
final FooDto dtoObject = new FooDto();
33+
dtoObject.setBooleanValue(true);
34+
35+
final String dtoAsString = mapper.writeValueAsString(dtoObject);
36+
37+
assertThat(dtoAsString, containsString("booleanValue"));
38+
System.out.println(dtoAsString);
39+
}
40+
41+
@Test
42+
public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
43+
final ObjectMapper mapper = new ObjectMapper();
44+
final BarDto dtoObject = new BarDto();
45+
dtoObject.setBooleanValue(true);
46+
47+
final String dtoAsString = mapper.writeValueAsString(dtoObject);
48+
49+
assertThat(dtoAsString, not(containsString("intValue")));
50+
assertThat(dtoAsString, containsString("booleanValue"));
51+
System.out.println(dtoAsString);
52+
}
53+
54+
// tests - multiple entities to json
55+
56+
@Test
57+
public final void whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
58+
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
59+
60+
final ObjectMapper mapper = new ObjectMapper();
61+
final String dtosAsString = mapper.writeValueAsString(listOfDtos);
62+
63+
System.out.println(dtosAsString);
64+
}
65+
66+
}

0 commit comments

Comments
 (0)