King River Lee
12/06/2023, 8:34 AM@JsonFormat(pattern = "yyyy-MM-dd HH:mm", shape = JsonFormat.Shape.STRING)
not work on LocalDateTime field of data classdave
12/06/2023, 10:37 AMdata class FooBar(val field: LocalDateTime)
object MyJackson : ConfigurableJackson(
KotlinModule.Builder().build()
.asConfigurable()
.withStandardMappings()
.text(StringBiDiMappings.localDateTime(DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm")))
.done()
)
fun main() {
println(MyJackson.asFormatString(FooBar(LocalDateTime.now())))
}
.. which will yield what you want:
{
"field": "2023-12-06 10:36"
}
King River Lee
12/06/2023, 12:29 PMKing River Lee
12/06/2023, 12:30 PMWRITE_DATES_AS_TIMESTAMPS
for jackson mapper.dave
12/06/2023, 12:40 PMobject MyJackson : ConfigurableJackson(
KotlinModule.Builder().build()
.asConfigurable()
.withStandardMappings()
.text(StringBiDiMappings.localDateTime(DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm")))
.done()
.configure(DeserializationFeature.WHATEVERFEATUREYOUWANT, true)
)
There are a few things going on here. http4k is overriding the serialization of the annotation with it's own mapping (which is in .withStandardMappings()
. I don't know about the predecence of the custom mappings to the annotations for Jackson - it's all quite magical (which is why we don't try to use them). You could create a custom type which controls the serialisation
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule
import dev.forkhandles.values.LocalDateTimeValue
import dev.forkhandles.values.LocalDateTimeValueFactory
import org.http4k.format.ConfigurableJackson
import org.http4k.format.asConfigurable
import org.http4k.format.value
import org.http4k.format.withStandardMappings
import org.http4k.lens.StringBiDiMappings
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
class MyFunnyDateTime private constructor(value: LocalDateTime) : LocalDateTimeValue(value) {
companion object : LocalDateTimeValueFactory<MyFunnyDateTime>(
::MyFunnyDateTime, fmt =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
)
}
data class FooBar(val field: MyFunnyDateTime)
object MyJackson : ConfigurableJackson(
KotlinModule.Builder().build()
.asConfigurable()
.withStandardMappings()
.value(MyFunnyDateTime)
.text(StringBiDiMappings.localDateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")))
.done()
.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, true)
)
fun main() {
println(MyJackson.asFormatString(FooBar(MyFunnyDateTime.of(LocalDateTime.now()))))
}
King River Lee
12/06/2023, 12:50 PMannotation
is magic. So I will try your method. I will try less annotation
in the furture. Thanks