Skip to content

Instantly share code, notes, and snippets.

@raymondzhaoy
Forked from schaloner/JsonBinder.java
Created February 26, 2019 02:50
Show Gist options
  • Save raymondzhaoy/cfc7ef31d818f9c0f6792b32d0d7730b to your computer and use it in GitHub Desktop.
Save raymondzhaoy/cfc7ef31d818f9c0f6792b32d0d7730b to your computer and use it in GitHub Desktop.
Supporting PostgreSQL JSON types in jOOQ using Jackson. Adapted from the GSON example found at https://www.jooq.org/doc/3.9/manual/code-generation/custom-data-type-bindings/
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.9.0.xsd">
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/foo</url>
<user>foo</user>
<password>bar</password>
</jdbc>
<generator>
<name>org.jooq.util.JavaGenerator</name>
<database>
<name>org.jooq.util.postgres.PostgresDatabase</name>
<includes>.*</includes>
<excludes/>
<inputSchema>public</inputSchema>
<outputSchemaToDefault>true</outputSchemaToDefault>
<forcedTypes>
<forcedType>
<userType>com.fasterxml.jackson.databind.JsonNode</userType>
<binding>com.pledgepot.data.access.db.jooq.JsonBinder</binding>
<expression>.*JSON.*</expression>
<types>.*</types>
</forcedType>
</forcedTypes>
</database>
<target>
<packageName>com.example.jooq.generated</packageName>
<directory>modules/db-access//src/main/java</directory>
</target>
</generator>
</configuration>
package be.objectify.example.jooq;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Types;
import java.util.Objects;
import com.fasterxml.jackson.databind.JsonNode;
import org.jooq.Binding;
import org.jooq.BindingGetResultSetContext;
import org.jooq.BindingGetSQLInputContext;
import org.jooq.BindingGetStatementContext;
import org.jooq.BindingRegisterContext;
import org.jooq.BindingSQLContext;
import org.jooq.BindingSetSQLOutputContext;
import org.jooq.BindingSetStatementContext;
import org.jooq.Converter;
import org.jooq.impl.DSL;
import play.libs.Json;
/**
* @author Steve Chaloner (steve@objectify.be)
*/
public class JsonBinder implements Binding<Object, JsonNode> {
@Override
public Converter<Object, JsonNode> converter() {
return new Converter<Object, JsonNode>() {
@Override
public JsonNode from(Object t) {
return t == null ? Json.newObject() : Json.parse("" + t);
}
@Override
public Object to(JsonNode u) {
return u == null ? null : Json.stringify(u);
}
@Override
public Class<Object> fromType() {
return Object.class;
}
@Override
public Class<JsonNode> toType() {
return JsonNode.class;
}
};
}
@Override
public void sql(final BindingSQLContext<JsonNode> ctx) throws SQLException {
ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::json");
}
@Override
public void register(final BindingRegisterContext<JsonNode> ctx) throws SQLException {
ctx.statement()
.registerOutParameter(ctx.index(),
Types.VARCHAR);
}
@Override
public void set(final BindingSetStatementContext<JsonNode> ctx) throws SQLException {
ctx.statement()
.setString(ctx.index(),
Objects.toString(ctx.convert(converter()).value(),
null));
}
@Override
public void set(final BindingSetSQLOutputContext<JsonNode> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void get(final BindingGetResultSetContext<JsonNode> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
}
@Override
public void get(final BindingGetStatementContext<JsonNode> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
}
@Override
public void get(final BindingGetSQLInputContext<JsonNode> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment