JOOQ如何根据其他列值转换JSON? [英] JOOQ How to convert JSON based on other column value?
问题描述
假设我有一个表 customer(int id,类型varchar,首选项jsonb).
类型可以是REGULAR
,PREMIUM
等.根据列类型值,首选项JSON结构将有所不同.
Let's say I have a table customer(int id, type varchar, preferences jsonb).
The type can be REGULAR
, PREMIUM
etc. Based on the column type value the preferences JSON structure will be different.
在从数据库加载客户记录时,如果 type = REGULAR 我想将其转换为RegularCustomerPreferences
对象类型,并且如果 type = PREMIUM 转换为PremiumCustomerPreferences
对象类型.
While loading a customer record from database if type=REGULAR I want to convert it into RegularCustomerPreferences
object type, and if type=PREMIUM I want to convert it into PremiumCustomerPreferences
object type.
我已经看过几本有关使用JOOQ JSON转换器/绑定的教程.但是它们是一对一的映射,而不是基于条件的(取决于另一列的值).
I have gone through several tutorials on using JOOQ JSON converters/Bindings..but they are one to one mapping and not conditional based (depending on another column value).
实现此目标的理想方法是什么?
What is the ideal way to implement this?
推荐答案
您显然不能以类型安全的方式执行此操作,因为您的preferences
列的类型为Field<RegularCustomerPreferences | PremiumCustomerPreferences>
(联合类型),而Java却没有目前不支持工会类型.因此,也许可以将常见的CustomerPreferences
超级类型绑定到该列,并在使用该值的任何地方向下转换该值.
You obviously cannot do this in a type safe way as the type of your preferences
column would be Field<RegularCustomerPreferences | PremiumCustomerPreferences>
(the union type), and Java doesn't currently support union types. So, you could bind a common CustomerPreferences
super type to the column, perhaps, and downcast the value wherever it is consumed.
绑定超级类型应该相对容易.您将实现 Binding<Object, CustomerPreferences>
,它可以处理RegularCustomerPreferences
和PremiumCustomerPreferences
值.具体来说,您的转换器看起来像这样:
Binding the super type should be relatively easy. You will be implementing a Binding<Object, CustomerPreferences>
, which can handle both RegularCustomerPreferences
and PremiumCustomerPreferences
values. Specifically, your converter would look something like this:
public Converter<Object, CustomerPreferences> converter() {
return new Converter<Object, CustomerPreferences>() {
@Override
public CustomerPreferences from(Object t) {
if (some check here to see if this is a regular customer)
return new RegularCustomerPreferences(t);
else
return new PremiumCustomerPreferences(t);
}
@Override
public Object to(CustomerPreferences u) {
return MyJSONTools.toJSON(u);
}
@Override
public Class<Object> fromType() {
return Object.class;
}
@Override
public Class<CustomerPreferences> toType() {
return CustomerPreferences.class;
}
};
}
此处的假设是,您的JSON内容可用于决定JSON文档应具有的类型,而在type
列中则多余,因为当前,从3.11和3.12版本开始,jOOQ不支持多列数据类型绑定,这些绑定读取多个值以基于其数据类型转换决策.这是与以下相关的待定功能: https://github.com/jOOQ/jOOQ/issues/6124
The assumption here is that your JSON content allows for making the decision what type the JSON document should have, redundantly with the type
column, because currently, as of version 3.11 and 3.12, jOOQ doesn't support multi-column data type bindings which read several values to base their data type conversion decisions on. This is a pending feature related to: https://github.com/jOOQ/jOOQ/issues/6124
这篇关于JOOQ如何根据其他列值转换JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!