Skip to content
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

Implemented circe deserializer with fallback #20

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ private[circe] trait CirceSerialization {
data.asJson.noSpaces.getBytes(StandardCharsets.UTF_8)
}

def circeJsonDeserializer[T: Decoder]: KafkaDeserializer[T] = deserializer { (_, data) =>
(for {
json <- parse(new String(data, StandardCharsets.UTF_8)): Either[Error, Json]
t <- json.as[T]: Either[Error, T]
} yield
t).fold(error => throw new RuntimeException(s"Deserialization failure: ${error.getMessage}", error), identity _)
def circeJsonDeserializer[T: Decoder]: KafkaDeserializer[T] =
circeJsonDeserializerWithFallback { error =>
throw new RuntimeException(s"Deserialization failure: ${error.getMessage}", error)
}

def circeJsonDeserializerWithFallback[T: Decoder](fallback: Error => T): KafkaDeserializer[T] = deserializer { (_, data) =>
parse(new String(data, StandardCharsets.UTF_8))
.flatMap(_.as[T])
.fold(fallback, identity _)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.circe.syntax._
class CirceSerializationSpec extends UnitSpec with CirceSerialization {

"CirceSerialization" when {
"serializing" should {
"circeJsonSerializer" should {
"write the Json body" in forAll { event: Event =>
val serializer = circeJsonSerializer[Event]
val bytes = serializer.serialize("Does not matter", event)
Expand All @@ -20,15 +20,31 @@ class CirceSerializationSpec extends UnitSpec with CirceSerialization {
}
}

"deserializing" should {
"circeJsonDeserializer" should {
"parse the json" in forAll { event: Event =>
val jsonBytes = event.asJson.noSpaces.getBytes(UTF_8)
val deserializer = circeJsonDeserializer[Event]
val deserialized = deserializer.deserialize("does not matter", jsonBytes)

deserialized shouldBe event
}
}

"circeJsonDeserializerWithFallback" should {
"parse the json" in forAll { event: Event =>
val jsonBytes = event.asJson.noSpaces.getBytes(UTF_8)
val deserializer = circeJsonDeserializerWithFallback[Event](_ => Event("", ""))
val deserialized = deserializer.deserialize("does not matter", jsonBytes)

deserialized shouldBe event
}
"execute fallback function in case of failure" in forAll { event: Event =>
val jsonBytes = "{}".getBytes(UTF_8)
val deserializer = circeJsonDeserializerWithFallback[Event](_ => Event("", ""))
val deserialized = deserializer.deserialize("does not matter", jsonBytes)

deserialized shouldBe Event("", "")
}
}
}

Expand Down