乔克 json 绑定的问题

我有一个问题转换 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的问题


开心每一天1111
浏览 87回答 1
1回答

HUWWW

工作代码public class PostgresJSONGsonBinding implements Binding<Object, JsonElement> {&nbsp; &nbsp; // Binding <T> = Object (unknown JDBC type), and <U> = JsonElement (user type)&nbsp; &nbsp; private static final Logger LOGGER = LoggerFactory.getLogger(PostgresJSONGsonBinding.class);&nbsp; &nbsp; // The converter does all the work&nbsp; &nbsp; @Override&nbsp; &nbsp; public Converter<Object, JsonElement> converter() {&nbsp; &nbsp; &nbsp; &nbsp; return new Converter<Object, JsonElement>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Convert a database object to a user object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public JsonElement from(Object t) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return t == null ? JsonNull.INSTANCE : new Gson().fromJson(t.toString(), JsonElement.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Convert a user object to a database object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Object to(JsonElement u) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return u == null || u == JsonNull.INSTANCE ? null : new Gson().toJson(u);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Class<Object> fromType() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Object.class;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Class<JsonElement> toType() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return JsonElement.class;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }&nbsp; &nbsp; // Rending a bind variable for the binding context's value and casting it to the json type&nbsp; &nbsp; @Override&nbsp; &nbsp; public void sql(BindingSQLContext<JsonElement> ctx) throws SQLException {&nbsp; &nbsp; &nbsp; &nbsp; // Depending on how you generate your SQL, you may need to explicitly distinguish&nbsp; &nbsp; &nbsp; &nbsp; // between jOOQ generating bind variables or inlined literals.&nbsp; &nbsp; &nbsp; &nbsp; if (ctx.render().paramType() == ParamType.INLINED){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.render().visit(DSL.inline(ctx.convert(converter()).value())).sql("::json");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.render().sql("?::json");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Registering VARCHAR types for JDBC CallableStatement OUT parameters&nbsp; &nbsp; @Override&nbsp; &nbsp; public void register(BindingRegisterContext<JsonElement> ctx) throws SQLException {&nbsp; &nbsp; &nbsp; &nbsp; ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR);&nbsp; &nbsp; }&nbsp; &nbsp; // Converting the JsonElement to a String value and setting that on a JDBC PreparedStatement&nbsp; &nbsp; @Override&nbsp; &nbsp; public void set(BindingSetStatementContext<JsonElement> ctx) throws SQLException {&nbsp; &nbsp; &nbsp; &nbsp; ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null));&nbsp; &nbsp; }&nbsp; &nbsp; // Getting a String value from a JDBC ResultSet and converting that to a JsonElement&nbsp; &nbsp; @Override&nbsp; &nbsp; public void get(BindingGetResultSetContext<JsonElement> ctx) throws SQLException {&nbsp; &nbsp; &nbsp; &nbsp; ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));&nbsp; &nbsp; }&nbsp; &nbsp; // Getting a String value from a JDBC CallableStatement and converting that to a JsonElement&nbsp; &nbsp; @Override&nbsp; &nbsp; public void get(BindingGetStatementContext<JsonElement> ctx) throws SQLException {&nbsp; &nbsp; &nbsp; &nbsp; ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));&nbsp; &nbsp; }&nbsp; &nbsp; // Setting a value on a JDBC SQLOutput (useful for Oracle OBJECT types)&nbsp; &nbsp; @Override&nbsp; &nbsp; public void set(BindingSetSQLOutputContext<JsonElement> ctx) throws SQLException {&nbsp; &nbsp; &nbsp; &nbsp; throw new SQLFeatureNotSupportedException();&nbsp; &nbsp; }&nbsp; &nbsp; // Getting a value from a JDBC SQLInput (useful for Oracle OBJECT types)&nbsp; &nbsp; @Override&nbsp; &nbsp; public void get(BindingGetSQLInputContext<JsonElement> ctx) throws SQLException {&nbsp; &nbsp; &nbsp; &nbsp; throw new SQLFeatureNotSupportedException();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java