Skip to content

Commit c55d117

Browse files
authored
Make Jackson object mapper available from custom deserializers (#129) (#130)
1 parent 700173a commit c55d117

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

java-client/src/main/java/co/elastic/clients/json/jackson/JacksonJsonpMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ public class JacksonJsonpMapper extends JsonpMapperBase {
4040
private final ObjectMapper objectMapper;
4141

4242
public JacksonJsonpMapper(ObjectMapper objectMapper) {
43-
this.provider = new JacksonJsonProvider();
4443
this.objectMapper = objectMapper
4544
.configure(SerializationFeature.INDENT_OUTPUT, false)
4645
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
46+
// Creating the json factory from the mapper ensures it will be returned by JsonParser.getCodec()
47+
this.provider = new JacksonJsonProvider(this.objectMapper.getFactory());
4748
}
4849

4950
public JacksonJsonpMapper() {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.elasticsearch.json.jackson;
21+
22+
import co.elastic.clients.elasticsearch.core.search.Hit;
23+
import co.elastic.clients.elasticsearch.model.ModelTestCase;
24+
import co.elastic.clients.json.JsonpDeserializer;
25+
import co.elastic.clients.json.JsonpMapper;
26+
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
27+
import com.fasterxml.jackson.core.JsonParser;
28+
import com.fasterxml.jackson.databind.DeserializationContext;
29+
import com.fasterxml.jackson.databind.JsonDeserializer;
30+
import com.fasterxml.jackson.databind.JsonNode;
31+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
32+
import org.junit.Test;
33+
34+
import java.io.IOException;
35+
36+
public class JacksonMapperTest extends ModelTestCase {
37+
38+
@Test
39+
public void testCustomDeserializer() {
40+
// See https://github.com/elastic/elasticsearch-java/issues/120
41+
JsonpMapper jsonpMapper = new JacksonJsonpMapper();
42+
43+
String json = "{\"_index\":\"foo\",\"_id\":\"1\",\"_source\":{\"model\":\"Foo\",\"age\":42}}";
44+
45+
Hit<TestData> testDataHit = fromJson(json,
46+
Hit.createHitDeserializer(JsonpDeserializer.of(TestData.class)),
47+
jsonpMapper
48+
);
49+
TestData data = testDataHit.source();
50+
assertEquals("Foo", data.theModel);
51+
assertEquals(42, data.theAge);
52+
}
53+
54+
@JsonDeserialize(using = TestData.TestDeserializer.class)
55+
public static class TestData {
56+
public String theModel;
57+
public int theAge;
58+
59+
public static class TestDeserializer extends JsonDeserializer<TestData> {
60+
61+
@Override
62+
public TestData deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
63+
JsonNode node = jp.getCodec().readTree(jp);
64+
65+
TestData res = new TestData();
66+
if (node.has("age")) {
67+
res.theAge = node.get("age").asInt();
68+
}
69+
if (node.has("model")) {
70+
res.theModel = node.get("model").asText();
71+
}
72+
return res;
73+
}
74+
}
75+
}
76+
}

java-client/src/test/java/co/elastic/clients/elasticsearch/model/ModelTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,15 @@ protected <T> T checkJsonRoundtrip(T value, String expectedJson) {
101101
}
102102

103103
protected <T> T fromJson(String json, JsonpDeserializer<T> deserializer) {
104+
return fromJson(json, deserializer, mapper);
105+
}
106+
107+
protected <T> T fromJson(String json, JsonpDeserializer<T> deserializer, JsonpMapper mapper) {
104108
JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
105109
return deserializer.deserialize(parser, mapper);
106110
}
107111

112+
108113
public static void assertGetterType(Class<?> expected, Class<?> clazz, String name) {
109114
Method method;
110115
try {

0 commit comments

Comments
 (0)