Skip to content

Commit 2364171

Browse files
l-trottaswallez
andauthored
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 <[email protected]>
1 parent e931921 commit 2364171

25 files changed

+2289
-8
lines changed

java-client/build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ signing {
177177
dependencies {
178178
val elasticsearchVersion = "9.0.0"
179179
val jacksonVersion = "2.18.3"
180+
val jackson3Version = "3.0.0"
180181
val openTelemetryVersion = "1.32.0"
181182

182183
api(project(":rest5-client"))
@@ -224,6 +225,11 @@ dependencies {
224225
implementation("com.fasterxml.jackson.core", "jackson-core", jacksonVersion)
225226
implementation("com.fasterxml.jackson.core", "jackson-databind", jacksonVersion)
226227

228+
// Apache 2.0
229+
// https://github.com/FasterXML/jackson
230+
implementation("tools.jackson.core", "jackson-databind", jackson3Version)
231+
implementation("tools.jackson.core", "jackson-core", jackson3Version)
232+
227233
// EPL-2.0 OR BSD-3-Clause
228234
// https://eclipse-ee4j.github.io/yasson/
229235
testImplementation("org.eclipse", "yasson", "3.0.4")
@@ -306,6 +312,7 @@ class SpdxReporter(val dest: File) : ReportRenderer {
306312
"org.apache.httpcomponents.client5" -> "https://hc.apache.org/"
307313
"org.apache.httpcomponents.core5" -> "https://hc.apache.org/"
308314
"com.fasterxml.jackson" -> "https://github.com/FasterXML/jackson"
315+
"tools.jackson" -> " https://github.com/FasterXML/jackson-bom "
309316
else -> if (info.moduleUrls.isEmpty()) {
310317
throw RuntimeException("No URL found for module '$depName'")
311318
} else {
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)