Skip to content

Commit 908eee2

Browse files
committed
migrate jackson 2.x to 3.x
1 parent e818480 commit 908eee2

25 files changed

+367
-396
lines changed

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- uses: actions/setup-java@v5
1515
with:
1616
distribution: temurin
17-
java-version: 11
17+
java-version: 17
1818

1919
- name: Set up Gradle
2020
uses: gradle/actions/setup-gradle@06832c7b30a0129d7fb559bcc6e43d26f6374244 # v4.3.1

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
rl-scanner:
1919
uses: ./.github/workflows/rl-secure.yml
2020
with:
21-
java-version: 11
21+
java-version: 17
2222
artifact-name: 'java-jwt.tgz'
2323
secrets:
2424
RLSECURE_LICENSE: ${{ secrets.RLSECURE_LICENSE }}
@@ -31,7 +31,7 @@ jobs:
3131
uses: ./.github/workflows/java-release.yml
3232
needs: rl-scanner
3333
with:
34-
java-version: 11.0.21-tem
34+
java-version: 17.0.12-tem
3535
secrets:
3636
ossr-username: ${{ secrets.OSSR_USERNAME }}
3737
ossr-token: ${{ secrets.OSSR_TOKEN }}

.github/workflows/snyk.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ jobs:
3434
with:
3535
ref: ${{ github.event.pull_request.head.sha || github.ref }}
3636

37-
- uses: snyk/actions/gradle-jdk11@b98d498629f1c368650224d6d212bf7dfa89e4bf # pin@0.4.0
37+
- uses: snyk/actions/gradle-jdk17@master # pin@0.4.0
3838
env:
3939
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}

gradle/maven-publish.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tasks.withType(Javadoc).configureEach {
1919

2020
javadoc {
2121
// Specify the Java version that the project will use
22-
options.addStringOption('-release', "11")
22+
options.addStringOption('-release', "17")
2323
}
2424
artifacts {
2525
archives sourcesJar, javadocJar
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.9.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.6-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

lib/build.gradle

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
buildscript {
2-
repositories {
3-
jcenter()
4-
}
5-
6-
dependencies {
7-
// https://github.com/melix/japicmp-gradle-plugin/issues/36
8-
classpath 'com.google.guava:guava:31.1-jre'
9-
}
10-
}
11-
121
plugins {
132
id 'java'
143
id 'jacoco'
154
id 'checkstyle'
16-
id 'me.champeau.gradle.japicmp' version '0.4.1'
5+
id 'me.champeau.gradle.japicmp' version '0.4.6'
176
id 'maven-publish'
187
}
198

@@ -102,29 +91,29 @@ private static File getBaselineJar(Project project, String baselineVersion) {
10291

10392
ext {
10493
baselineCompareVersion = '4.1.0'
105-
testInJavaVersions = [8, 11, 17, 21]
94+
testInJavaVersions = [17, 21]
10695
}
10796

10897
java {
10998
toolchain {
110-
languageVersion = JavaLanguageVersion.of(11)
99+
languageVersion = JavaLanguageVersion.of(17)
111100
}
112101
}
113102

114103
compileJava {
115-
exclude 'module-info.java'
116-
// Required to be compatible with JDK 8+
117-
options.release = 8
104+
options.release = 17
105+
options.compilerArgs << "-Xlint:unchecked"
118106
}
119107

120108
javadoc {
121109
// Exclude internal implementation package from javadoc
122110
excludes = ['com/auth0/jwt/impl', 'module-info.java']
111+
modularity.inferModulePath = false
123112
}
124113

125114
dependencies {
126-
implementation 'com.fasterxml.jackson.core:jackson-core:2.21.0'
127-
implementation 'com.fasterxml.jackson.core:jackson-databind:2.21.0'
115+
implementation 'tools.jackson.core:jackson-core:3.0.4'
116+
implementation 'tools.jackson.core:jackson-databind:3.0.4'
128117

129118
testImplementation 'org.bouncycastle:bcprov-jdk15on:1.70'
130119
testImplementation 'junit:junit:4.13.2'
@@ -155,39 +144,11 @@ test {
155144
}
156145
}
157146

158-
task compileModuleInfoJava(type: JavaCompile) {
159-
classpath = files()
160-
source = 'src/main/java/module-info.java'
161-
destinationDir = compileJava.destinationDir
162-
doLast {
163-
def descriptor = new File(compileJava.destinationDir, 'module-info.class')
164-
def dest = new File(compileJava.destinationDir, 'META-INF/versions/9')
165-
ant.move file: descriptor, todir: dest
166-
}
167-
168-
doFirst {
169-
options.compilerArgs = [
170-
'--release', '9',
171-
'--module-path', compileJava.classpath.asPath
172-
]
173-
}
174-
}
175-
176147
compileTestJava {
177-
options.release = 8
148+
options.release = 17
178149
options.compilerArgs = ["-Xlint:deprecation"]
179150
}
180151

181-
def testJava8 = tasks.register('testJava8', Test) {
182-
description = 'Runs unit tests on Java 8.'
183-
group = 'verification'
184-
185-
javaLauncher.set(javaToolchains.launcherFor {
186-
languageVersion = JavaLanguageVersion.of(8)
187-
})
188-
shouldRunAfter(tasks.named('test'))
189-
}
190-
191152
def testJava17 = tasks.register('testJava17', Test) {
192153
description = 'Runs unit tests on Java 17.'
193154
group = 'verification'
@@ -209,7 +170,6 @@ def testJava21 = tasks.register('testJava21', Test) {
209170
}
210171

211172
tasks.named('check') {
212-
dependsOn(testJava8)
213173
dependsOn(testJava17)
214174
dependsOn(testJava21)
215175
}
@@ -218,9 +178,6 @@ jar {
218178
manifest.attributes('Multi-Release': 'true')
219179
}
220180

221-
compileModuleInfoJava.dependsOn compileJava
222-
classes.dependsOn compileModuleInfoJava
223-
224181
// you can pass any arguments JMH accepts via Gradle args.
225182
// Example: ./gradlew runJMH --args="-lrf"
226183
tasks.register('runJMH', JavaExec) {

lib/src/main/java/com/auth0/jwt/JWTCreator.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import com.auth0.jwt.exceptions.JWTCreationException;
55
import com.auth0.jwt.exceptions.SignatureGenerationException;
66
import com.auth0.jwt.impl.*;
7-
import com.fasterxml.jackson.core.JsonProcessingException;
8-
import com.fasterxml.jackson.databind.MapperFeature;
9-
import com.fasterxml.jackson.databind.ObjectMapper;
10-
import com.fasterxml.jackson.databind.json.JsonMapper;
11-
import com.fasterxml.jackson.databind.module.SimpleModule;
7+
import tools.jackson.databind.MapperFeature;
8+
import tools.jackson.databind.ObjectMapper;
9+
import tools.jackson.databind.json.JsonMapper;
10+
import tools.jackson.databind.module.SimpleModule;
11+
import tools.jackson.core.JacksonException;
1212

1313
import java.nio.charset.StandardCharsets;
1414
import java.time.Instant;
@@ -38,8 +38,8 @@ public final class JWTCreator {
3838

3939
mapper = JsonMapper.builder()
4040
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
41-
.build()
42-
.registerModule(module);
41+
.addModule(module)
42+
.build();
4343
}
4444

4545
private JWTCreator(Algorithm algorithm, Map<String, Object> headerClaims, Map<String, Object> payloadClaims)
@@ -48,7 +48,7 @@ private JWTCreator(Algorithm algorithm, Map<String, Object> headerClaims, Map<St
4848
try {
4949
headerJson = mapper.writeValueAsString(new HeaderClaimsHolder(headerClaims));
5050
payloadJson = mapper.writeValueAsString(new PayloadClaimsHolder(payloadClaims));
51-
} catch (JsonProcessingException e) {
51+
} catch (JacksonException e) {
5252
throw new JWTCreationException("Some of the Claims couldn't be converted to a valid JSON format.", e);
5353
}
5454
}
@@ -114,7 +114,7 @@ public Builder withHeader(String headerClaimsJson) throws IllegalArgumentExcepti
114114
try {
115115
Map<String, Object> headerClaims = mapper.readValue(headerClaimsJson, LinkedHashMap.class);
116116
return withHeader(headerClaims);
117-
} catch (JsonProcessingException e) {
117+
} catch (JacksonException e) {
118118
throw new IllegalArgumentException("Invalid header JSON", e);
119119
}
120120
}
@@ -510,7 +510,7 @@ public Builder withPayload(String payloadClaimsJson) throws IllegalArgumentExcep
510510
try {
511511
Map<String, Object> payloadClaims = mapper.readValue(payloadClaimsJson, LinkedHashMap.class);
512512
return withPayload(payloadClaims);
513-
} catch (JsonProcessingException e) {
513+
} catch (JacksonException e) {
514514
throw new IllegalArgumentException("Invalid payload JSON", e);
515515
}
516516
}

lib/src/main/java/com/auth0/jwt/impl/BasicHeader.java

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

33
import com.auth0.jwt.interfaces.Claim;
44
import com.auth0.jwt.interfaces.Header;
5-
import com.fasterxml.jackson.core.ObjectCodec;
6-
import com.fasterxml.jackson.databind.JsonNode;
5+
import tools.jackson.databind.DeserializationContext;
6+
import tools.jackson.databind.JsonNode;
77

88
import java.io.Serializable;
99
import java.util.Collections;
@@ -22,22 +22,22 @@ class BasicHeader implements Header, Serializable {
2222
private final String contentType;
2323
private final String keyId;
2424
private final Map<String, JsonNode> tree;
25-
private final ObjectCodec objectCodec;
25+
private final DeserializationContext context;
2626

2727
BasicHeader(
2828
String algorithm,
2929
String type,
3030
String contentType,
3131
String keyId,
3232
Map<String, JsonNode> tree,
33-
ObjectCodec objectCodec
33+
DeserializationContext context
3434
) {
3535
this.algorithm = algorithm;
3636
this.type = type;
3737
this.contentType = contentType;
3838
this.keyId = keyId;
3939
this.tree = tree == null ? Collections.emptyMap() : Collections.unmodifiableMap(tree);
40-
this.objectCodec = objectCodec;
40+
this.context = context;
4141
}
4242

4343
Map<String, JsonNode> getTree() {
@@ -66,6 +66,6 @@ public String getKeyId() {
6666

6767
@Override
6868
public Claim getHeaderClaim(String name) {
69-
return extractClaim(name, tree, objectCodec);
69+
return extractClaim(name, tree, context);
7070
}
7171
}

lib/src/main/java/com/auth0/jwt/impl/ClaimsSerializer.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.auth0.jwt.impl;
22

3-
import com.fasterxml.jackson.core.JsonGenerator;
4-
import com.fasterxml.jackson.databind.SerializerProvider;
5-
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
3+
import tools.jackson.core.JacksonException;
4+
import tools.jackson.core.JsonGenerator;
5+
import tools.jackson.databind.ser.std.StdSerializer;
6+
import tools.jackson.databind.SerializationContext;
67

7-
import java.io.IOException;
88
import java.time.Instant;
99
import java.util.Date;
1010
import java.util.List;
@@ -22,7 +22,7 @@ public ClaimsSerializer(Class<T> t) {
2222
}
2323

2424
@Override
25-
public void serialize(T holder, JsonGenerator gen, SerializerProvider provider) throws IOException {
25+
public void serialize(T holder, JsonGenerator gen, SerializationContext provider) throws JacksonException {
2626
gen.writeStartObject();
2727
for (Map.Entry<String, Object> entry : holder.getClaims().entrySet()) {
2828
writeClaim(entry, gen);
@@ -37,14 +37,14 @@ public void serialize(T holder, JsonGenerator gen, SerializerProvider provider)
3737
*
3838
* @param entry The entry that corresponds to the JSON field to write
3939
* @param gen The {@code JsonGenerator} to use
40-
* @throws IOException if there is either an underlying I/O problem or encoding issue at format layer
40+
* @throws JacksonException if there is either an underlying I/O problem or encoding issue at format layer
4141
*/
42-
protected void writeClaim(Map.Entry<String, Object> entry, JsonGenerator gen) throws IOException {
43-
gen.writeFieldName(entry.getKey());
42+
protected void writeClaim(Map.Entry<String, Object> entry, JsonGenerator gen) throws JacksonException {
43+
gen.writeName(entry.getKey());
4444
handleSerialization(entry.getValue(), gen);
4545
}
4646

47-
private static void handleSerialization(Object value, JsonGenerator gen) throws IOException {
47+
private static void handleSerialization(Object value, JsonGenerator gen) throws JacksonException {
4848
if (value instanceof Date) {
4949
gen.writeNumber(dateToSeconds((Date) value));
5050
} else if (value instanceof Instant) { // EXPIRES_AT, ISSUED_AT, NOT_BEFORE, custom Instant claims
@@ -54,21 +54,21 @@ private static void handleSerialization(Object value, JsonGenerator gen) throws
5454
} else if (value instanceof List) {
5555
serializeList((List<?>) value, gen);
5656
} else {
57-
gen.writeObject(value);
57+
gen.writePOJO(value);
5858
}
5959
}
6060

61-
private static void serializeMap(Map<?, ?> map, JsonGenerator gen) throws IOException {
61+
private static void serializeMap(Map<?, ?> map, JsonGenerator gen) throws JacksonException {
6262
gen.writeStartObject();
6363
for (Map.Entry<?, ?> entry : map.entrySet()) {
64-
gen.writeFieldName((String) entry.getKey());
64+
gen.writeName((String) entry.getKey());
6565
Object value = entry.getValue();
6666
handleSerialization(value, gen);
6767
}
6868
gen.writeEndObject();
6969
}
7070

71-
private static void serializeList(List<?> list, JsonGenerator gen) throws IOException {
71+
private static void serializeList(List<?> list, JsonGenerator gen) throws JacksonException {
7272
gen.writeStartArray();
7373
for (Object entry : list) {
7474
handleSerialization(entry, gen);

lib/src/main/java/com/auth0/jwt/impl/HeaderDeserializer.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import com.auth0.jwt.HeaderParams;
44
import com.auth0.jwt.exceptions.JWTDecodeException;
55
import com.auth0.jwt.interfaces.Header;
6-
import com.fasterxml.jackson.core.JsonParser;
7-
import com.fasterxml.jackson.core.type.TypeReference;
8-
import com.fasterxml.jackson.databind.DeserializationContext;
9-
import com.fasterxml.jackson.databind.JsonNode;
10-
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
6+
import tools.jackson.core.JacksonException;
7+
import tools.jackson.core.JsonParser;
8+
import tools.jackson.core.type.TypeReference;
9+
import tools.jackson.databind.DeserializationContext;
10+
import tools.jackson.databind.JsonNode;
11+
import tools.jackson.databind.deser.std.StdDeserializer;
1112

12-
import java.io.IOException;
1313
import java.util.Map;
1414

1515
/**
@@ -26,8 +26,8 @@ class HeaderDeserializer extends StdDeserializer<Header> {
2626
}
2727

2828
@Override
29-
public Header deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
30-
Map<String, JsonNode> tree = p.getCodec().readValue(p, new TypeReference<Map<String, JsonNode>>() {
29+
public Header deserialize(JsonParser p, DeserializationContext ctxt) throws JacksonException {
30+
Map<String, JsonNode> tree = ctxt.readValue(p, new TypeReference<>() {
3131
});
3232
if (tree == null) {
3333
throw new JWTDecodeException("Parsing the Header's JSON resulted on a Null map");
@@ -37,14 +37,14 @@ public Header deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
3737
String type = getString(tree, HeaderParams.TYPE);
3838
String contentType = getString(tree, HeaderParams.CONTENT_TYPE);
3939
String keyId = getString(tree, HeaderParams.KEY_ID);
40-
return new BasicHeader(algorithm, type, contentType, keyId, tree, p.getCodec());
40+
return new BasicHeader(algorithm, type, contentType, keyId, tree, ctxt);
4141
}
4242

4343
String getString(Map<String, JsonNode> tree, String claimName) {
4444
JsonNode node = tree.get(claimName);
4545
if (node == null || node.isNull()) {
4646
return null;
4747
}
48-
return node.asText(null);
48+
return node.asString(null);
4949
}
5050
}

0 commit comments

Comments
 (0)