Last active
May 14, 2020 13:52
-
-
Save imanabu/5815e2b6231e122da2b876c6fbe4d2f8 to your computer and use it in GitHub Desktop.
implicit converter lets you read Json into Option[Double] Scala Data Type
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
object DoubleReads { | |
def reads(json: JsValue): JsResult[Option[Double]] = json match { | |
case JsNumber(n) => JsSuccess(Some(n.toDouble)) | |
case JsString(s) => JsSuccess(Some(s.toDouble)) | |
case _ => JsSuccess(None) | |
} | |
} | |
// Later in your code where you need | |
implicit val odr: json.Reads[Option[Double]] = DoubleReads.reads | |
// then you can | |
val data = Json.fromJson[ObjContainingOptionDouble](json) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment