-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...ns-jdbi/src/main/java/de/digitalcollections/commons/jdbi/JsonbSetColumnMapperFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package de.digitalcollections.commons.jdbi; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import org.jdbi.v3.core.config.ConfigRegistry; | ||
import org.jdbi.v3.core.mapper.ColumnMapper; | ||
import org.jdbi.v3.core.mapper.ColumnMapperFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class JsonbSetColumnMapperFactory<T> implements ColumnMapperFactory { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(JsonbSetColumnMapperFactory.class); | ||
|
||
private final Class clz; | ||
private final ObjectMapper objectMapper; | ||
|
||
public JsonbSetColumnMapperFactory(Class<T> clz, ObjectMapper objectMapper) { | ||
this.clz = clz; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) { | ||
if (!(type instanceof ParameterizedType)) { | ||
return Optional.empty(); | ||
} | ||
|
||
Type listType = ((ParameterizedType) type).getActualTypeArguments()[0]; | ||
if (!clz.equals(listType)) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of( | ||
(r, i, c) -> { | ||
String jsonb = r.getString(i); | ||
if (jsonb == null) { | ||
return null; | ||
} | ||
/* see https://stackoverflow.com/a/11681540 */ | ||
try { | ||
JavaType javaType = | ||
objectMapper.getTypeFactory().constructParametricType(Set.class, (Class) listType); | ||
|
||
return objectMapper.readValue(jsonb, javaType); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
LOGGER.error("Error deserializing JSON", ex); | ||
return null; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
}); | ||
} | ||
} |