Katia Cirlene
10/02/2024, 5:28 PM{
"new-york-store": [
{
"open": 14
},
{
"close": 22
}
],
"new-jersey-store": [
{
"open": 10
},
{
"close": 18
}
]
}
that I've mapped this way:
enum class Status(val description: String) {
OPEN("open"),
CLOSED("closed")
}
data class StoreOpenHours(val type: Status?, val hour: Long?) {
}
data class Store(val name: String) {
}
data class Response(
val hours: MutableMap<Store, List<StoreOpenHours>?> = sortedMapOf<Store, List<StoreOpenHours>?>(),
) {
But I'm receiving the following error
io.ktor.serialization.JsonConvertException: Illegal json parameter found:
Unrecognized field "open" (class storeservice.model.StoreOpenHours), not marked as ignorable (2 known properties: "type", "hour"])
Do you have any idea how can I solve this?
I've already tried many things, like for example use @JsonValue annotation "type". But nothing worked.Renan Kummer
10/02/2024, 6:19 PMdata class StoreOpenHours(
val open: Int,
val close: Int
)
And if you want a dynamic property names for each store use Map<String, List<StoreOpenHours>>
.
I'm not familiar with the remaining parts of your code, but as far as serialization with Jackson goes, this should do the trick.Katia Cirlene
10/02/2024, 6:29 PM{
"new-york-store": [
{
"open": 14
}
]
}
Renan Kummer
10/02/2024, 6:35 PMKatia Cirlene
10/02/2024, 6:39 PM{
"new-york-store": [
{
"open": 14
},
{
"close": 22
}
],
"new-jersey-store": [
{
"open": 10
},
{
"close": 18
}
]
}
On my side I need to combine this information about the opening hours with other information and create an endpoint that returns the data with the "enhanced information".Katia Cirlene
10/02/2024, 6:41 PMRenan Kummer
10/02/2024, 6:45 PMKatia Cirlene
10/03/2024, 2:42 PMenum class Status(val description: String) {
OPEN("open"),
CLOSE("close");
companion object {
fun fromString(value: String): Status {
return entries.find { it.name.equals(value, ignoreCase = true) }
?: throw IllegalArgumentException("Unknown Status: $value")
}
}
}
@JsonDeserialize(using = StoreOpenHoursDeserializer::class)
data class StoreOpenHours(val hours: Map<Status, Long) {
}
data class Store(val name: String) {
}
data class Response(
val hours: MutableMap<Store, List<StoreOpenHours>> = sortedMapOf<Store, List<StoreOpenHours>>(),
) {}
}
data class ClientResponse(
val openingHours: MutableMap<DayOfWeek, List<OpeningHours>> = sortedMapOf<DayOfWeek, List<OpeningHours>>(),
) {
@JsonAnySetter
fun values(
name: String,
hours: List<OpeningHours>,
) {
if (hours.isEmpty()) {
openingHours[DayOfWeek.valueOf(name.uppercase())] = Collections.emptyList()
} else {
openingHours[DayOfWeek.valueOf(name.uppercase())] = hours
}
}
}
I had to create a custom serializer and register it on my json configuration:
addModule(SimpleModule().addDeserializer(StoreOpenHours::class.java, StoreOpenHoursDeserializer()))
Now everything is working as I expected. :-)