Created
November 15, 2017 03:26
-
-
Save homerquan/d4630a4d92e24415b5e2ceda3e9cf165 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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