Skip to content

Instantly share code, notes, and snippets.

@jerryankur
Created June 27, 2024 10:27
Show Gist options
  • Save jerryankur/51d41c2a58382ccec1d27f37380e3416 to your computer and use it in GitHub Desktop.
Save jerryankur/51d41c2a58382ccec1d27f37380e3416 to your computer and use it in GitHub Desktop.
JsonType
public class JsonType implements UserType<Object>, DynamicParameterizedType {
private final MutabilityPlan<Object> mutabilityPlan;
private final ObjectMapper objectMapper = new ObjectMapper();
private Type propertyType;
private Class<?> propertyClass;
public JsonType() {
this.mutabilityPlan = new MutableMutabilityPlan<>() {
@Override
protected Object deepCopyNotNull(Object value) {
return ObjectUtil.clone(value);
}
};
}
public String getName() {
return "json";
}
@Override
public boolean equals(Object first, Object second) {
return ObjectUtil.equals(first, second);
}
@Override
public int hashCode(Object object) {
return object.hashCode();
}
@Override
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws
SQLException {
String json = rs.getString(position);
if (json == null) {
return null;
}
if (String.class.isAssignableFrom(propertyClass)) {
return json;
}
try {
return objectMapper.readValue(json, objectMapper.getTypeFactory().constructType(propertyType));
} catch (JsonProcessingException exception) {
LogUtil.error("Failed to deserialize json content from database", exception);
throw new RuntimeException(exception);
}
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, getSqlType());
return;
}
try {
st.setObject(index, value instanceof String ? value : objectMapper.writeValueAsString(value), getSqlType());
} catch (JsonProcessingException exception) {
LogUtil.error("Failed to serialize json content to database", exception);
throw new RuntimeException(exception);
}
}
@Override
public Object deepCopy(Object value) {
return this.mutabilityPlan.deepCopy(value);
}
@Override
public boolean isMutable() {
return this.mutabilityPlan.isMutable();
}
@Override
public Serializable disassemble(Object value) {
return this.mutabilityPlan.disassemble(value, null);
}
@Override
public Object assemble(Serializable cached, Object owner) {
return this.mutabilityPlan.assemble(cached, null);
}
@Override
public int getSqlType() {
return Types.OTHER;
}
@Override
public Class<Object> returnedClass() {
return Object.class;
}
@Override
public void setParameterValues(Properties parameters) {
final XProperty xProperty = (XProperty) parameters.get(XPROPERTY);
this.propertyType = ((JavaXMember) xProperty).getJavaType();
this.propertyClass = (Class<?>) ((ParameterizedType) propertyType).getRawType();
}
}
@jerryankur
Copy link
Author

use it like this in entity field

@Type(value = JsonType.class)
@Column(columnDefinition = "json")
private Map<ConfigurationKey, Object> configuration = new HashMap<>();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment