model.GeoCoordinateBSONCodec.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of baku Show documentation
Show all versions of baku Show documentation
helps you focus your REST API back-end on the business logic
package com.github.fluidsonic.baku
import com.github.fluidsonic.fluid.stdlib.*
import org.bson.BsonReader
import org.bson.BsonWriter
internal object GeoCoordinateBSONCodec : AbstractBSONCodec() {
override fun BsonReader.decode(context: BSONCodingContext): GeoCoordinate {
var coordinate: GeoCoordinate? = null
var type: String? = null
readDocumentWithValues { fieldName ->
when (fieldName) {
"coordinates" -> coordinate = readArray {
val longitude = readDouble()
val latitude = readDouble()
GeoCoordinate(latitude = latitude, longitude = longitude)
}
"type" -> type = readString()
else -> skipValue()
}
}
if (type != "Point") throw BSONException("invalid type for GeoCoordinate: $type")
return coordinate ?: throw BSONException("missing coordinate")
}
override fun BsonWriter.encode(value: GeoCoordinate, context: BSONCodingContext) {
writeStartDocument()
writeName("coordinates")
writeStartArray()
writeDouble(value.longitude)
writeDouble(value.latitude)
writeEndArray()
writeName("type")
writeString("Point")
writeEndDocument()
}
}