Skip to content

Instantly share code, notes, and snippets.

@schaloner
Created March 31, 2017 09:55
Show Gist options
  • Save schaloner/50c308fa62a2e19fdad3ed468294881e to your computer and use it in GitHub Desktop.
Save schaloner/50c308fa62a2e19fdad3ed468294881e 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();
}
}
@vijayparashar12
Copy link

well, It's not purely Jackson as it uses Playjson, but it helped me to write the pure Jackson version, Thanks for sharing ...

@diranl
Copy link

diranl commented Oct 16, 2018

@vijayparashar12 could you share the Jackson version you wrote?

@ccjmne
Copy link

ccjmne commented Oct 20, 2018

well, It's not purely Jackson as it uses Playjson, but it helped me to write the pure Jackson version, Thanks for sharing ...

@vijayparashar12 Yeah, it'd be cool if you'd share your pure Jackson version, please 🙂

@anishpr9
Copy link

@vijayparashar12 could you share it ?

@vijayparashar12
Copy link

vijayparashar12 commented Mar 11, 2019

@ccjmne
Copy link

ccjmne commented May 1, 2019

@vijayparashar12 Thanks!
I don't think you get notified in gists even when people mention you explicitly... so the late response is entirely understandable.

I did write my own in the meantime, 'cause it wasn't actually a hard task 😆
Mine is practically identical to yours.

Thanks again for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment