I have the problem with Object Mapper deserialize.
I use hibernate + spring boot and needed to deserialize json array for entity field.
This is my hibernate AttributeConverter:
abstract class JsonConverter<T>: AttributeConverter<Collection<T>, String> {
abstract val typeReference: TypeReference<out Collection<T>>
override fun convertToDatabaseColumn(attribute: Collection<T>?): String? {
if (attribute.isNullOrEmpty())
return null
return ObjectMapper.writeValueAsString(attribute)
}
}
implementation for collection
abstract class ListConverter<T>: JsonConverter<T>() {
override val typeReference = object : TypeReference<List<T>>() {}
override fun convertToEntityAttribute(dbData: String?): Collection<T> {
if (dbData == null)
return emptyList()
return ObjectMapper.readValue(dbData, typeReference)
}
}
abstract class SetConverter<T>: JsonConverter<T>() {
override val typeReference = object : TypeReference<Set<T>>() {}
override fun convertToEntityAttribute(dbData: String?): Collection<T> {
if (dbData == null)
return emptySet()
return ObjectMapper.readValue(dbData, typeReference)
}
}
Hibernate entity field
@Column(name = "product_ids")
@Convert(converter = LongSetConverter::class)
var productIds: Set<Long>,
implementation AttributeConverter for this entity field
class LongSetConverter: SetConverter<Long>()
The problem is that Long Generic not working and Object Mapper always return Set of Int
Long (or another type) just ingnore