Skip to content

Commit

Permalink
Either format added to JsonUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbanda committed Sep 27, 2023
1 parent 0c12a71 commit 7cb0756
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.cequence.openaiscala

import java.{util => ju}
import play.api.libs.json._
import play.api.libs.json.{Format, _}

import java.util.Date

Expand Down Expand Up @@ -136,4 +136,38 @@ object JsonUtil {
JsObject(fields)
}
}

import play.api.libs.json._

private class EitherFormat[L, R](
leftFormat: Format[L],
rightFormat: Format[R]
) extends Format[Either[L, R]] {

override def reads(json: JsValue): JsResult[Either[L, R]] = {
val left = leftFormat.reads(json)
val right = rightFormat.reads(json)

if (left.isSuccess) {
left.map(Left(_))
} else if (right.isSuccess) {
right.map(Right(_))
} else {
JsError(s"Unable to read Either type from JSON $json")
}
}

override def writes(o: Either[L, R]): JsValue =
o match {
case Left(value) => leftFormat.writes(value)
case Right(value) => rightFormat.writes(value)
}
}

def eitherFormat[L: Format, R: Format]: Format[Either[L, R]] = {
val leftFormat = implicitly[Format[L]]
val rightFormat = implicitly[Format[R]]

new EitherFormat[L, R](leftFormat, rightFormat)
}
}

0 comments on commit 7cb0756

Please sign in to comment.