Skip to content

Commit da58ba4

Browse files
committed
upgrade jackson
1 parent 73a89d9 commit da58ba4

8 files changed

Lines changed: 117 additions & 61 deletions
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.jackson.dtos;
2+
3+
public class MyDtoWithSpecialField {
4+
5+
private String[] stringValue;
6+
private int intValue;
7+
private boolean booleanValue;
8+
9+
public MyDtoWithSpecialField() {
10+
super();
11+
}
12+
13+
public MyDtoWithSpecialField(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+
//
48+
49+
@Override
50+
public String toString() {
51+
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
52+
}
53+
54+
}

jackson/src/test/java/com/baeldung/jackson/dtos/MyMixInForString.java renamed to jackson/src/test/java/com/baeldung/jackson/dtos/MyMixInForIgnoreType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import com.fasterxml.jackson.annotation.JsonIgnoreType;
44

55
@JsonIgnoreType
6-
public class MyMixInForString {
6+
public class MyMixInForIgnoreType {
77
//
88
}

‎jackson/src/test/java/com/baeldung/jackson/test/JacksonAnnotationTest.java‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
import java.util.Date;
1313
import java.util.TimeZone;
1414

15-
import com.baeldung.jackson.bidirection.ItemWithIdentity;
16-
import com.baeldung.jackson.bidirection.ItemWithRef;
17-
import com.baeldung.jackson.bidirection.UserWithRef;
18-
import com.baeldung.jackson.dtos.User;
19-
import com.baeldung.jackson.dtos.withEnum.TypeEnumWithValue;
15+
import org.junit.Test;
16+
2017
import com.baeldung.jackson.annotation.BeanWithCreator;
2118
import com.baeldung.jackson.annotation.BeanWithCustomAnnotation;
2219
import com.baeldung.jackson.annotation.BeanWithFilter;
@@ -30,16 +27,17 @@
3027
import com.baeldung.jackson.annotation.UnwrappedUser;
3128
import com.baeldung.jackson.annotation.UserWithIgnoreType;
3229
import com.baeldung.jackson.annotation.Zoo;
30+
import com.baeldung.jackson.bidirection.ItemWithIdentity;
31+
import com.baeldung.jackson.bidirection.ItemWithRef;
3332
import com.baeldung.jackson.bidirection.UserWithIdentity;
33+
import com.baeldung.jackson.bidirection.UserWithRef;
3434
import com.baeldung.jackson.date.EventWithFormat;
3535
import com.baeldung.jackson.date.EventWithSerializer;
36-
import com.baeldung.jackson.dtos.MyMixInForString;
36+
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
37+
import com.baeldung.jackson.dtos.withEnum.TypeEnumWithValue;
3738
import com.baeldung.jackson.exception.UserWithRoot;
3839
import com.baeldung.jackson.jsonview.Item;
3940
import com.baeldung.jackson.jsonview.Views;
40-
import org.junit.Ignore;
41-
import org.junit.Test;
42-
4341
import com.fasterxml.jackson.core.JsonParseException;
4442
import com.fasterxml.jackson.core.JsonProcessingException;
4543
import com.fasterxml.jackson.databind.InjectableValues;
@@ -127,7 +125,7 @@ public void whenSerializingUsingJsonSerialize_thenCorrect() throws JsonProcessin
127125
public void whenDeserializingUsingJsonCreator_thenCorrect() throws JsonProcessingException, IOException {
128126
final String json = "{\"id\":1,\"theName\":\"My bean\"}";
129127

130-
final BeanWithCreator bean = new ObjectMapper().reader(BeanWithCreator.class).readValue(json);
128+
final BeanWithCreator bean = new ObjectMapper().readerFor(BeanWithCreator.class).readValue(json);
131129
assertEquals("My bean", bean.name);
132130
}
133131

@@ -136,7 +134,7 @@ public void whenDeserializingUsingJsonInject_thenCorrect() throws JsonProcessing
136134
final String json = "{\"name\":\"My bean\"}";
137135
final InjectableValues inject = new InjectableValues.Std().addValue(int.class, 1);
138136

139-
final BeanWithInject bean = new ObjectMapper().reader(inject).withType(BeanWithInject.class).readValue(json);
137+
final BeanWithInject bean = new ObjectMapper().reader(inject).forType(BeanWithInject.class).readValue(json);
140138
assertEquals("My bean", bean.name);
141139
assertEquals(1, bean.id);
142140
}
@@ -145,7 +143,7 @@ public void whenDeserializingUsingJsonInject_thenCorrect() throws JsonProcessing
145143
public void whenDeserializingUsingJsonAnySetter_thenCorrect() throws JsonProcessingException, IOException {
146144
final String json = "{\"name\":\"My bean\",\"attr2\":\"val2\",\"attr1\":\"val1\"}";
147145

148-
final ExtendableBean bean = new ObjectMapper().reader(ExtendableBean.class).readValue(json);
146+
final ExtendableBean bean = new ObjectMapper().readerFor(ExtendableBean.class).readValue(json);
149147
assertEquals("My bean", bean.name);
150148
assertEquals("val2", bean.getProperties().get("attr2"));
151149
}
@@ -154,7 +152,7 @@ public void whenDeserializingUsingJsonAnySetter_thenCorrect() throws JsonProcess
154152
public void whenDeserializingUsingJsonSetter_thenCorrect() throws JsonProcessingException, IOException {
155153
final String json = "{\"id\":1,\"name\":\"My bean\"}";
156154

157-
final BeanWithGetter bean = new ObjectMapper().reader(BeanWithGetter.class).readValue(json);
155+
final BeanWithGetter bean = new ObjectMapper().readerFor(BeanWithGetter.class).readValue(json);
158156
assertEquals("My bean", bean.getTheName());
159157
}
160158

@@ -164,7 +162,7 @@ public void whenDeserializingUsingJsonDeserialize_thenCorrect() throws JsonProce
164162

165163
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
166164

167-
final EventWithSerializer event = new ObjectMapper().reader(EventWithSerializer.class).readValue(json);
165+
final EventWithSerializer event = new ObjectMapper().readerFor(EventWithSerializer.class).readValue(json);
168166
assertEquals("20-12-2014 02:30:00", df.format(event.eventDate));
169167
}
170168

@@ -235,7 +233,7 @@ public void whenSerializingPolymorphic_thenCorrect() throws JsonProcessingExcept
235233
public void whenDeserializingPolymorphic_thenCorrect() throws JsonProcessingException, IOException {
236234
final String json = "{\"animal\":{\"name\":\"lacy\",\"type\":\"cat\"}}";
237235

238-
final Zoo zoo = new ObjectMapper().reader().withType(Zoo.class).readValue(json);
236+
final Zoo zoo = new ObjectMapper().readerFor(Zoo.class).readValue(json);
239237

240238
assertEquals("lacy", zoo.animal.name);
241239
assertEquals(Zoo.Cat.class, zoo.animal.getClass());
@@ -250,7 +248,7 @@ public void whenUsingJsonProperty_thenCorrect() throws IOException {
250248
assertThat(result, containsString("My bean"));
251249
assertThat(result, containsString("1"));
252250

253-
final BeanWithGetter resultBean = new ObjectMapper().reader(BeanWithGetter.class).readValue(result);
251+
final BeanWithGetter resultBean = new ObjectMapper().readerFor(BeanWithGetter.class).readValue(result);
254252
assertEquals("My bean", resultBean.getTheName());
255253
}
256254

@@ -338,19 +336,19 @@ public void whenSerializingUsingCustomAnnotation_thenCorrect() throws JsonProces
338336
assertThat(result, not(containsString("dateCreated")));
339337
}
340338

341-
@Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins")
339+
// @Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins")
342340
@Test
343341
public void whenSerializingUsingMixInAnnotation_thenCorrect() throws JsonProcessingException {
344-
final User user = new User(1, "John");
342+
final com.baeldung.jackson.dtos.Item item = new com.baeldung.jackson.dtos.Item(1, "book", null);
345343

346-
String result = new ObjectMapper().writeValueAsString(user);
347-
assertThat(result, containsString("John"));
344+
String result = new ObjectMapper().writeValueAsString(item);
345+
assertThat(result, containsString("owner"));
348346

349347
final ObjectMapper mapper = new ObjectMapper();
350-
mapper.addMixIn(String.class, MyMixInForString.class);
348+
mapper.addMixIn(com.baeldung.jackson.dtos.User.class, MyMixInForIgnoreType.class);
351349

352-
result = mapper.writeValueAsString(user);
353-
assertThat(result, not(containsString("John")));
350+
result = mapper.writeValueAsString(item);
351+
assertThat(result, not(containsString("owner")));
354352
}
355353

356354
@Test

‎jackson/src/test/java/com/baeldung/jackson/test/JacksonBidirectionRelationTest.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import java.io.IOException;
99

10+
import org.junit.Test;
11+
1012
import com.baeldung.jackson.bidirection.Item;
1113
import com.baeldung.jackson.bidirection.ItemWithIdentity;
1214
import com.baeldung.jackson.bidirection.ItemWithIgnore;
@@ -20,8 +22,6 @@
2022
import com.baeldung.jackson.bidirection.UserWithSerializer;
2123
import com.baeldung.jackson.bidirection.UserWithView;
2224
import com.baeldung.jackson.jsonview.Views;
23-
import org.junit.Test;
24-
2525
import com.fasterxml.jackson.core.JsonProcessingException;
2626
import com.fasterxml.jackson.databind.JsonMappingException;
2727
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -93,7 +93,7 @@ public void givenBidirectionRelation_whenUsingCustomSerializer_thenCorrect() thr
9393
public void givenBidirectionRelation_whenDeserializingUsingIdentity_thenCorrect() throws JsonProcessingException, IOException {
9494
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
9595

96-
final ItemWithIdentity item = new ObjectMapper().reader(ItemWithIdentity.class).readValue(json);
96+
final ItemWithIdentity item = new ObjectMapper().readerFor(ItemWithIdentity.class).readValue(json);
9797

9898
assertEquals(2, item.id);
9999
assertEquals("book", item.itemName);
@@ -104,7 +104,7 @@ public void givenBidirectionRelation_whenDeserializingUsingIdentity_thenCorrect(
104104
public void givenBidirectionRelation_whenUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException {
105105
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
106106

107-
final ItemWithSerializer item = new ObjectMapper().reader(ItemWithSerializer.class).readValue(json);
107+
final ItemWithSerializer item = new ObjectMapper().readerFor(ItemWithSerializer.class).readValue(json);
108108
assertEquals(2, item.id);
109109
assertEquals("book", item.itemName);
110110
assertEquals("John", item.owner.name);

‎jackson/src/test/java/com/baeldung/jackson/test/JacksonDateTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
import java.util.Date;
1212
import java.util.TimeZone;
1313

14-
import com.baeldung.jackson.date.EventWithLocalDateTime;
15-
import com.baeldung.jackson.date.Event;
16-
import com.baeldung.jackson.date.EventWithFormat;
17-
import com.baeldung.jackson.date.EventWithJodaTime;
18-
import com.baeldung.jackson.date.EventWithSerializer;
1914
import org.joda.time.DateTime;
2015
import org.joda.time.DateTimeZone;
2116
import org.junit.Test;
2217

18+
import com.baeldung.jackson.date.Event;
19+
import com.baeldung.jackson.date.EventWithFormat;
20+
import com.baeldung.jackson.date.EventWithJodaTime;
21+
import com.baeldung.jackson.date.EventWithLocalDateTime;
22+
import com.baeldung.jackson.date.EventWithSerializer;
2323
import com.fasterxml.jackson.core.JsonProcessingException;
2424
import com.fasterxml.jackson.databind.ObjectMapper;
2525
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -128,7 +128,7 @@ public void whenDeserializingDateWithJackson_thenCorrect() throws JsonProcessing
128128
final ObjectMapper mapper = new ObjectMapper();
129129
mapper.setDateFormat(df);
130130

131-
final Event event = mapper.reader(Event.class).readValue(json);
131+
final Event event = mapper.readerFor(Event.class).readValue(json);
132132
assertEquals("20-12-2014 02:30:00", df.format(event.eventDate));
133133
}
134134

@@ -139,7 +139,7 @@ public void whenDeserializingDateUsingCustomDeserializer_thenCorrect() throws Js
139139
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
140140
final ObjectMapper mapper = new ObjectMapper();
141141

142-
final EventWithSerializer event = mapper.reader(EventWithSerializer.class).readValue(json);
142+
final EventWithSerializer event = mapper.readerFor(EventWithSerializer.class).readValue(json);
143143
assertEquals("20-12-2014 02:30:00", df.format(event.eventDate));
144144
}
145145

‎jackson/src/test/java/com/baeldung/jackson/test/JacksonExceptionsTest.java‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
import java.io.IOException;
88
import java.util.List;
99

10-
import com.baeldung.jackson.exception.*;
1110
import org.junit.Test;
1211

12+
import com.baeldung.jackson.exception.User;
13+
import com.baeldung.jackson.exception.UserWithPrivateFields;
14+
import com.baeldung.jackson.exception.UserWithRoot;
15+
import com.baeldung.jackson.exception.Zoo;
16+
import com.baeldung.jackson.exception.ZooConfigured;
1317
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
1418
import com.fasterxml.jackson.annotation.PropertyAccessor;
1519
import com.fasterxml.jackson.core.JsonFactory;
@@ -30,15 +34,15 @@ public void givenAbstractClass_whenDeserializing_thenException() throws IOExcept
3034
final String json = "{\"animal\":{\"name\":\"lacy\"}}";
3135
final ObjectMapper mapper = new ObjectMapper();
3236

33-
mapper.reader().withType(Zoo.class).readValue(json);
37+
mapper.reader().forType(Zoo.class).readValue(json);
3438
}
3539

3640
@Test
3741
public void givenAbstractClassConfigured_whenDeserializing_thenCorrect() throws IOException {
3842
final String json = "{\"animal\":{\"name\":\"lacy\"}}";
3943
final ObjectMapper mapper = new ObjectMapper();
4044

41-
mapper.reader().withType(ZooConfigured.class).readValue(json);
45+
mapper.reader().forType(ZooConfigured.class).readValue(json);
4246
}
4347

4448
// JsonMappingException: No serializer found for class
@@ -67,15 +71,15 @@ public void givenNoDefaultConstructor_whenDeserializing_thenException() throws I
6771
final String json = "{\"id\":1,\"name\":\"John\"}";
6872
final ObjectMapper mapper = new ObjectMapper();
6973

70-
mapper.reader().withType(User.class).readValue(json);
74+
mapper.reader().forType(User.class).readValue(json);
7175
}
7276

7377
@Test
7478
public void givenDefaultConstructor_whenDeserializing_thenCorrect() throws IOException {
7579
final String json = "{\"id\":1,\"name\":\"John\"}";
7680
final ObjectMapper mapper = new ObjectMapper();
7781

78-
final com.baeldung.jackson.dtos.User user = mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
82+
final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
7983
assertEquals("John", user.name);
8084
}
8185

@@ -87,7 +91,7 @@ public void givenWrappedJsonString_whenDeserializing_thenException() throws IOEx
8791
final ObjectMapper mapper = new ObjectMapper();
8892
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
8993

90-
mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
94+
mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
9195
}
9296

9397
@Test
@@ -97,7 +101,7 @@ public void givenWrappedJsonStringAndConfigureClass_whenDeserializing_thenCorrec
97101
final ObjectMapper mapper = new ObjectMapper();
98102
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
99103

100-
final UserWithRoot user = mapper.reader().withType(UserWithRoot.class).readValue(json);
104+
final UserWithRoot user = mapper.reader().forType(UserWithRoot.class).readValue(json);
101105
assertEquals("John", user.name);
102106
}
103107

@@ -107,15 +111,15 @@ public void givenJsonOfArray_whenDeserializing_thenException() throws JsonProces
107111
final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]";
108112
final ObjectMapper mapper = new ObjectMapper();
109113

110-
mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
114+
mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
111115
}
112116

113117
@Test
114118
public void givenJsonOfArray_whenDeserializing_thenCorrect() throws JsonProcessingException, IOException {
115119
final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]";
116120
final ObjectMapper mapper = new ObjectMapper();
117121

118-
final List<com.baeldung.jackson.dtos.User> users = mapper.reader().withType(new TypeReference<List<com.baeldung.jackson.dtos.User>>() {
122+
final List<com.baeldung.jackson.dtos.User> users = mapper.reader().forType(new TypeReference<List<com.baeldung.jackson.dtos.User>>() {
119123
}).readValue(json);
120124

121125
assertEquals(2, users.size());
@@ -127,7 +131,7 @@ public void givenJsonStringWithExtra_whenDeserializing_thenException() throws IO
127131
final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}";
128132

129133
final ObjectMapper mapper = new ObjectMapper();
130-
mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
134+
mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
131135
}
132136

133137
@Test
@@ -137,7 +141,7 @@ public void givenJsonStringWithExtra_whenConfigureDeserializing_thenCorrect() th
137141
final ObjectMapper mapper = new ObjectMapper();
138142
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
139143

140-
final com.baeldung.jackson.dtos.User user = mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
144+
final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
141145
assertEquals("John", user.name);
142146
}
143147

@@ -147,7 +151,7 @@ public void givenStringWithSingleQuotes_whenDeserializing_thenException() throws
147151
final String json = "{'id':1,'name':'John'}";
148152
final ObjectMapper mapper = new ObjectMapper();
149153

150-
mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
154+
mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
151155
}
152156

153157
@Test
@@ -158,7 +162,7 @@ public void givenStringWithSingleQuotes_whenConfigureDeserializing_thenCorrect()
158162
factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
159163
final ObjectMapper mapper = new ObjectMapper(factory);
160164

161-
final com.baeldung.jackson.dtos.User user = mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
165+
final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
162166
assertEquals("John", user.name);
163167
}
164168

0 commit comments

Comments
 (0)