Skip to content

Commit 66faf09

Browse files
Merge pull request #38 from folio-org/EDGAPIUTL-37
EDGAPIUTL-37: Migrate Jackson from 2 to 3 (tools.jackson.core)
2 parents fb0fe82 + a2aafe8 commit 66faf09

5 files changed

Lines changed: 41 additions & 28 deletions

File tree

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
<dependencyManagement>
3838
<dependencies>
3939
<dependency>
40-
<groupId>com.fasterxml.jackson</groupId>
40+
<groupId>tools.jackson</groupId>
4141
<artifactId>jackson-bom</artifactId>
42-
<version>2.21.0</version>
42+
<version>3.1.0</version>
4343
<scope>import</scope>
4444
<type>pom</type>
4545
</dependency>
@@ -92,7 +92,7 @@
9292
<version>${aws-java-sdk.version}</version>
9393
</dependency>
9494
<dependency>
95-
<groupId>com.fasterxml.jackson.dataformat</groupId>
95+
<groupId>tools.jackson.dataformat</groupId>
9696
<artifactId>jackson-dataformat-xml</artifactId>
9797
</dependency>
9898
<!-- Other dependencies -->

src/main/java/org/folio/edge/api/utils/Mappers.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package org.folio.edge.api.utils;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import com.fasterxml.jackson.databind.SerializationFeature;
5-
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
63
import java.text.SimpleDateFormat;
4+
import tools.jackson.databind.SerializationFeature;
5+
import tools.jackson.databind.json.JsonMapper;
6+
import tools.jackson.dataformat.xml.XmlMapper;
77

8-
public class Mappers {
8+
/**
9+
* Provide {@link #jsonMapper} and {@link #xmlMapper}.
10+
*/
11+
public final class Mappers {
912
public static final String XML_PROLOG = "<?xml version='1.0' encoding='UTF-8'?>\n";
1013

1114
public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
1215

13-
public static final ObjectMapper jsonMapper = new ObjectMapper()
16+
public static final JsonMapper jsonMapper = JsonMapper.builder()
1417
.enable(SerializationFeature.INDENT_OUTPUT)
15-
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
16-
.setDateFormat(new SimpleDateFormat(DATE_FORMAT));
18+
.defaultDateFormat(new SimpleDateFormat(DATE_FORMAT))
19+
.build();
1720

18-
public static final XmlMapper xmlMapper = (XmlMapper) new XmlMapper()
21+
public static final XmlMapper xmlMapper = XmlMapper.builder()
1922
.enable(SerializationFeature.INDENT_OUTPUT)
20-
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
21-
.setDateFormat(new SimpleDateFormat(DATE_FORMAT));
23+
.defaultDateFormat(new SimpleDateFormat(DATE_FORMAT))
24+
.build();
2225

2326
private Mappers() {
2427

src/main/java/org/folio/edge/api/utils/util/ApiKeyParser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.folio.edge.api.utils.util;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import java.util.Base64;
54
import org.apache.commons.lang3.StringUtils;
65
import org.folio.edge.api.utils.model.ClientInfo;
6+
import tools.jackson.databind.json.JsonMapper;
77

88
public class ApiKeyParser {
99

@@ -15,8 +15,7 @@ public static ClientInfo parseApiKey(String apiKey) throws MalformedApiKeyExcept
1515

1616
try {
1717
String decoded = new String(Base64.getUrlDecoder().decode(apiKey.getBytes()));
18-
ObjectMapper mapper = new ObjectMapper();
19-
clientInfo = mapper.readValue(decoded, ClientInfo.class);
18+
clientInfo = JsonMapper.shared().readValue(decoded, ClientInfo.class);
2019
} catch (Exception var4) {
2120
throw new MalformedApiKeyException("Failed to parse apiKey to retrieve client info", var4);
2221
}

src/test/java/org/folio/edge/api/utils/MappersTest.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package org.folio.edge.api.utils;
22

3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
35
import static org.junit.Assert.assertEquals;
46

5-
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import com.fasterxml.jackson.databind.node.ObjectNode;
7-
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
8-
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.annotation.JsonRootName;
9+
import java.time.OffsetDateTime;
910
import java.util.HashMap;
1011
import java.util.Map;
1112
import org.junit.Test;
13+
import tools.jackson.databind.json.JsonMapper;
1214

1315
public class MappersTest {
1416

@@ -17,8 +19,8 @@ public void testJsonMapper() throws Exception {
1719
String key = "foo";
1820
String value = "bar";
1921

20-
ObjectMapper mapper = new ObjectMapper();
21-
ObjectNode node = mapper.createObjectNode();
22+
var mapper = JsonMapper.shared();
23+
var node = mapper.createObjectNode();
2224
node.put(key, value);
2325

2426
String json = node.toPrettyString();
@@ -54,15 +56,24 @@ public void testXmlMapper() throws Exception {
5456
assertEquals(obj.b, asObj.b);
5557
}
5658

57-
@JacksonXmlRootElement(localName = "test")
59+
@Test
60+
public void testJsonDate() {
61+
var datestring1 = "\"1999-12-31T23:59:58.765Z\"";
62+
var date = Mappers.jsonMapper.readValue(datestring1, OffsetDateTime.class);
63+
assertThat(date.toString(), is("1999-12-31T23:59:58.765Z"));
64+
var datestring2 = Mappers.jsonMapper.writeValueAsString(date);
65+
assertThat(datestring2, is(datestring1));
66+
}
67+
68+
@JsonRootName(value = "test")
5869
public static class TestObject {
5970

60-
@JacksonXmlProperty(localName = "a")
71+
@JsonProperty(value = "a")
6172
private String a;
62-
@JacksonXmlProperty(localName = "b")
73+
@JsonProperty(value = "b")
6374
private String b;
6475

65-
public TestObject(@JacksonXmlProperty(localName = "a") String a, @JacksonXmlProperty(localName = "b") String b) {
76+
public TestObject(@JsonProperty(value = "a") String a, @JsonProperty(value = "b") String b) {
6677
this.a = a;
6778
this.b = b;
6879
}

src/test/java/org/folio/edge/api/utils/util/ApiKeyParserTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import static org.junit.Assert.assertEquals;
44

5-
import com.fasterxml.jackson.databind.ObjectMapper;
65
import java.util.Base64;
76
import org.folio.edge.api.utils.model.ClientInfo;
87
import org.folio.edge.api.utils.util.ApiKeyParser.MalformedApiKeyException;
98
import org.junit.Assert;
109
import org.junit.Test;
10+
import tools.jackson.databind.json.JsonMapper;
1111

1212
public class ApiKeyParserTest {
1313

@@ -16,7 +16,7 @@ public class ApiKeyParserTest {
1616
public static final String USERNAME = "diku";
1717
public static final String API_KEY = "eyJzIjoiZ0szc0RWZ3labCIsInQiOiJkaWt1IiwidSI6ImRpa3UifQ==";
1818
public static final String BAD_API_KEY = "broken";
19-
ObjectMapper objectMapper = new ObjectMapper();
19+
JsonMapper objectMapper = JsonMapper.shared();
2020

2121

2222
@Test

0 commit comments

Comments
 (0)