Skip to content

[7.17] Make Jackson object mapper available from custom deserializers (#129) #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ public class JacksonJsonpMapper extends JsonpMapperBase {
private final ObjectMapper objectMapper;

public JacksonJsonpMapper(ObjectMapper objectMapper) {
this.provider = new JacksonJsonProvider();
this.objectMapper = objectMapper
.configure(SerializationFeature.INDENT_OUTPUT, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// Creating the json factory from the mapper ensures it will be returned by JsonParser.getCodec()
this.provider = new JacksonJsonProvider(this.objectMapper.getFactory());
}

public JacksonJsonpMapper() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package co.elastic.clients.elasticsearch.json.jackson;

import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.model.ModelTestCase;
import co.elastic.clients.json.JsonpDeserializer;
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.junit.Test;

import java.io.IOException;

public class JacksonMapperTest extends ModelTestCase {

@Test
public void testCustomDeserializer() {
// See https://github.com/elastic/elasticsearch-java/issues/120
JsonpMapper jsonpMapper = new JacksonJsonpMapper();

String json = "{\"_index\":\"foo\",\"_id\":\"1\",\"_source\":{\"model\":\"Foo\",\"age\":42}}";

Hit<TestData> testDataHit = fromJson(json,
Hit.createHitDeserializer(JsonpDeserializer.of(TestData.class)),
jsonpMapper
);
TestData data = testDataHit.source();
assertEquals("Foo", data.theModel);
assertEquals(42, data.theAge);
}

@JsonDeserialize(using = TestData.TestDeserializer.class)
public static class TestData {
public String theModel;
public int theAge;

public static class TestDeserializer extends JsonDeserializer<TestData> {

@Override
public TestData deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
JsonNode node = jp.getCodec().readTree(jp);

TestData res = new TestData();
if (node.has("age")) {
res.theAge = node.get("age").asInt();
}
if (node.has("model")) {
res.theModel = node.get("model").asText();
}
return res;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,15 @@ protected <T> T checkJsonRoundtrip(T value, String expectedJson) {
}

protected <T> T fromJson(String json, JsonpDeserializer<T> deserializer) {
return fromJson(json, deserializer, mapper);
}

protected <T> T fromJson(String json, JsonpDeserializer<T> deserializer, JsonpMapper mapper) {
JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
return deserializer.deserialize(parser, mapper);
}


public static void assertGetterType(Class<?> expected, Class<?> clazz, String name) {
Method method;
try {
Expand Down