commonMain.com.bselzer.ktx.serialization.serializer.Point3DSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geometry-serialization-jvm Show documentation
Show all versions of geometry-serialization-jvm Show documentation
kotlinx.serialization for two and three dimensional geometrical objects.
package com.bselzer.ktx.serialization.serializer
import com.bselzer.ktx.geometry.dimension.tri.position.Point3D
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
/**
* A serializer for converting an array into a three-dimensional point.
*/
class Point3DSerializer : KSerializer {
/**
* The serializer of the 3D point data.
*/
private val serializer = ListSerializer(Double.serializer())
@OptIn(ExperimentalSerializationApi::class)
override val descriptor: SerialDescriptor = SerialDescriptor(Point3D::class.qualifiedName!!, serializer.descriptor)
override fun deserialize(decoder: Decoder): Point3D = deserialize(serializer.deserialize(decoder))
/**
* Converts a list to a [Point3D].
*
* @param list the three-dimensional components
*/
fun deserialize(list: List) = Point3D(list.getOrElse(0) { 0.0 }, list.getOrElse(1) { 0.0 }, list.getOrElse(2) { 0.0 })
override fun serialize(encoder: Encoder, value: Point3D) {
val list = listOf(value.x, value.y, value.z)
serializer.serialize(encoder, list)
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy