@Converter(autoApply = true)
public final class StringToMapConverter implements AttributeConverter<Map<String, ? extends Object>, String> {
private static final Coder coder = new StandardCoder();
@Override
public String convertToDatabaseColumn(Map<String, ? extends Object> map) {
try {
return map == null ? null : coder.encode(map);
} catch (CoderException e) {
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e);
}
}
@Override
public Map<String, ? extends Object> convertToEntityAttribute(String dbData) {
if (dbData == null) {
return Map.of();
}
try {
return coder.decode(dbData, Map.class);
} catch (CoderException e) {
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e);
}
}
}
|