-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use circe as serde lib for json: 200 #214
Changes from 28 commits
f5e6795
8e8e746
ac908df
1dee384
852d4fd
37b5434
eb77ba5
f6d2ece
3c707e6
935cde0
3c2ca00
0f2b220
de28a5a
ad59999
cc9e949
3f5af81
78b6149
9adff68
b124fb1
a9e23e4
cf9cbb4
282d16e
cde28ee
c27ccb3
787d922
66bbbc9
305ade5
a3d2b20
c051b42
0750cb6
e3483f8
3e9d7b7
5e0cf75
a75e988
0e4af21
5208392
4b457a3
21885c4
24a5018
f0878b0
479ec33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,9 @@ import sttp.client3._ | |
import sttp.model.Uri | ||
import za.co.absa.atum.agent.exception.AtumAgentException.HttpException | ||
import za.co.absa.atum.model.dto.{AdditionalDataSubmitDTO, AtumContextDTO, CheckpointDTO, PartitioningSubmitDTO} | ||
import za.co.absa.atum.model.utils.SerializationUtils | ||
import io.circe.generic.auto._ | ||
import io.circe.syntax.EncoderOps | ||
import za.co.absa.atum.model.utils.JsonUtils.fromJson | ||
|
||
class HttpDispatcher(config: Config) extends Dispatcher(config: Config) with Logging { | ||
import HttpDispatcher._ | ||
|
@@ -47,19 +49,19 @@ class HttpDispatcher(config: Config) extends Dispatcher(config: Config) with Log | |
override protected[agent] def createPartitioning(partitioning: PartitioningSubmitDTO): AtumContextDTO = { | ||
val request = commonAtumRequest | ||
.post(createPartitioningEndpoint) | ||
.body(SerializationUtils.asJson(partitioning)) | ||
.body(partitioning.asJson.noSpaces) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this a bit inconsistent. If you intent to have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively we could use extension methods. object JsonSyntaxExtensions {
implicit class JsonSerializationSyntax[T: Encoder](val obj: T) extends AnyVal {
def asJsonString: String = obj.asJson.noSpaces
def asJsonStringPretty: String = obj.asJson.spaces2
}
implicit class JsonDeserializationSyntax[T: Decoder](val jsonStr: String) extends AnyVal {
def as: T = {
decode[T](jsonStr) match {
case Right(value) => value
case Left(error) => throw new RuntimeException(s"Failed to decode JSON: $error")
}
}
}
}
val person = Person("Alice", 30)
// Convert a Person instance to a compact JSON string
val jsonString = person.asJsonString
println(jsonString) // {"name":"Alice","age":30}
// Convert a Person instance to a pretty JSON string
val jsonPrettyString = person.asJsonStringPretty
val jsonStr = """{"name":"Bob","age":25}"""
// Convert a JSON string back to a Person instance
val personFromJson = jsonStr.as[Person] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This I tried to do, but it keeps saying that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Working solution object JsonSyntaxExtensions {
implicit class JsonSerializationSyntax[T: Encoder](val obj: T) {
def asJsonString: String = obj.asJson.noSpaces
def asJsonStringPretty: String = obj.asJson.spaces2
}
implicit class JsonDeserializationSyntax(val jsonStr: String) {
def as[T: Decoder]: T = {
decode[T](jsonStr) match {
case Right(value) => value
case Left(error) => throw new RuntimeException(s"Failed to decode JSON: $error")
}
}
}
} |
||
|
||
val response = backend.send(request) | ||
|
||
SerializationUtils.fromJson[AtumContextDTO]( | ||
fromJson[AtumContextDTO]( | ||
handleResponseBody(response) | ||
) | ||
} | ||
|
||
override protected[agent] def saveCheckpoint(checkpoint: CheckpointDTO): Unit = { | ||
val request = commonAtumRequest | ||
.post(createCheckpointEndpoint) | ||
.body(SerializationUtils.asJson(checkpoint)) | ||
.body(checkpoint.asJson.noSpaces) | ||
|
||
val response = backend.send(request) | ||
|
||
|
@@ -69,7 +71,7 @@ class HttpDispatcher(config: Config) extends Dispatcher(config: Config) with Log | |
override protected[agent] def saveAdditionalData(additionalDataSubmitDTO: AdditionalDataSubmitDTO): Unit = { | ||
val request = commonAtumRequest | ||
.post(createAdditionalDataEndpoint) | ||
.body(SerializationUtils.asJson(additionalDataSubmitDTO)) | ||
.body(additionalDataSubmitDTO.asJson.noSpaces) | ||
|
||
val response = backend.send(request) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems unused
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we have defined our serde in companions, therefore we don't need generic.auto.