Skip to content

Instantly share code, notes, and snippets.

@homerquan
Created November 15, 2017 03:26
Show Gist options
  • Save homerquan/d4630a4d92e24415b5e2ceda3e9cf165 to your computer and use it in GitHub Desktop.
Save homerquan/d4630a4d92e24415b5e2ceda3e9cf165 to your computer and use it in GitHub Desktop.
sealed trait Obj
case class Error(error:String) extends Obj
case class DataA(a1:String, a2: Int) extends Obj
case class DataB(b1:String, b2: Boolean) extends Obj
val strA = """{"a1":"foo", "a2": 1}"""
val strB = """{"b1":"bar", "b2": false}"""
val srtE = """{"error": "oops"}"""
object JsonProtocols extends DefaultJsonProtocol {
implicit val errorFormat = jsonFormat1(Error)
implicit val dataAFormat = jsonFormat2(DataA)
implicit val dataBFormat = jsonFormat2(DataB)
}
import JsonProtocols._
val result:Obj = strA.parseJson.convertTo[Either[DataA,Error]] match {
case Left(dataA) => dataA
case Right(error) => error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment