-
-
Save schaloner/50c308fa62a2e19fdad3ed468294881e to your computer and use it in GitHub Desktop.
<?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 ([email protected]) | |
*/ | |
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 could you share the Jackson version you wrote?
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 ๐
@vijayparashar12 could you share it ?
@diranl @ccjmne @anishpr9 here you go https://gist.github.com/vijayparashar12/e1b61543ace2debb20b6a302fa408eaf
apologies for late response.
@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!
well, It's not purely Jackson as it uses Playjson, but it helped me to write the pure Jackson version, Thanks for sharing ...