All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.resteasy.reactive.kotlin.serialization.runtime.ViolationReportViolationSerializer.kt Maven / Gradle / Ivy

Go to download

Kotlin Serialization support for Quarkus REST. This extension is not compatible with the quarkus-resteasy extension, or any of the extensions that depend on it.

There is a newer version: 3.17.5
Show newest version
package io.quarkus.resteasy.reactive.kotlin.serialization.runtime

import io.quarkus.hibernate.validator.runtime.jaxrs.ViolationReport
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.Serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.serialDescriptor
import kotlinx.serialization.encoding.*

@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = ViolationReport.Violation::class)
object ViolationReportViolationSerializer : KSerializer {
    override val descriptor: SerialDescriptor =
        buildClassSerialDescriptor(
            "io.quarkus.hibernate.validator.runtime.jaxrs.ViolationReport.Violation"
        ) {
            element("field", serialDescriptor())
            element("message", serialDescriptor())
        }

    override fun deserialize(decoder: Decoder): ViolationReport.Violation {
        return decoder.decodeStructure(descriptor) {
            var field: String? = null
            var message: String? = null

            loop@ while (true) {
                when (val index = decodeElementIndex(descriptor)) {
                    CompositeDecoder.DECODE_DONE -> break@loop
                    0 -> field = decodeStringElement(descriptor, 0)
                    1 -> message = decodeStringElement(descriptor, 1)
                    else -> throw SerializationException("Unexpected index $index")
                }
            }

            ViolationReport.Violation(
                requireNotNull(field),
                requireNotNull(message),
            )
        }
    }

    override fun serialize(encoder: Encoder, value: ViolationReport.Violation) {
        encoder.encodeStructure(descriptor) {
            encodeStringElement(descriptor, 0, value.field)
            encodeStringElement(descriptor, 1, value.message)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy