Skip to content

Commit 856c99d

Browse files
l-trottaswallez
andauthored
Backporting Jackson 3 support to 8.19 (#1158)
* Jackson 3 support (#1074) * add jackson 3 support * updated unit tests * fix test * removed comment * deprecate jackson 2 classes * Catch parsing exceptions to convert them to JsonP's JsonParsingException * Jackson 3 does work :-) * cleanup, using stable dependencies * refactor gradle * add new jackson bom url * restore exception wrap behavior --------- Co-authored-by: Sylvain Wallez <sylvain@elastic.co> * undeprecate jackson 2 (#1095) * fix previous cherry pick --------- Co-authored-by: Sylvain Wallez <sylvain@elastic.co>
1 parent 1efb7f8 commit 856c99d

17 files changed

Lines changed: 2280 additions & 9 deletions

java-client/build.gradle.kts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ dependencies {
180180
val elasticsearchVersion = "8.19.0"
181181
val jacksonVersion = "2.17.0"
182182
val openTelemetryVersion = "1.29.0"
183+
val jackson3Version = "3.0.0"
183184

184185
// Apache 2.0
185186
// https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html
@@ -217,6 +218,11 @@ dependencies {
217218
testImplementation("com.fasterxml.jackson.core", "jackson-core", jacksonVersion)
218219
testImplementation("com.fasterxml.jackson.core", "jackson-databind", jacksonVersion)
219220

221+
// Apache 2.0
222+
// https://github.com/FasterXML/jackson
223+
implementation("tools.jackson.core", "jackson-databind", jackson3Version)
224+
implementation("tools.jackson.core", "jackson-core", jackson3Version)
225+
220226
// EPL-2.0 OR BSD-3-Clause
221227
// https://eclipse-ee4j.github.io/yasson/
222228
testImplementation("org.eclipse", "yasson", "2.0.4") {
@@ -288,7 +294,17 @@ class SpdxReporter(val dest: File) : ReportRenderer {
288294
val depName = dep.group + ":" + dep.name
289295

290296
val info = LicenseDataCollector.multiModuleLicenseInfo(dep)
291-
val depUrl = info.moduleUrls.first()
297+
val depUrl = when(dep.group) {
298+
"org.apache.httpcomponents.client5" -> "https://hc.apache.org/"
299+
"org.apache.httpcomponents.core5" -> "https://hc.apache.org/"
300+
"com.fasterxml.jackson" -> "https://github.com/FasterXML/jackson"
301+
"tools.jackson" -> " https://github.com/FasterXML/jackson-bom "
302+
else -> if (info.moduleUrls.isEmpty()) {
303+
throw RuntimeException("No URL found for module '$depName'")
304+
} else {
305+
info.moduleUrls.first()
306+
}
307+
}
292308

293309
val licenseIds = info.licenses.mapNotNull { license ->
294310
license.name?.let {
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.json.jackson;
21+
22+
import co.elastic.clients.json.JsonBuffer;
23+
import co.elastic.clients.json.JsonData;
24+
import co.elastic.clients.json.JsonpDeserializer;
25+
import co.elastic.clients.json.JsonpMapper;
26+
import co.elastic.clients.json.JsonpUtils;
27+
28+
import jakarta.json.JsonValue;
29+
import jakarta.json.stream.JsonGenerator;
30+
import jakarta.json.stream.JsonParser;
31+
import tools.jackson.core.JacksonException;
32+
import tools.jackson.databind.util.TokenBuffer;
33+
34+
import java.io.StringWriter;
35+
import java.lang.reflect.Type;
36+
37+
class Jackson3JsonBuffer implements JsonBuffer, JsonData {
38+
private final TokenBuffer buffer;
39+
private final Jackson3JsonpMapper mapper;
40+
41+
Jackson3JsonBuffer(TokenBuffer buffer, Jackson3JsonpMapper mapper) {
42+
this.buffer = buffer;
43+
this.mapper = mapper;
44+
}
45+
46+
@Override
47+
public JsonParser asParser() {
48+
return new Jackson3JsonpParser(buffer.asParser(), mapper);
49+
}
50+
51+
@Override
52+
public JsonValue toJson() {
53+
try (JsonParser parser = asParser()) {
54+
parser.next(); // move to first event
55+
return parser.getValue();
56+
}
57+
}
58+
59+
@Override
60+
public JsonValue toJson(JsonpMapper mapper) {
61+
// We don't need the mapper
62+
return toJson();
63+
}
64+
65+
@Override
66+
public <T> T to(Type type) {
67+
return to(type, this.mapper);
68+
}
69+
70+
@Override
71+
public <T> T to(Type type, JsonpMapper mapper) {
72+
try (JsonParser parser = asParser()) {
73+
return mapper.deserialize(parser, type);
74+
}
75+
}
76+
77+
@Override
78+
public <T> T deserialize(JsonpDeserializer<T> deserializer) {
79+
return deserialize(deserializer, this.mapper);
80+
}
81+
82+
@Override
83+
public <T> T deserialize(JsonpDeserializer<T> deserializer, JsonpMapper mapper) {
84+
try (JsonParser parser = asParser()) {
85+
return deserializer.deserialize(parser, mapper);
86+
}
87+
}
88+
89+
@Override
90+
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
91+
if (generator instanceof Jackson3JsonpGenerator) {
92+
Jackson3JsonpGenerator jkGenerator = (Jackson3JsonpGenerator) generator;
93+
try {
94+
buffer.serialize(jkGenerator.jacksonGenerator());
95+
} catch (JacksonException e) {
96+
throw Jackson3Utils.convertException(e);
97+
}
98+
} else {
99+
try (JsonParser parser = asParser()) {
100+
JsonpUtils.copy(parser, generator);
101+
}
102+
}
103+
}
104+
105+
/**
106+
* Renders this buffer as a JSON string for debugger and logging convenience.
107+
*/
108+
@Override
109+
public String toString() {
110+
StringWriter writer = new StringWriter();
111+
try (Jackson3JsonpGenerator generator =
112+
new Jackson3JsonpGenerator(mapper.objectMapper().createGenerator(writer))) {
113+
serialize(generator, mapper);
114+
generator.close();
115+
return writer.toString();
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)