【问题标题】:Issue with JOOQ json bindingJOOQ json绑定问题
【发布时间】:2019-08-30 22:19:09
【问题描述】:

我在转换 postgresql jsonb 时遇到问题 我按照教程中的说明创建了绑定:jooq tutorial 另请注意,我没有使用 codegen 在我的仓库中,我有以下代码

Binding binding = new PostgresJSONGsonBinding();
Field<JsonElement> gsonObj = 
       DSL.field("gsonObj",SQLDataType.OTHER.asConvertedDataType(binding));

在方法和方法中出现错误

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated array at line 1 column 42 path $.factories[1]

有人可以帮助理解我做错了什么

是的,我看到了其他问题:issue with jooq

【问题讨论】:

  • 您能否说明您在 jOOQ 查询中是如何使用该字段的,以及 gsonObj 中的数据是什么样的?
  • 它已经工作了,问题出在 JsonElement 上,现在我遇到了时间戳问题,我正在使用 postgresql 并且有带 timezon 的时间戳,我实现了记录映射器,但得到了原因:org.jooq.exception。 MappingException: 找不到匹配的构造函数。 ,我应该打开新问题吗? :) @LukasEder
  • 我建议您在这里回答自己的问题,因为这可能对其他人有用,然后是的,请就您的新问题提出一个新问题。
  • 是的,添加了。

标签: java postgresql jooq


【解决方案1】:

工作代码

public class PostgresJSONGsonBinding implements Binding<Object, JsonElement> {
    // Binding <T> = Object (unknown JDBC type), and <U> = JsonElement (user type)

    private static final Logger LOGGER = LoggerFactory.getLogger(PostgresJSONGsonBinding.class);

    // The converter does all the work
    @Override
    public Converter<Object, JsonElement> converter() {
        return new Converter<Object, JsonElement>() {

            //Convert a database object to a user object
            @Override
            public JsonElement from(Object t) {
                return t == null ? JsonNull.INSTANCE : new Gson().fromJson(t.toString(), JsonElement.class);
            }

            // Convert a user object to a database object
            @Override
            public Object to(JsonElement u) {
                return u == null || u == JsonNull.INSTANCE ? null : new Gson().toJson(u);
            }

            @Override
            public Class<Object> fromType() {
                return Object.class;
            }

            @Override
            public Class<JsonElement> toType() {
                return JsonElement.class;
            }
        };
    }

    // Rending a bind variable for the binding context's value and casting it to the json type
    @Override
    public void sql(BindingSQLContext<JsonElement> ctx) throws SQLException {
        // Depending on how you generate your SQL, you may need to explicitly distinguish
        // between jOOQ generating bind variables or inlined literals.
        if (ctx.render().paramType() == ParamType.INLINED){
            ctx.render().visit(DSL.inline(ctx.convert(converter()).value())).sql("::json");
        }
        else{
            ctx.render().sql("?::json");
        }
    }

    // Registering VARCHAR types for JDBC CallableStatement OUT parameters
    @Override
    public void register(BindingRegisterContext<JsonElement> ctx) throws SQLException {
        ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR);
    }

    // Converting the JsonElement to a String value and setting that on a JDBC PreparedStatement
    @Override
    public void set(BindingSetStatementContext<JsonElement> ctx) throws SQLException {
        ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null));
    }

    // Getting a String value from a JDBC ResultSet and converting that to a JsonElement
    @Override
    public void get(BindingGetResultSetContext<JsonElement> ctx) throws SQLException {
        ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
    }

    // Getting a String value from a JDBC CallableStatement and converting that to a JsonElement
    @Override
    public void get(BindingGetStatementContext<JsonElement> ctx) throws SQLException {
        ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
    }

    // Setting a value on a JDBC SQLOutput (useful for Oracle OBJECT types)
    @Override
    public void set(BindingSetSQLOutputContext<JsonElement> ctx) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    // Getting a value from a JDBC SQLInput (useful for Oracle OBJECT types)
    @Override
    public void get(BindingGetSQLInputContext<JsonElement> ctx) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }
}

【讨论】:

    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 2017-02-18
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2013-05-15
    相关资源
    最近更新 更多