mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
Extract ebean-jackson-mapper module - rename ScalarTypeJsonObjectMapper to ScalarJsonJacksonMapper and move
This commit is contained in:
1 parent
d5f753d110
commit
e74e4ba532
15 files changed
+103
-12
No files matched your search
@@ -72,6 +72,7 @@ public final class DefaultTypeManager implements TypeManager {
|
||||
|
||||
private final PlatformArrayTypeFactory arrayTypeListFactory;
|
||||
private final PlatformArrayTypeFactory arrayTypeSetFactory;
|
||||
private final ScalarJsonMapper jsonMapper;
|
||||
private GeoTypeBinder geoTypeBinder;
|
||||
|
||||
/**
|
||||
@@ -94,6 +95,9 @@ public final class DefaultTypeManager implements TypeManager {
|
||||
this.offlineMigrationGeneration = DbOffline.isGenerateMigration();
|
||||
this.defaultEnumType = config.getDefaultEnumType();
|
||||
|
||||
ServiceLoader<ScalarJsonMapper> mappers = ServiceLoader.load(ScalarJsonMapper.class);
|
||||
jsonMapper = mappers.findFirst().orElse(null);
|
||||
|
||||
initialiseStandard(config);
|
||||
initialiseJavaTimeTypes(config);
|
||||
loadTypesFromProviders(config, objectMapper);
|
||||
@@ -377,14 +381,14 @@ public final class DefaultTypeManager implements TypeManager {
|
||||
}
|
||||
|
||||
private ScalarType<?> createJsonObjectMapperType(DeployBeanProperty prop, int dbType, DocPropertyType docType) {
|
||||
if (objectMapper == null) {
|
||||
if (jsonMapper == null) {
|
||||
throw new IllegalArgumentException("Unsupported @DbJson mapping - Jackson ObjectMapper not present for " + prop);
|
||||
}
|
||||
if (MutationDetection.DEFAULT == prop.getMutationDetection()) {
|
||||
prop.setMutationDetection(jsonManager.mutationDetection());
|
||||
}
|
||||
var req = new ScalarJsonRequest(jsonManager, dbType, docType, prop.getDesc().getBeanType(), prop.getMutationDetection(), prop.getName());
|
||||
return ScalarTypeJsonObjectMapper.createTypeFor(req);
|
||||
return jsonMapper.createType(req);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedClassResolver;
|
||||
|
||||
/**
|
||||
* Used to obtain the Jackson AnnotatedClass for a given bean type utlimately to obtain field level Jackson annotations.
|
||||
*/
|
||||
final class DeployBeanObtainJackson<T> {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final Class<T> beanType;
|
||||
|
||||
DeployBeanObtainJackson(ObjectMapper objectMapper, Class<T> beanType) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.beanType = beanType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Jackson AnnotatedClass for the given bean type.
|
||||
*/
|
||||
AnnotatedClass obtain() {
|
||||
JavaType javaType = objectMapper.getTypeFactory().constructType(beanType);
|
||||
return AnnotatedClassResolver.resolve(objectMapper.getDeserializationConfig(), javaType, objectMapper.getDeserializationConfig());
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
|
||||
|
||||
final class JacksonTypeHelper {
|
||||
|
||||
private final AnnotatedField field;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final JavaType javaType;
|
||||
private final DeserializationConfig deserConfig;
|
||||
private final AnnotationIntrospector ai;
|
||||
|
||||
JacksonTypeHelper(AnnotatedField field, ObjectMapper objectMapper) {
|
||||
this.field = field;
|
||||
this.objectMapper = objectMapper;
|
||||
this.javaType = field.getType();
|
||||
this.deserConfig = objectMapper.getDeserializationConfig();
|
||||
this.ai = deserConfig.getAnnotationIntrospector();
|
||||
}
|
||||
|
||||
JavaType type() {
|
||||
if (ai == null || javaType == null || javaType.hasRawClass(Object.class)) {
|
||||
return javaType;
|
||||
} else {
|
||||
try {
|
||||
return ai.refineDeserializationType(deserConfig, field, javaType);
|
||||
} catch (JsonMappingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ObjectWriter objectWriter() {
|
||||
if (ai == null || javaType == null || javaType.hasRawClass(Object.class)) {
|
||||
return objectMapper.writerFor(javaType);
|
||||
} else {
|
||||
try {
|
||||
JavaType serType = ai.refineSerializationType(objectMapper.getSerializationConfig(), field, javaType);
|
||||
return objectMapper.writerFor(serType);
|
||||
} catch (JsonMappingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
/**
|
||||
* Helper that removes whitespace from JSON. Used to normalise Postgres JSONB content.
|
||||
*/
|
||||
final class JsonTrim {
|
||||
|
||||
/**
|
||||
* Return JSON with whitespace trimmed.
|
||||
*/
|
||||
static String trim(String json) {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
int len = json.length();
|
||||
StringBuilder builder = new StringBuilder(len);
|
||||
boolean escaped = false;
|
||||
boolean quoted = false;
|
||||
for (int i = 0; i < len; i++) {
|
||||
char c = json.charAt(i);
|
||||
if (c == '\"') {
|
||||
if (!escaped) {
|
||||
quoted = !quoted;
|
||||
} else {
|
||||
escaped = false;
|
||||
}
|
||||
} else if (escaped) {
|
||||
escaped = false;
|
||||
} else if (quoted && c == '\\') {
|
||||
escaped = true;
|
||||
}
|
||||
if (quoted || !Character.isWhitespace(c)) {
|
||||
builder.append(c);
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
-244
@@ -1,244 +0,0 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
|
||||
import io.ebean.annotation.MutationDetection;
|
||||
import io.ebean.core.type.*;
|
||||
import io.ebean.text.TextException;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Supports @DbJson properties using Jackson ObjectMapper.
|
||||
*/
|
||||
final class ScalarTypeJsonObjectMapper {
|
||||
|
||||
static final Map<Class<?>, AnnotatedClass> jacksonClasses = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Create and return the appropriate ScalarType.
|
||||
*/
|
||||
static ScalarType<?> createTypeFor(ScalarJsonRequest req) {
|
||||
AnnotatedClass annotatedClass = annotatedClass(req);
|
||||
AnnotatedField field = annotatedField(annotatedClass, req);
|
||||
if (req.mode() == MutationDetection.NONE) {
|
||||
return new NoMutationDetection(req.manager(), field, req.dbType(), req.docType());
|
||||
} else {
|
||||
return new GenericObject(req.manager(), field, req.dbType(), req.docType());
|
||||
}
|
||||
}
|
||||
|
||||
private static AnnotatedField annotatedField(AnnotatedClass annotatedClass, ScalarJsonRequest req) {
|
||||
for (AnnotatedField field : annotatedClass.fields()) {
|
||||
if (field.getName().equals(req.name())) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("AnnotatedField not found to match " + req.name());
|
||||
}
|
||||
|
||||
private static AnnotatedClass annotatedClass(ScalarJsonRequest req) {
|
||||
return jacksonClasses.computeIfAbsent(req.beanType(), key -> {
|
||||
ObjectMapper mapper = (ObjectMapper) req.manager().mapper();
|
||||
return new DeployBeanObtainJackson(mapper, req.beanType()).obtain();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* No mutation detection on this json property.
|
||||
*/
|
||||
private static final class NoMutationDetection extends Base<Object> {
|
||||
|
||||
NoMutationDetection(ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
|
||||
super(Object.class, jsonManager, field, dbType, docType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Supports HASH and SOURCE dirty detection modes.
|
||||
*/
|
||||
private static final class GenericObject extends Base<Object> {
|
||||
|
||||
private final boolean jsonb;
|
||||
|
||||
GenericObject(ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
|
||||
super(Object.class, jsonManager, field, dbType, docType);
|
||||
this.jsonb = "jsonb".equals(pgType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mutable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean jsonMapper() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(DataReader reader) throws SQLException {
|
||||
String json = reader.getString();
|
||||
if (jsonb) {
|
||||
json = JsonTrim.trim(json);
|
||||
}
|
||||
// pushJson such that we MD5 and store on EntityBeanIntercept later
|
||||
reader.pushJson(json);
|
||||
if (json == null || json.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return objectReader.readValue(json, deserType);
|
||||
} catch (IOException e) {
|
||||
throw new TextException("Failed to parse JSON [{}] as " + deserType, json, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBinder binder, Object value) throws SQLException {
|
||||
// popJson as dirty detection already converted to json string
|
||||
String rawJson = binder.popJson();
|
||||
if (rawJson == null && value != null) {
|
||||
rawJson = formatValue(value); // not expected, need to check?
|
||||
}
|
||||
if (pgType != null) {
|
||||
binder.setObject(PostgresHelper.asObject(pgType, rawJson));
|
||||
} else {
|
||||
if (value == null) {
|
||||
// use varchar, otherwise SqlServer/db2 will fail with 'Invalid JDBC data type 5.001.'
|
||||
binder.setNull(Types.VARCHAR);
|
||||
} else {
|
||||
binder.setString(rawJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ScalarType that uses Jackson ObjectMapper to marshall/unmarshall to/from JSON
|
||||
* and storing them in one of JSON, JSONB, VARCHAR, CLOB or BLOB.
|
||||
*/
|
||||
private static abstract class Base<T> extends ScalarTypeBase<T> {
|
||||
|
||||
protected final ObjectWriter objectWriter;
|
||||
protected final ObjectMapper objectReader;
|
||||
protected final JavaType deserType;
|
||||
protected final String pgType;
|
||||
private final DocPropertyType docType;
|
||||
|
||||
Base(Class<T> cls, ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
|
||||
super(cls, false, dbType);
|
||||
this.objectReader = (ObjectMapper)jsonManager.mapper();
|
||||
this.pgType = jsonManager.postgresType(dbType);
|
||||
this.docType = docType;
|
||||
final JacksonTypeHelper helper = new JacksonTypeHelper(field, objectReader);
|
||||
this.deserType = helper.type();
|
||||
this.objectWriter = helper.objectWriter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T read(DataReader reader) throws SQLException {
|
||||
String json = reader.getString();
|
||||
if (json == null || json.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return objectReader.readValue(json, deserType);
|
||||
} catch (IOException e) {
|
||||
throw new TextException("Failed to parse JSON [{}] as " + deserType, json, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBinder binder, T value) throws SQLException {
|
||||
if (pgType != null) {
|
||||
String rawJson = (value == null) ? null : formatValue(value);
|
||||
binder.setObject(PostgresHelper.asObject(pgType, rawJson));
|
||||
} else {
|
||||
if (value == null) {
|
||||
binder.setNull(Types.VARCHAR); // use varchar, otherwise SqlServer/db2 will fail with 'Invalid JDBC data type 5.001.'
|
||||
} else {
|
||||
binder.setString(formatValue(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Object toJdbcType(Object value) {
|
||||
// no type conversion supported
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public final T toBeanType(Object value) {
|
||||
// no type conversion supported
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String formatValue(T value) {
|
||||
try {
|
||||
return objectWriter.writeValueAsString(value);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new PersistenceException("Unable to create JSON", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T parse(String value) {
|
||||
try {
|
||||
return objectReader.readValue(value, deserType);
|
||||
} catch (IOException e) {
|
||||
throw new TextException("Failed to parse JSON [{}] as " + deserType, value, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final DocPropertyType docType() {
|
||||
return docType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T jsonRead(JsonParser parser) throws IOException {
|
||||
return objectReader.readValue(parser, deserType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void jsonWrite(JsonGenerator writer, T value) throws IOException {
|
||||
objectWriter.writeValue(writer, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
return parse(dataInput.readUTF());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeData(DataOutput dataOutput, T value) throws IOException {
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeUTF(format(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ module io.ebean.core {
|
||||
uses io.ebean.datasource.DataSourceAlertFactory;
|
||||
uses io.ebean.core.type.ExtraTypeFactory;
|
||||
uses io.ebean.core.type.ScalarTypeSetFactory;
|
||||
uses io.ebean.core.type.ScalarJsonMapper;
|
||||
uses io.ebeanservice.docstore.api.DocStoreFactory;
|
||||
uses io.ebean.migration.auto.AutoMigrationRunner;
|
||||
uses io.avaje.classpath.scanner.ClassPathScannerFactory;
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class JsonTrimTest {
|
||||
|
||||
@Test
|
||||
void trim_array() {
|
||||
final String trimmed = JsonTrim.trim("[{\"name\": \"one\", \"along\": 1, \"timestamp\": 1629609021559}, {\"name\": \"two\", \"along\": 2, \"timestamp\": 1629609021559}]");
|
||||
assertThat(trimmed).isEqualTo("[{\"name\":\"one\",\"along\":1,\"timestamp\":1629609021559},{\"name\":\"two\",\"along\":2,\"timestamp\":1629609021559}]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_object() {
|
||||
final String trimmed = JsonTrim.trim("{\"name\": \"one\",\t \t \"along\": 1, \"timestamp\": 1629609021559}");
|
||||
assertThat(trimmed).isEqualTo("{\"name\":\"one\",\"along\":1,\"timestamp\":1629609021559}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_object_embeddedEscapeSlash() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\nf\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\nf\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_escapedTabNewLine() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\t\\nf\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\t\\nf\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_leadingEscapedNewLine() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"\\no\\t\\nf\\n\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"\\no\\t\\nf\\n\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_trailingEscapedNewLine() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\t\\nf\\n\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\t\\nf\\n\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_trailingEscapedSlash() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\t\\nf\\\\\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\t\\nf\\\\\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_object_embeddedEscaped() {
|
||||
final String trimmed = JsonTrim.trim("{\"name\": \"one\nfoo\nbar\\bazz\tboo\",\t \t \n \"along\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"name\":\"one\nfoo\nbar\\bazz\tboo\",\"along\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_escaped() {
|
||||
final String trimmed = JsonTrim.trim(" \t \n {\"a\": \n \"\\t1\\t2\\n3\\\\\",\t \n \"b\": \"\\t1\\t2\\n3\\\\\" , \t \n \"c\": \"\\t1\\t2\\n3\\\\\" \t \n }");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"\\t1\\t2\\n3\\\\\",\"b\":\"\\t1\\t2\\n3\\\\\",\"c\":\"\\t1\\t2\\n3\\\\\"}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user