Skip to content

Commit

Permalink
Fix an issue with JsonLDConverter. Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Konstantinov authored and vanxa committed Feb 17, 2025
1 parent c1594fc commit 2339124
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/main/java/com/ontotext/kafka/convert/JsonDataConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.eclipse.rdf4j.rio.RDFFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

public final class JsonDataConverter extends RdfFormatConverter {
Expand All @@ -18,6 +20,7 @@ public JsonDataConverter(RDFFormat inputFormat) {
this.objectMapper = new ObjectMapper();
}


public <T> T convert(byte[] data, Class<T> cls) {
try {
byte[] converted = convertData(data);
Expand All @@ -33,12 +36,23 @@ public <T> T convert(byte[] data, Class<T> cls) {
}

public Map<String, Object> convert(byte[] data) {
byte[] converted = null;
try {
byte[] converted = convertData(data);
return objectMapper.readValue(converted, new TypeReference<>() {
converted = convertData(data);
ArrayList<Map<String, Object>> res = objectMapper.readValue(converted, new TypeReference<>() {
});
if (CollectionUtils.isNotEmpty(res)) {
return res.get(0);
}
} catch (DatabindException e) {
log.error("Could not deserialize data to a generic map", e);
// Just in case try to deserialize straight to a map without the outer list wrapper.
log.error("Could not deserialize data to a list of items. Will repeat and try to deserialize as a map instead.");
try {
return objectMapper.readValue(converted, new TypeReference<>() {
});
} catch (Exception e1) {
log.error("Could not deserialize data to a map. Cannot proceed", e);
}
} catch (StreamReadException e) {
log.error("Caught exception while reading data stream", e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ontotext.kafka.convert;

import org.eclipse.rdf4j.rio.RDFFormat;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

class JsonDataConverterTest {


@Test
void test_convertToMap_ok() {
String ttl = "<urn:a> <urn:b> <urn:c> .";
JsonDataConverter converter = new JsonDataConverter(RDFFormat.TURTLE);
Map<String, Object> data = converter.convert(ttl.getBytes(StandardCharsets.UTF_8));
assertThat(data).isNotNull();
assertThat(data).containsEntry("@id", "urn:a");
assertThat(data).containsEntry("urn:b", Collections.singletonList(Collections.singletonMap("@id", "urn:c")));
}
}

0 comments on commit 2339124

Please sign in to comment.