Skip to content
Snippets Groups Projects
Commit c758f507 authored by Sören Henning's avatar Sören Henning
Browse files

Merge branch 'Replace-GSON' into 'main'

Replace GSON with Jackson ObjectMapper

See merge request !292
parents 93a3eb26 2334aa5a
No related branches found
No related tags found
1 merge request!292Replace GSON with Jackson ObjectMapper
Pipeline #12762 failed
......@@ -23,7 +23,6 @@ dependencies {
implementation 'org.bouncycastle:bcprov-ext-jdk15on:1.69'
implementation 'org.bouncycastle:bcpkix-jdk15on:1.69'
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'org.slf4j:slf4j-simple:1.7.32'
implementation 'io.github.microutils:kotlin-logging:2.1.16'
implementation 'org.apache.kafka:kafka-clients:2.8.0'
......
......
package rocks.theodolite.core
import com.google.gson.GsonBuilder
import com.fasterxml.jackson.databind.ObjectMapper
import mu.KotlinLogging
import java.io.File
import java.io.PrintWriter
......@@ -56,8 +56,9 @@ class IOHandler {
* @param fileURL the URL of the file
*/
fun <T> writeToJSONFile(objectToSave: T, fileURL: String) {
val gson = GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create()
writeStringToTextFile(fileURL, gson.toJson(objectToSave))
val outputFile = File(fileURL)
ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(outputFile, objectToSave)
logger.info { "Wrote txt file: $fileURL to ${outputFile.absolutePath}." }
}
/**
......
......
package rocks.theodolite.kubernetes.operator
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.fasterxml.jackson.databind.ObjectMapper
import io.fabric8.kubernetes.client.informers.ResourceEventHandler
import mu.KotlinLogging
import rocks.theodolite.kubernetes.model.BenchmarkExecution
......@@ -22,8 +21,7 @@ class ExecutionEventHandler(
private val controller: TheodoliteController,
private val stateHandler: ExecutionStateHandler
) : ResourceEventHandler<ExecutionCRD> {
private val gson: Gson = GsonBuilder().enableComplexMapKeySerialization().create()
private val mapper: ObjectMapper = ObjectMapper()
/**
* Adds an execution to the end of the queue of the TheodoliteController.
......@@ -60,7 +58,7 @@ class ExecutionEventHandler(
override fun onUpdate(oldExecution: ExecutionCRD, newExecution: ExecutionCRD) {
newExecution.spec.name = newExecution.metadata.name
oldExecution.spec.name = oldExecution.metadata.name
if (gson.toJson(oldExecution.spec) != gson.toJson(newExecution.spec)) {
if (mapper.writeValueAsString(oldExecution.spec) != mapper.writeValueAsString(newExecution.spec)) {
logger.info { "Receive update event for execution ${oldExecution.metadata.name}." }
when (this.stateHandler.getExecutionState(newExecution.metadata.name)) {
ExecutionState.RUNNING -> {
......
......
package rocks.theodolite.kubernetes.slo
import com.google.gson.Gson
import com.fasterxml.jackson.databind.ObjectMapper
import khttp.get
import khttp.responses.Response
import mu.KotlinLogging
......@@ -67,7 +67,7 @@ class MetricFetcher(private val prometheusURL: String, private val offset: Durat
* @return a [PrometheusResponse]
*/
private fun parseValues(values: Response): PrometheusResponse {
return Gson().fromJson<PrometheusResponse>(
return ObjectMapper().readValue<PrometheusResponse>(
values.jsonObject.toString(),
PrometheusResponse::class.java
)
......
......
package rocks.theodolite.kubernetes.slo
import com.google.gson.Gson
import com.fasterxml.jackson.databind.ObjectMapper
class SloJson constructor(
val results: List<List<PromResult>>,
......@@ -8,7 +8,7 @@ class SloJson constructor(
) {
fun toJson(): String {
return Gson().toJson(
return ObjectMapper().writeValueAsString(
mapOf(
"results" to this.results,
"metadata" to this.metadata
......
......
package rocks.theodolite.core
import com.google.gson.GsonBuilder
import com.fasterxml.jackson.databind.ObjectMapper
import io.quarkus.test.junit.QuarkusTest
import org.hamcrest.CoreMatchers.containsString
import org.hamcrest.MatcherAssert.assertThat
......@@ -121,7 +121,7 @@ internal class IOHandlerTest {
objectToSave = testContentResource
)
val expected = GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create().toJson(testContentResource)
val expected = ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(testContentResource)
assertEquals(
expected,
......
......
package rocks.theodolite.kubernetes.operator
import com.google.gson.Gson
import com.fasterxml.jackson.databind.ObjectMapper
import io.fabric8.kubernetes.api.model.ConfigMapBuilder
import io.fabric8.kubernetes.api.model.Pod
import io.fabric8.kubernetes.api.model.PodBuilder
......@@ -10,16 +10,16 @@ import io.fabric8.kubernetes.client.server.mock.KubernetesServer
import io.fabric8.kubernetes.client.server.mock.OutputStreamMessage
import io.fabric8.kubernetes.client.utils.Utils
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
import rocks.theodolite.kubernetes.model.crd.BenchmarkState
import rocks.theodolite.kubernetes.model.KubernetesBenchmark
import rocks.theodolite.kubernetes.model.crd.BenchmarkCRDummy
import rocks.theodolite.kubernetes.ConfigMapResourceSet
import rocks.theodolite.kubernetes.ExecActionSelector
import rocks.theodolite.kubernetes.PodSelector
import rocks.theodolite.kubernetes.ResourceSets
import rocks.theodolite.kubernetes.model.KubernetesBenchmark
import rocks.theodolite.kubernetes.model.crd.BenchmarkCRDummy
import rocks.theodolite.kubernetes.model.crd.BenchmarkState
internal class BenchmarkStateCheckerTest {
private val server = KubernetesServer(false, false)
......@@ -128,7 +128,7 @@ internal class BenchmarkStateCheckerTest {
val resource = resourceBuilder.build()
resource.metadata.name = "test-deployment"
resource.metadata.labels = mutableMapOf("app" to "pod1")
val resourceString = Gson().toJson(resource)
val resourceString = ObjectMapper().writeValueAsString(resource)
// create and deploy configmap
val configMap1 = ConfigMapBuilder()
......
......
package rocks.theodolite.kubernetes.operator
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.fasterxml.jackson.databind.ObjectMapper
import io.fabric8.kubernetes.client.CustomResourceList
import io.fabric8.kubernetes.client.server.mock.KubernetesServer
import io.quarkus.test.junit.QuarkusTest
......@@ -19,7 +18,7 @@ import rocks.theodolite.kubernetes.model.crd.*
class ControllerTest {
private final val server = KubernetesServer(false, false)
lateinit var controller: TheodoliteController
private val gson: Gson = GsonBuilder().enableComplexMapKeySerialization().create()
private val mapper = ObjectMapper()
private var benchmark = KubernetesBenchmark()
private var execution = BenchmarkExecution()
......@@ -118,8 +117,8 @@ class ControllerTest {
assertEquals(2, result.size)
assertEquals(
gson.toJson(benchmark),
gson.toJson(result.firstOrNull()?.spec)
mapper.writeValueAsString(benchmark),
mapper.writeValueAsString(result.firstOrNull()?.spec)
)
}
......@@ -133,8 +132,8 @@ class ControllerTest {
val result = method.invoke(controller) as BenchmarkExecution?
assertEquals(
gson.toJson(this.execution),
gson.toJson(result)
mapper.writeValueAsString(this.execution),
mapper.writeValueAsString(result)
)
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment