use kotlinx serialization
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2021-08-18 14:01:16 +02:00
parent 874480aadb
commit e4bcfab2af
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
4 changed files with 25 additions and 11 deletions

View File

@ -1,6 +1,7 @@
plugins { plugins {
application application
kotlin("jvm") version "1.5.10" kotlin("jvm") version "1.5.10"
kotlin("plugin.serialization") version "1.5.10"
id("com.github.johnrengelman.shadow") version "7.0.0" id("com.github.johnrengelman.shadow") version "7.0.0"
} }
@ -16,7 +17,19 @@ repositories {
jcenter() jcenter()
} }
buildscript {
repositories { mavenCentral() }
dependencies { dependencies {
val kotlinVersion = "1.5.10"
classpath(kotlin("gradle-plugin", version = kotlinVersion))
classpath(kotlin("serialization", version = kotlinVersion))
}
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1")
implementation("com.rabbitmq:amqp-client:5.13.0") implementation("com.rabbitmq:amqp-client:5.13.0")
implementation("com.viartemev:the-white-rabbit:0.0.6") implementation("com.viartemev:the-white-rabbit:0.0.6")
implementation("com.zaxxer:HikariCP:5.0.0") implementation("com.zaxxer:HikariCP:5.0.0")
@ -26,7 +39,6 @@ dependencies {
implementation("org.jetbrains.exposed:exposed-java-time:0.33.1") implementation("org.jetbrains.exposed:exposed-java-time:0.33.1")
implementation("io.insert-koin:koin-core:3.1.2") implementation("io.insert-koin:koin-core:3.1.2")
implementation("org.postgresql:postgresql:42.2.23") implementation("org.postgresql:postgresql:42.2.23")
implementation("com.google.code.gson:gson:2.8.7")
} }
tasks { tasks {

View File

@ -1,10 +1,11 @@
package api package api
import com.google.gson.annotations.SerializedName import kotlinx.datetime.LocalDate
import java.time.LocalDate import kotlinx.serialization.Serializable
@Serializable
data class ApiObject( data class ApiObject(
@SerializedName("tag") val tag: String, val tag: String,
@SerializedName("device_id") val device_id: Int, val device_id: Int,
@SerializedName("device_date") val device_date: LocalDate val device_date: LocalDate
) )

View File

@ -2,6 +2,7 @@ package database.service
import database.model.SampleObject import database.model.SampleObject
import api.ApiObject import api.ApiObject
import kotlinx.datetime.toJavaLocalDate
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
class SampleObjectService : ISampleObjectService { class SampleObjectService : ISampleObjectService {
@ -10,7 +11,7 @@ class SampleObjectService : ISampleObjectService {
transaction { transaction {
SampleObject.new { SampleObject.new {
tag = apiObject.tag tag = apiObject.tag
device_date = apiObject.device_date device_date = apiObject.device_date.toJavaLocalDate()
device_id = apiObject.device_id device_id = apiObject.device_id
} }
} }

View File

@ -1,15 +1,15 @@
package mq package mq
import api.ApiObject import api.ApiObject
import com.google.gson.GsonBuilder import kotlinx.serialization.json.*
import com.rabbitmq.client.* import com.rabbitmq.client.*
import database.service.ISampleObjectService import database.service.ISampleObjectService
import kotlinx.serialization.decodeFromString
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
import org.koin.core.component.inject import org.koin.core.component.inject
class DatabaseConsumer (channel: Channel) : Consumer, KoinComponent { class DatabaseConsumer (channel: Channel) : Consumer, KoinComponent {
private val resultObjectService: ISampleObjectService by inject() private val resultObjectService: ISampleObjectService by inject()
private val gson = GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssz").create()
private val basicChannel = channel private val basicChannel = channel
override fun handleConsumeOk(consumerTag: String?) { override fun handleConsumeOk(consumerTag: String?) {
} }
@ -38,7 +38,7 @@ class DatabaseConsumer (channel: Channel) : Consumer, KoinComponent {
) { ) {
try { try {
val rawJson = body!!.toString(Charsets.UTF_8) val rawJson = body!!.toString(Charsets.UTF_8)
val apiObject = gson.fromJson(rawJson, ApiObject::class.java) val apiObject = Json.decodeFromString<ApiObject>(rawJson)
resultObjectService.addOne(apiObject) resultObjectService.addOne(apiObject)
basicChannel.basicAck(envelope!!.deliveryTag,false) basicChannel.basicAck(envelope!!.deliveryTag,false)
} catch (e: Exception) { } catch (e: Exception) {