mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
Merge pull request #2828 from ebean-orm/feature/jackson-mapper-module
Extract jackson mapper module
This commit is contained in:
20 files changed
+288
-116
No files matched your search
@@ -37,6 +37,12 @@
|
|||||||
<version>13.9.3-SNAPSHOT</version>
|
<version>13.9.3-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.ebean</groupId>
|
||||||
|
<artifactId>ebean-jackson-mapper</artifactId>
|
||||||
|
<version>13.9.3-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ebean</groupId>
|
<groupId>io.ebean</groupId>
|
||||||
<artifactId>ebean-datasource</artifactId>
|
<artifactId>ebean-datasource</artifactId>
|
||||||
|
|||||||
@@ -122,6 +122,12 @@
|
|||||||
<version>13.9.3-SNAPSHOT</version>
|
<version>13.9.3-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.ebean</groupId>
|
||||||
|
<artifactId>ebean-jackson-mapper</artifactId>
|
||||||
|
<version>13.9.3-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ebean</groupId>
|
<groupId>io.ebean</groupId>
|
||||||
<artifactId>ebean-ddl-generator</artifactId>
|
<artifactId>ebean-ddl-generator</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package io.ebean.core.type;
|
||||||
|
|
||||||
|
import io.ebean.annotation.MutationDetection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds the configured mapper and default mutation detection mode to be used.
|
||||||
|
*/
|
||||||
|
public interface ScalarJsonManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the default mutation detection mode.
|
||||||
|
*/
|
||||||
|
MutationDetection mutationDetection();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the object mapper supplied in DatabaseConfig.
|
||||||
|
*/
|
||||||
|
Object mapper();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the Postgres type given the jdbc type.
|
||||||
|
*/
|
||||||
|
String postgresType(int dbType);
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
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.
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
<A extends Annotation> Class<A> markerAnnotation();
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package io.ebean.core.type;
|
||||||
|
|
||||||
|
import io.ebean.annotation.MutationDetection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A request to create a ScalarType for a bean property (typically annotated with {@code @DbJson}).
|
||||||
|
*/
|
||||||
|
public final class ScalarJsonRequest {
|
||||||
|
|
||||||
|
private final ScalarJsonManager manager;
|
||||||
|
private final int dbType;
|
||||||
|
private final DocPropertyType docType;
|
||||||
|
private final Class<?> beanType;
|
||||||
|
private final MutationDetection mode;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public ScalarJsonRequest(ScalarJsonManager manager, int dbType, DocPropertyType docType, Class<?> beanType, MutationDetection mode, String name) {
|
||||||
|
this.manager = manager;
|
||||||
|
this.dbType = dbType;
|
||||||
|
this.docType = docType;
|
||||||
|
this.beanType = beanType;
|
||||||
|
this.mode = mode;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the manager.
|
||||||
|
*/
|
||||||
|
public ScalarJsonManager manager() {
|
||||||
|
return manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the jdbc type this property maps to (e.g. JSON, JSONB etc).
|
||||||
|
*/
|
||||||
|
public int dbType() {
|
||||||
|
return dbType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the document store docType.
|
||||||
|
*/
|
||||||
|
public DocPropertyType docType() {
|
||||||
|
return docType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the type of the bean that this property belongs to.
|
||||||
|
*/
|
||||||
|
public Class<?> beanType() {
|
||||||
|
return beanType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the mutation detection mode the property should use.
|
||||||
|
*/
|
||||||
|
public MutationDetection mode() {
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the property name.
|
||||||
|
*/
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
-11
@@ -152,7 +152,6 @@ public class DeployBeanDescriptor<T> {
|
|||||||
private DocStoreMode docStoreDelete;
|
private DocStoreMode docStoreDelete;
|
||||||
private DeployBeanProperty idProperty;
|
private DeployBeanProperty idProperty;
|
||||||
private TableJoin primaryKeyJoin;
|
private TableJoin primaryKeyJoin;
|
||||||
private Object jacksonAnnotatedClass;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct the BeanDescriptor.
|
* Construct the BeanDescriptor.
|
||||||
@@ -1130,14 +1129,4 @@ public class DeployBeanDescriptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the jackson annotated class, if jackson is present.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Object /*AnnotatedClass*/ getJacksonAnnotatedClass() {
|
|
||||||
if (jacksonAnnotatedClass == null) {
|
|
||||||
jacksonAnnotatedClass = new DeployBeanObtainJackson(config, beanType).obtain();
|
|
||||||
}
|
|
||||||
return jacksonAnnotatedClass;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1042,20 +1042,6 @@ public class DeployBeanProperty {
|
|||||||
this.elementProperty = true;
|
this.elementProperty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the jackson annotated field, if jackson is present.
|
|
||||||
*/
|
|
||||||
public Object /*AnnotatedField*/ getJacksonField() {
|
|
||||||
com.fasterxml.jackson.databind.introspect.AnnotatedClass jac =
|
|
||||||
(com.fasterxml.jackson.databind.introspect.AnnotatedClass) getDesc().getJacksonAnnotatedClass();
|
|
||||||
for (com.fasterxml.jackson.databind.introspect.AnnotatedField candidate : jac.fields()) {
|
|
||||||
if (candidate.getName().equals(getName())) {
|
|
||||||
return candidate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initMetaAnnotations(Set<Class<?>> metaAnnotationsFilter) {
|
public void initMetaAnnotations(Set<Class<?>> metaAnnotationsFilter) {
|
||||||
metaAnnotations = AnnotationUtil.metaFindAllFor(field, metaAnnotationsFilter);
|
metaAnnotations = AnnotationUtil.metaFindAllFor(field, metaAnnotationsFilter);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,13 +65,14 @@ public final class DefaultTypeManager implements TypeManager {
|
|||||||
private final Object objectMapper;
|
private final Object objectMapper;
|
||||||
private final boolean objectMapperPresent;
|
private final boolean objectMapperPresent;
|
||||||
private final boolean postgres;
|
private final boolean postgres;
|
||||||
private final TypeJsonManager jsonManager;
|
private final ScalarJsonManager jsonManager;
|
||||||
private final boolean offlineMigrationGeneration;
|
private final boolean offlineMigrationGeneration;
|
||||||
private final EnumType defaultEnumType;
|
private final EnumType defaultEnumType;
|
||||||
private final DatabasePlatform databasePlatform;
|
private final DatabasePlatform databasePlatform;
|
||||||
|
|
||||||
private final PlatformArrayTypeFactory arrayTypeListFactory;
|
private final PlatformArrayTypeFactory arrayTypeListFactory;
|
||||||
private final PlatformArrayTypeFactory arrayTypeSetFactory;
|
private final PlatformArrayTypeFactory arrayTypeSetFactory;
|
||||||
|
private final ScalarJsonMapper jsonMapper;
|
||||||
private GeoTypeBinder geoTypeBinder;
|
private GeoTypeBinder geoTypeBinder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -94,6 +95,9 @@ public final class DefaultTypeManager implements TypeManager {
|
|||||||
this.offlineMigrationGeneration = DbOffline.isGenerateMigration();
|
this.offlineMigrationGeneration = DbOffline.isGenerateMigration();
|
||||||
this.defaultEnumType = config.getDefaultEnumType();
|
this.defaultEnumType = config.getDefaultEnumType();
|
||||||
|
|
||||||
|
ServiceLoader<ScalarJsonMapper> mappers = ServiceLoader.load(ScalarJsonMapper.class);
|
||||||
|
jsonMapper = mappers.findFirst().orElse(null);
|
||||||
|
|
||||||
initialiseStandard(config);
|
initialiseStandard(config);
|
||||||
initialiseJavaTimeTypes(config);
|
initialiseJavaTimeTypes(config);
|
||||||
loadTypesFromProviders(config, objectMapper);
|
loadTypesFromProviders(config, objectMapper);
|
||||||
@@ -293,34 +297,24 @@ public final class DefaultTypeManager implements TypeManager {
|
|||||||
@Override
|
@Override
|
||||||
public ScalarType<?> dbJsonType(DeployBeanProperty prop, int dbType, int dbLength) {
|
public ScalarType<?> dbJsonType(DeployBeanProperty prop, int dbType, int dbLength) {
|
||||||
Class<?> type = prop.getPropertyType();
|
Class<?> type = prop.getPropertyType();
|
||||||
Type genericType = prop.getGenericType();
|
|
||||||
boolean hasJacksonAnnotations = objectMapperPresent && checkJacksonAnnotations(prop);
|
|
||||||
|
|
||||||
if (type.equals(String.class)) {
|
if (type.equals(String.class)) {
|
||||||
return ScalarTypeJsonString.typeFor(postgres, dbType);
|
return ScalarTypeJsonString.typeFor(postgres, dbType);
|
||||||
}
|
}
|
||||||
if (type.equals(List.class)) {
|
if (jsonMapper != null) {
|
||||||
DocPropertyType docType = docType(genericType);
|
var markerAnnotation = jsonMapper.markerAnnotation();
|
||||||
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
|
if (markerAnnotation != null && !prop.getMetaAnnotations(markerAnnotation).isEmpty()) {
|
||||||
return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop));
|
return createJsonObjectMapperType(prop, dbType, docPropertyType(prop, type));
|
||||||
} else {
|
|
||||||
return createJsonObjectMapperType(prop, dbType, docType);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (type.equals(Set.class)) {
|
Type genericType = prop.getGenericType();
|
||||||
DocPropertyType docType = docType(genericType);
|
if (type.equals(List.class) && isValueTypeSimple(genericType)) {
|
||||||
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
|
return ScalarTypeJsonList.typeFor(postgres, dbType, docType(genericType), prop.isNullable(), keepSource(prop));
|
||||||
return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop));
|
|
||||||
} else {
|
|
||||||
return createJsonObjectMapperType(prop, dbType, docType);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (type.equals(Map.class)) {
|
if (type.equals(Set.class) && isValueTypeSimple(genericType)) {
|
||||||
if (!hasJacksonAnnotations && isMapValueTypeObject(genericType)) {
|
return ScalarTypeJsonSet.typeFor(postgres, dbType, docType(genericType), prop.isNullable(), keepSource(prop));
|
||||||
return ScalarTypeJsonMap.typeFor(postgres, dbType, jsonManager.keepSource(prop));
|
}
|
||||||
} else {
|
if (type.equals(Map.class) && isMapValueTypeObject(genericType)) {
|
||||||
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
|
return ScalarTypeJsonMap.typeFor(postgres, dbType, keepSource(prop));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (objectMapperPresent && prop.getMutationDetection() == MutationDetection.DEFAULT) {
|
if (objectMapperPresent && prop.getMutationDetection() == MutationDetection.DEFAULT) {
|
||||||
ScalarTypeSet<?> typeSet = typeSets.get(type);
|
ScalarTypeSet<?> typeSet = typeSets.get(type);
|
||||||
@@ -331,12 +325,15 @@ public final class DefaultTypeManager implements TypeManager {
|
|||||||
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
|
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private boolean keepSource(DeployBeanProperty prop) {
|
||||||
* Returns TRUE, if there is any jackson annotation on that property. All jackson annotations
|
if (prop.getMutationDetection() == MutationDetection.DEFAULT) {
|
||||||
* are annotated with the @JacksonAnnotation meta annotation. So detection is easy.
|
prop.setMutationDetection(jsonManager.mutationDetection());
|
||||||
*/
|
}
|
||||||
private boolean checkJacksonAnnotations(DeployBeanProperty prop) {
|
return prop.getMutationDetection() == MutationDetection.SOURCE;
|
||||||
return prop.getMetaAnnotation(com.fasterxml.jackson.annotation.JacksonAnnotation.class) != null;
|
}
|
||||||
|
|
||||||
|
private DocPropertyType docPropertyType(DeployBeanProperty prop, Class<?> type) {
|
||||||
|
return type.equals(List.class) || type.equals(Set.class) ? docType(prop.getGenericType()) : DocPropertyType.OBJECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DocPropertyType docType(Type genericType) {
|
private DocPropertyType docType(Type genericType) {
|
||||||
@@ -370,11 +367,14 @@ public final class DefaultTypeManager implements TypeManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ScalarType<?> createJsonObjectMapperType(DeployBeanProperty prop, int dbType, DocPropertyType docType) {
|
private ScalarType<?> createJsonObjectMapperType(DeployBeanProperty prop, int dbType, DocPropertyType docType) {
|
||||||
Class<?> type = prop.getPropertyType();
|
if (jsonMapper == null) {
|
||||||
if (objectMapper == null) {
|
throw new IllegalArgumentException("Unsupported @DbJson mapping - Jackson ObjectMapper not present for " + prop);
|
||||||
throw new IllegalArgumentException("Type [" + type + "] unsupported for @DbJson mapping - Jackson ObjectMapper not present");
|
|
||||||
}
|
}
|
||||||
return ScalarTypeJsonObjectMapper.createTypeFor(jsonManager, prop, dbType, docType);
|
if (MutationDetection.DEFAULT == prop.getMutationDetection()) {
|
||||||
|
prop.setMutationDetection(jsonManager.mutationDetection());
|
||||||
|
}
|
||||||
|
var req = new ScalarJsonRequest(jsonManager, dbType, docType, prop.getDesc().getBeanType(), prop.getMutationDetection(), prop.getName());
|
||||||
|
return jsonMapper.createType(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,44 +1,35 @@
|
|||||||
package io.ebeaninternal.server.type;
|
package io.ebeaninternal.server.type;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import io.ebean.ModifyAwareType;
|
import io.ebean.ModifyAwareType;
|
||||||
import io.ebean.annotation.MutationDetection;
|
import io.ebean.annotation.MutationDetection;
|
||||||
import io.ebean.config.dbplatform.DbPlatformType;
|
import io.ebean.config.dbplatform.DbPlatformType;
|
||||||
import io.ebean.core.type.PostgresHelper;
|
import io.ebean.core.type.PostgresHelper;
|
||||||
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
import io.ebean.core.type.ScalarJsonManager;
|
||||||
|
|
||||||
final class TypeJsonManager {
|
final class TypeJsonManager implements ScalarJsonManager {
|
||||||
|
|
||||||
private final boolean postgres;
|
private final boolean postgres;
|
||||||
private final ObjectMapper objectMapper;
|
private final Object mapper;
|
||||||
private final MutationDetection mutationDetection;
|
private final MutationDetection mutationDetection;
|
||||||
|
|
||||||
TypeJsonManager(boolean postgres, Object objectMapper, MutationDetection mutationDetection) {
|
TypeJsonManager(boolean postgres, Object mapper, MutationDetection mutationDetection) {
|
||||||
this.postgres = postgres;
|
this.postgres = postgres;
|
||||||
this.objectMapper = (ObjectMapper) objectMapper;
|
this.mapper = mapper;
|
||||||
this.mutationDetection = mutationDetection;
|
this.mutationDetection = mutationDetection;
|
||||||
}
|
}
|
||||||
|
|
||||||
MutationDetection mutationDetection() {
|
@Override
|
||||||
|
public MutationDetection mutationDetection() {
|
||||||
return mutationDetection;
|
return mutationDetection;
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectMapper objectMapper() {
|
@Override
|
||||||
return objectMapper;
|
public Object mapper() {
|
||||||
|
return mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean keepSource(DeployBeanProperty prop) {
|
@Override
|
||||||
if (prop.getMutationDetection() == MutationDetection.SOURCE) {
|
public String postgresType(int dbType) {
|
||||||
return true;
|
|
||||||
} else if (prop.getMutationDetection() == MutationDetection.DEFAULT) {
|
|
||||||
prop.setMutationDetection(mutationDetection);
|
|
||||||
return mutationDetection == MutationDetection.SOURCE;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String postgresType(int dbType) {
|
|
||||||
if (postgres) {
|
if (postgres) {
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
case DbPlatformType.JSON:
|
case DbPlatformType.JSON:
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ module io.ebean.core {
|
|||||||
uses io.ebean.datasource.DataSourceAlertFactory;
|
uses io.ebean.datasource.DataSourceAlertFactory;
|
||||||
uses io.ebean.core.type.ExtraTypeFactory;
|
uses io.ebean.core.type.ExtraTypeFactory;
|
||||||
uses io.ebean.core.type.ScalarTypeSetFactory;
|
uses io.ebean.core.type.ScalarTypeSetFactory;
|
||||||
|
uses io.ebean.core.type.ScalarJsonMapper;
|
||||||
uses io.ebeanservice.docstore.api.DocStoreFactory;
|
uses io.ebeanservice.docstore.api.DocStoreFactory;
|
||||||
uses io.ebean.migration.auto.AutoMigrationRunner;
|
uses io.ebean.migration.auto.AutoMigrationRunner;
|
||||||
uses io.avaje.classpath.scanner.ClassPathScannerFactory;
|
uses io.avaje.classpath.scanner.ClassPathScannerFactory;
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>ebean-parent</artifactId>
|
||||||
|
<groupId>io.ebean</groupId>
|
||||||
|
<version>13.9.3-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>ebean-jackson-mapper</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.ebean</groupId>
|
||||||
|
<artifactId>ebean-core-type</artifactId>
|
||||||
|
<version>13.9.3-SNAPSHOT</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-core</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.avaje</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>1.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
+4
-13
@@ -1,28 +1,19 @@
|
|||||||
package io.ebeaninternal.server.deploy.meta;
|
package io.ebean.jackson.mapper;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JavaType;
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
|
||||||
import com.fasterxml.jackson.databind.introspect.AnnotatedClassResolver;
|
import com.fasterxml.jackson.databind.introspect.AnnotatedClassResolver;
|
||||||
import io.ebean.config.DatabaseConfig;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to obtain the Jackson AnnotatedClass for a given bean type utlimately to obtain field level Jackson annotations.
|
* Used to obtain the Jackson AnnotatedClass for a given bean type utlimately to obtain field level Jackson annotations.
|
||||||
*/
|
*/
|
||||||
final class DeployBeanObtainJackson<T> {
|
final class AnnotatedClassUtil {
|
||||||
|
|
||||||
private final DatabaseConfig config;
|
|
||||||
private final Class<T> beanType;
|
|
||||||
|
|
||||||
DeployBeanObtainJackson(DatabaseConfig config, Class<T> beanType) {
|
|
||||||
this.config = config;
|
|
||||||
this.beanType = beanType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the Jackson AnnotatedClass for the given bean type.
|
* Return the Jackson AnnotatedClass for the given bean type.
|
||||||
*/
|
*/
|
||||||
Object obtain() {
|
static AnnotatedClass obtain(ObjectMapper objectMapper, Class<?> beanType) {
|
||||||
ObjectMapper objectMapper = (ObjectMapper) config.getObjectMapper();
|
|
||||||
JavaType javaType = objectMapper.getTypeFactory().constructType(beanType);
|
JavaType javaType = objectMapper.getTypeFactory().constructType(beanType);
|
||||||
return AnnotatedClassResolver.resolve(objectMapper.getDeserializationConfig(), javaType, objectMapper.getDeserializationConfig());
|
return AnnotatedClassResolver.resolve(objectMapper.getDeserializationConfig(), javaType, objectMapper.getDeserializationConfig());
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package io.ebeaninternal.server.type;
|
package io.ebean.jackson.mapper;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.*;
|
import com.fasterxml.jackson.databind.*;
|
||||||
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
|
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package io.ebeaninternal.server.type;
|
package io.ebean.jackson.mapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper that removes whitespace from JSON. Used to normalise Postgres JSONB content.
|
* Helper that removes whitespace from JSON. Used to normalise Postgres JSONB content.
|
||||||
+43
-21
@@ -1,4 +1,4 @@
|
|||||||
package io.ebeaninternal.server.type;
|
package io.ebean.jackson.mapper;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.fasterxml.jackson.core.JsonParser;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
@@ -6,42 +6,63 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import com.fasterxml.jackson.databind.JavaType;
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||||
|
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
|
||||||
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
|
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
|
||||||
import io.ebean.annotation.MutationDetection;
|
import io.ebean.annotation.MutationDetection;
|
||||||
import io.ebean.core.type.*;
|
import io.ebean.core.type.*;
|
||||||
import io.ebean.text.TextException;
|
import io.ebean.text.TextException;
|
||||||
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
|
||||||
|
|
||||||
import javax.persistence.PersistenceException;
|
import javax.persistence.PersistenceException;
|
||||||
import java.io.DataInput;
|
import java.io.DataInput;
|
||||||
import java.io.DataOutput;
|
import java.io.DataOutput;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Types;
|
import java.sql.Types;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Supports @DbJson properties using Jackson ObjectMapper.
|
* Supports @DbJson properties using Jackson ObjectMapper.
|
||||||
*/
|
*/
|
||||||
final class ScalarTypeJsonObjectMapper {
|
public final class ScalarJsonJacksonMapper implements ScalarJsonMapper {
|
||||||
|
|
||||||
|
private final Map<Class<?>, AnnotatedClass> jacksonClasses = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public <A extends Annotation> Class<A> markerAnnotation() {
|
||||||
|
return (Class<A>)com.fasterxml.jackson.annotation.JacksonAnnotation.class;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create and return the appropriate ScalarType.
|
* Create and return the appropriate ScalarType.
|
||||||
*/
|
*/
|
||||||
static ScalarType<?> createTypeFor(TypeJsonManager jsonManager, DeployBeanProperty prop, int dbType, DocPropertyType docType) {
|
@Override
|
||||||
AnnotatedField field = (AnnotatedField) prop.getJacksonField();
|
public ScalarType<?> createType(ScalarJsonRequest req) {
|
||||||
MutationDetection mode = prop.getMutationDetection();
|
AnnotatedClass annotatedClass = annotatedClass(req);
|
||||||
if (mode == MutationDetection.NONE) {
|
AnnotatedField field = annotatedField(annotatedClass, req);
|
||||||
return new NoMutationDetection(jsonManager, field, dbType, docType);
|
if (req.mode() == MutationDetection.NONE) {
|
||||||
} else if (mode != MutationDetection.DEFAULT) {
|
return new NoMutationDetection(req.manager(), field, req.dbType(), req.docType());
|
||||||
return new GenericObject(jsonManager, field, dbType, docType);
|
} else {
|
||||||
|
return new GenericObject(req.manager(), field, req.dbType(), req.docType());
|
||||||
}
|
}
|
||||||
// using the global default MutationDetection mode (defaults to HASH)
|
}
|
||||||
final MutationDetection defaultMode = jsonManager.mutationDetection();
|
|
||||||
prop.setMutationDetection(defaultMode);
|
private AnnotatedField annotatedField(AnnotatedClass annotatedClass, ScalarJsonRequest req) {
|
||||||
if (MutationDetection.NONE == defaultMode) {
|
for (AnnotatedField field : annotatedClass.fields()) {
|
||||||
return new NoMutationDetection(jsonManager, field, dbType, docType);
|
if (field.getName().equals(req.name())) {
|
||||||
|
return field;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return new GenericObject(jsonManager, field, dbType, docType);
|
throw new IllegalStateException("AnnotatedField not found to match " + req.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
private AnnotatedClass annotatedClass(ScalarJsonRequest req) {
|
||||||
|
return jacksonClasses.computeIfAbsent(req.beanType(), key -> {
|
||||||
|
ObjectMapper mapper = (ObjectMapper) req.manager().mapper();
|
||||||
|
return AnnotatedClassUtil.obtain(mapper, req.beanType());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,7 +70,7 @@ final class ScalarTypeJsonObjectMapper {
|
|||||||
*/
|
*/
|
||||||
private static final class NoMutationDetection extends Base<Object> {
|
private static final class NoMutationDetection extends Base<Object> {
|
||||||
|
|
||||||
NoMutationDetection(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
|
NoMutationDetection(ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
|
||||||
super(Object.class, jsonManager, field, dbType, docType);
|
super(Object.class, jsonManager, field, dbType, docType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,7 +82,7 @@ final class ScalarTypeJsonObjectMapper {
|
|||||||
|
|
||||||
private final boolean jsonb;
|
private final boolean jsonb;
|
||||||
|
|
||||||
GenericObject(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
|
GenericObject(ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
|
||||||
super(Object.class, jsonManager, field, dbType, docType);
|
super(Object.class, jsonManager, field, dbType, docType);
|
||||||
this.jsonb = "jsonb".equals(pgType);
|
this.jsonb = "jsonb".equals(pgType);
|
||||||
}
|
}
|
||||||
@@ -126,9 +147,9 @@ final class ScalarTypeJsonObjectMapper {
|
|||||||
protected final String pgType;
|
protected final String pgType;
|
||||||
private final DocPropertyType docType;
|
private final DocPropertyType docType;
|
||||||
|
|
||||||
Base(Class<T> cls, TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
|
Base(Class<T> cls, ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
|
||||||
super(cls, false, dbType);
|
super(cls, false, dbType);
|
||||||
this.objectReader = jsonManager.objectMapper();
|
this.objectReader = (ObjectMapper)jsonManager.mapper();
|
||||||
this.pgType = jsonManager.postgresType(dbType);
|
this.pgType = jsonManager.postgresType(dbType);
|
||||||
this.docType = docType;
|
this.docType = docType;
|
||||||
final JacksonTypeHelper helper = new JacksonTypeHelper(field, objectReader);
|
final JacksonTypeHelper helper = new JacksonTypeHelper(field, objectReader);
|
||||||
@@ -223,7 +244,8 @@ final class ScalarTypeJsonObjectMapper {
|
|||||||
if (value == null) {
|
if (value == null) {
|
||||||
dataOutput.writeBoolean(false);
|
dataOutput.writeBoolean(false);
|
||||||
} else {
|
} else {
|
||||||
ScalarHelp.writeUTF(dataOutput, format(value));
|
dataOutput.writeBoolean(true);
|
||||||
|
dataOutput.writeUTF(format(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import io.ebean.jackson.mapper.ScalarJsonJacksonMapper;
|
||||||
|
|
||||||
|
module io.ebean.jackson.mapper {
|
||||||
|
|
||||||
|
requires io.ebean.core.type;
|
||||||
|
requires com.fasterxml.jackson.annotation;
|
||||||
|
requires com.fasterxml.jackson.core;
|
||||||
|
requires com.fasterxml.jackson.databind;
|
||||||
|
|
||||||
|
provides io.ebean.core.type.ScalarJsonMapper with ScalarJsonJacksonMapper;
|
||||||
|
}
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
io.ebean.jackson.mapper.ScalarJsonJacksonMapper
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package io.ebeaninternal.server.type;
|
package io.ebean.jackson.mapper;
|
||||||
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -116,6 +116,13 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.ebean</groupId>
|
||||||
|
<artifactId>ebean-jackson-mapper</artifactId>
|
||||||
|
<version>13.9.3-SNAPSHOT</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ebean</groupId>
|
<groupId>io.ebean</groupId>
|
||||||
<artifactId>ebean-platform-all</artifactId>
|
<artifactId>ebean-platform-all</artifactId>
|
||||||
|
|||||||
@@ -92,6 +92,7 @@
|
|||||||
<module>ebean-joda-time</module>
|
<module>ebean-joda-time</module>
|
||||||
<module>ebean-csv-reader</module>
|
<module>ebean-csv-reader</module>
|
||||||
<module>ebean-jackson-jsonnode</module>
|
<module>ebean-jackson-jsonnode</module>
|
||||||
|
<module>ebean-jackson-mapper</module>
|
||||||
<!-- <module>ebean-kotlin</module>-->
|
<!-- <module>ebean-kotlin</module>-->
|
||||||
|
|
||||||
</modules>
|
</modules>
|
||||||
|
|||||||
Reference in new issue
Block a user