亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

喬克 json 綁定的問題

喬克 json 綁定的問題

開心每一天1111 2022-09-28 09:43:42
我有一個問題轉換 postgresql jsonb 我創建了綁定,如教程中所述:jooq教程 另外請注意,我沒有使用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]有人可以幫助理解我做錯了什么嗎?是的,我看到了其他問題:jooq的問題
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

工作代碼


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();

    }

}


查看完整回答
反對 回復 2022-09-28
  • 1 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號