diff --git a/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonMapper.java b/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonMapper.java index 2cbd964a4..5a027ff96 100644 --- a/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonMapper.java +++ b/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonMapper.java @@ -1,5 +1,9 @@ package io.ebean.core.type; +import io.avaje.lang.Nullable; + +import java.lang.annotation.Annotation; + /** * Supports JSON bean properties that use something like Jackson ObjectMapper to convert between * JSON content and the logical bean property value. @@ -10,4 +14,10 @@ public interface ScalarJsonMapper { * Return a ScalarType for the given JSON property. */ ScalarType createType(ScalarJsonRequest request); + + /** + * Return a marker annotation to detect when the JSON mapper should be used explicitly. + */ + @Nullable + Class markerAnnotation(); } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java index be3a25609..d122ebd06 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java @@ -297,12 +297,17 @@ public final class DefaultTypeManager implements TypeManager { @Override public ScalarType dbJsonType(DeployBeanProperty prop, int dbType, int dbLength) { Class type = prop.getPropertyType(); - Type genericType = prop.getGenericType(); - boolean hasJacksonAnnotations = objectMapperPresent && checkJacksonAnnotations(prop); - if (type.equals(String.class)) { return ScalarTypeJsonString.typeFor(postgres, dbType); } + boolean hasJacksonAnnotations = false; + if (jsonMapper != null) { + var markerAnnotation = jsonMapper.markerAnnotation(); + if (markerAnnotation != null) { + hasJacksonAnnotations = !prop.getMetaAnnotations(markerAnnotation).isEmpty(); + } + } + Type genericType = prop.getGenericType(); if (type.equals(List.class)) { DocPropertyType docType = docType(genericType); if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) { @@ -342,14 +347,6 @@ public final class DefaultTypeManager implements TypeManager { return prop.getMutationDetection() == MutationDetection.SOURCE; } - /** - * Returns TRUE, if there is any jackson annotation on that property. All jackson annotations - * are annotated with the @JacksonAnnotation meta annotation. So detection is easy. - */ - private boolean checkJacksonAnnotations(DeployBeanProperty prop) { - return prop.getMetaAnnotation(com.fasterxml.jackson.annotation.JacksonAnnotation.class) != null; - } - private DocPropertyType docType(Type genericType) { if (genericType instanceof Class) { ScalarType found = type((Class) genericType); diff --git a/ebean-jackson-mapper/src/main/java/io/ebean/jackson/mapper/ScalarJsonJacksonMapper.java b/ebean-jackson-mapper/src/main/java/io/ebean/jackson/mapper/ScalarJsonJacksonMapper.java index a005c5e2d..a4dfbac9a 100644 --- a/ebean-jackson-mapper/src/main/java/io/ebean/jackson/mapper/ScalarJsonJacksonMapper.java +++ b/ebean-jackson-mapper/src/main/java/io/ebean/jackson/mapper/ScalarJsonJacksonMapper.java @@ -16,6 +16,7 @@ import javax.persistence.PersistenceException; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; +import java.lang.annotation.Annotation; import java.sql.SQLException; import java.sql.Types; import java.util.Map; @@ -28,6 +29,12 @@ public final class ScalarJsonJacksonMapper implements ScalarJsonMapper { private final Map, AnnotatedClass> jacksonClasses = new ConcurrentHashMap<>(); + @SuppressWarnings("unchecked") + @Override + public Class markerAnnotation() { + return (Class)com.fasterxml.jackson.annotation.JacksonAnnotation.class; + } + /** * Create and return the appropriate ScalarType. */