You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched in the issues and found nothing similar.
Describe the bug
Problem:
I wanted to change default pattern of formatting for OffsetDateTime (only if the field is not annotated with @JsonFormat)
Solution:
I realized custom serializer which extends JsonSerializer<OffsetDateTime>. I also implemented ContextualSerializer in order to define is field annotated or not, all annotated fields will be passed to default serializer. I made custom modifier (exteded BeanSerializerModifier) to get and pass default serializer to my custom serializer.
I tested my serializer with unannotated OffsetDateTime fields and default format is working. But when I tried fields with @JsonFormat(pattern=...), I realized that defaultSerializer has ignored the pattern and used its default one but I expected pattern which is defined in @JsonFormat
Version Information
2.15.4
Reproduction
Custom Serializer:
class CustomOffsetDateTimeSerializer(
private val defaultSerializer: JsonSerializer<*>,
private val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern(defaultPattern),
) : JsonSerializer<OffsetDateTime>(), ContextualSerializer {
companion object {
private const val defaultPattern = "yyyy-MM-dd'T'HH:mm"
}
override fun serialize(
value: OffsetDateTime,
jsonGenerator: JsonGenerator,
serializerProvider: SerializerProvider?,
) {
jsonGenerator.writeString(formatter.format(value))
}
override fun createContextual(
prov: SerializerProvider?,
property: BeanProperty?,
): JsonSerializer<*> {
if (property?.getAnnotation(JsonFormat::class.java) != null) {
return defaultSerializer
}
return CustomOffsetDateTimeSerializer(defaultSerializer)
}
}
2.15.4 is not the latest so please make sure to verify with 2.17.2 or 2.18.0-rc1. Ideally before filing issue :)
Also looks like reproduction is in Kotlin so I may need to move to Kotlin module (although might be easy enough to translate to plain Java to keep here).
Also: code is not complete enough as reproduction: would need code to show configuration, calls to serialize.
Search before asking
Describe the bug
Problem:
I wanted to change default pattern of formatting for OffsetDateTime (only if the field is not annotated with
@JsonFormat
)Solution:
I realized custom serializer which extends
JsonSerializer<OffsetDateTime>
. I also implementedContextualSerializer
in order to define is field annotated or not, all annotated fields will be passed to default serializer. I made custom modifier (extededBeanSerializerModifier
) to get and pass default serializer to my custom serializer.I tested my serializer with unannotated OffsetDateTime fields and default format is working. But when I tried fields with
@JsonFormat(pattern=...)
, I realized that defaultSerializer has ignored the pattern and used its default one but I expected pattern which is defined in@JsonFormat
Version Information
2.15.4
Reproduction
Custom Serializer:
Custom Modifier:
Expected behavior
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: