Created
December 16, 2012 21:53
-
-
Save mandubian/4313522 to your computer and use it in GitHub Desktop.
Reads[java.math.BigDecimal]
This file contains 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
import play.api.libs.json._ | |
import play.api.libs.functional.syntax._ | |
import scala.util.control.Exception._ | |
import java.lang.NumberFormatException | |
import play.api.data.validation.ValidationError | |
implicit val bigDecReads = Reads[java.math.BigDecimal]( js => js match { | |
case JsString(s) => | |
catching(classOf[NumberFormatException]) | |
.opt( JsSuccess(new java.math.BigDecimal(s)) ) | |
.getOrElse( JsError(ValidationError("validate.error.expected.numberformatexception"))) | |
case JsNumber(d) => JsSuccess(d.underlying) | |
case _ => JsError(ValidationError("validate.error.expected.jsnumberorjsstring")) | |
} ) | |
implicit val bigDecWrites = Writes[java.math.BigDecimal]( bd => JsString(bd.toString) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment