mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Rename ebean-json-node module to ebean-jackson-jsonnode
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package io.ebean.jackson.jsonnode;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.ebean.config.dbplatform.DbPlatformType;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebean.core.type.ScalarTypeSet;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
class JsonNodeSet implements ScalarTypeSet<JsonNode> {
|
||||
|
||||
final ScalarType<JsonNode> varchar;
|
||||
final ScalarType<JsonNode> clob;
|
||||
final ScalarType<JsonNode> blob;
|
||||
final ScalarType<JsonNode> jsonb;
|
||||
final ScalarType<JsonNode> json;
|
||||
|
||||
JsonNodeSet(ScalarType<JsonNode> varchar, ScalarType<JsonNode> clob, ScalarType<JsonNode> blob, ScalarType<JsonNode> jsonb, ScalarType<JsonNode> json) {
|
||||
this.varchar = varchar;
|
||||
this.clob = clob;
|
||||
this.blob = blob;
|
||||
this.jsonb = jsonb;
|
||||
this.json = json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> type() {
|
||||
return JsonNode.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScalarType<?> defaultType() {
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScalarType<JsonNode> forType(int dbType) {
|
||||
switch (dbType) {
|
||||
case Types.VARCHAR:
|
||||
return varchar;
|
||||
case Types.BLOB:
|
||||
return blob;
|
||||
case Types.CLOB:
|
||||
return clob;
|
||||
case DbPlatformType.JSONB:
|
||||
return jsonb;
|
||||
default:
|
||||
return json;
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package io.ebean.jackson.jsonnode;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.core.type.PostgresHelper;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebean.core.type.ScalarTypeSet;
|
||||
import io.ebean.core.type.ScalarTypeSetFactory;
|
||||
|
||||
public class JsonNodeTypeFactory implements ScalarTypeSetFactory {
|
||||
|
||||
@Override
|
||||
public ScalarTypeSet<?> createTypeSet(DatabaseConfig config, Object objectMapper) {
|
||||
if (objectMapper == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ObjectMapper mapper = (ObjectMapper) objectMapper;
|
||||
var varchar = new ScalarTypeJsonNode.Varchar(mapper);
|
||||
var clob = new ScalarTypeJsonNode.Clob(mapper);
|
||||
var blob = new ScalarTypeJsonNode.Blob(mapper);
|
||||
ScalarType<JsonNode> json = clob; // Default for non-Postgres databases
|
||||
ScalarType<JsonNode> jsonb = clob; // Default for non-Postgres databases
|
||||
if (PostgresHelper.isPostgresCompatible(config.getDatabasePlatform())) {
|
||||
json = new ScalarTypeJsonNodePostgres.JSON(mapper);
|
||||
jsonb = new ScalarTypeJsonNodePostgres.JSONB(mapper);
|
||||
}
|
||||
return new JsonNodeSet(varchar, clob, blob, jsonb, json);
|
||||
}
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
package io.ebean.jackson.jsonnode;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.ebean.core.type.DataBinder;
|
||||
import io.ebean.core.type.DataReader;
|
||||
import io.ebean.core.type.DocPropertyType;
|
||||
import io.ebean.core.type.ScalarTypeBase;
|
||||
import io.ebean.text.TextException;
|
||||
import io.ebean.util.IOUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
/**
|
||||
* Type which maps Jackson's JsonNode to various DB types (Clob, Varchar, Blob) in JSON format.
|
||||
*/
|
||||
abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
|
||||
|
||||
/**
|
||||
* Clob storage based implementation.
|
||||
*/
|
||||
static final class Clob extends ScalarTypeJsonNode {
|
||||
|
||||
Clob(ObjectMapper objectMapper) {
|
||||
super(objectMapper, Types.CLOB);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode read(DataReader reader) throws SQLException {
|
||||
String content = reader.getStringFromStream();
|
||||
if (content == null) {
|
||||
return null;
|
||||
}
|
||||
return parse(content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Varchar storage based implementation.
|
||||
*/
|
||||
static final class Varchar extends ScalarTypeJsonNode {
|
||||
|
||||
public Varchar(ObjectMapper objectMapper) {
|
||||
super(objectMapper, Types.VARCHAR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Blob storage based implementation.
|
||||
*/
|
||||
static class Blob extends ScalarTypeJsonNode {
|
||||
|
||||
Blob(ObjectMapper objectMapper) {
|
||||
super(objectMapper, Types.BLOB);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode read(DataReader dataReader) throws SQLException {
|
||||
InputStream is = dataReader.getBinaryStream();
|
||||
if (is == null) {
|
||||
return null;
|
||||
}
|
||||
try (Reader reader = IOUtils.newReader(is)) {
|
||||
return parse(reader);
|
||||
} catch (IOException e) {
|
||||
throw new SQLException("Error reading Blob stream from DB", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBinder binder, JsonNode value) throws SQLException {
|
||||
if (value == null) {
|
||||
binder.setNull(Types.BLOB);
|
||||
} else {
|
||||
String rawJson = formatValue(value);
|
||||
binder.setBlob(rawJson.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Jackson's ObjectMapper used to read / write JsonNode
|
||||
*/
|
||||
final ObjectMapper objectMapper;
|
||||
|
||||
ScalarTypeJsonNode(ObjectMapper objectMapper, int jdbcType) {
|
||||
super(JsonNode.class, false, jdbcType);
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map is a mutable type. Use the isDirty() method to check for dirty state.
|
||||
*/
|
||||
@Override
|
||||
public boolean mutable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the value should be considered dirty (and included in an update).
|
||||
*/
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode read(DataReader reader) throws SQLException {
|
||||
String rawJson = reader.getString();
|
||||
if (rawJson == null) {
|
||||
return null;
|
||||
}
|
||||
return parse(rawJson);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBinder binder, JsonNode value) throws SQLException {
|
||||
if (value == null) {
|
||||
binder.setNull(Types.VARCHAR);
|
||||
} else {
|
||||
String rawJson = formatValue(value);
|
||||
binder.setString(rawJson);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode toBeanType(Object value) {
|
||||
return (JsonNode) value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(JsonNode jsonNode) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(jsonNode);
|
||||
} catch (IOException e) {
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode parse(String value) {
|
||||
try {
|
||||
return objectMapper.readValue(value, JsonNode.class);
|
||||
} catch (IOException e) {
|
||||
throw new TextException("Failed to parse JSON [{}] as JsonNode", value, e);
|
||||
}
|
||||
}
|
||||
|
||||
public JsonNode parse(Reader reader) {
|
||||
try {
|
||||
return objectMapper.readValue(reader, JsonNode.class);
|
||||
} catch (IOException e) {
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
return parse(dataInput.readUTF());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, JsonNode value) throws IOException {
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeUTF(format(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator writer, JsonNode value) throws IOException {
|
||||
objectMapper.writeTree(writer, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode jsonRead(JsonParser parser) throws IOException {
|
||||
return objectMapper.readValue(parser, JsonNode.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocPropertyType docType() {
|
||||
return DocPropertyType.OBJECT;
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package io.ebean.jackson.jsonnode;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.ebean.config.dbplatform.DbPlatformType;
|
||||
import io.ebean.core.type.DataBinder;
|
||||
import io.ebean.core.type.PostgresHelper;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Support for mapping JsonNode to Postgres DB types JSON and JSONB.
|
||||
*/
|
||||
abstract class ScalarTypeJsonNodePostgres extends ScalarTypeJsonNode {
|
||||
|
||||
final ObjectMapper objectMapper;
|
||||
final String postgresType;
|
||||
|
||||
ScalarTypeJsonNodePostgres(ObjectMapper objectMapper, int jdbcType, String postgresType) {
|
||||
super(objectMapper, jdbcType);
|
||||
this.objectMapper = objectMapper;
|
||||
this.postgresType = postgresType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBinder binder, JsonNode value) throws SQLException {
|
||||
String rawJson = (value == null) ? null : formatValue(value);
|
||||
binder.setObject(PostgresHelper.asObject(postgresType, rawJson));
|
||||
}
|
||||
|
||||
/**
|
||||
* ScalarType mapping JsonNode to Postgres JSON database type.
|
||||
*/
|
||||
static final class JSON extends ScalarTypeJsonNodePostgres {
|
||||
|
||||
public JSON(ObjectMapper objectMapper) {
|
||||
super(objectMapper, DbPlatformType.JSON, PostgresHelper.JSON_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ScalarType mapping JsonNode to Postgres JSONB database type.
|
||||
*/
|
||||
static final class JSONB extends ScalarTypeJsonNodePostgres {
|
||||
|
||||
public JSONB(ObjectMapper objectMapper) {
|
||||
super(objectMapper, DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import io.ebean.jackson.jsonnode.JsonNodeTypeFactory;
|
||||
|
||||
module io.ebean.jackson.jsonnode {
|
||||
|
||||
requires io.ebean.core.type;
|
||||
requires com.fasterxml.jackson.core;
|
||||
requires com.fasterxml.jackson.databind;
|
||||
|
||||
provides io.ebean.core.type.ScalarTypeSetFactory with JsonNodeTypeFactory;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
io.ebean.jackson.jsonnode.JsonNodeTypeFactory
|
||||
Reference in New Issue
Block a user