I have the following case class:
case class VirtualAssetConfigParam(
id: Long,
pMin: Double,
pMax: Double,
dispatchPriority: Int
)
object VirtualAssetConfigParam {
implicit val virtualAssetConfigParamReads: Reads[VirtualAssetConfigParam] = (
(JsPath \ "id").read[Long] and
(JsPath \ "power_min").read[Double] and
(JsPath \ "power_max").read[Double] and
(JsPath \ "dispatch_priority").read[Int]
)(VirtualAssetConfigParam.apply _)
}
This is the JSON that I get from the database:
[{"id":"1","power_min":"200","power_max":"400","dispatch_priority":"1"},{"id":"2","power_min":"200","power_max":"400","dispatch_priority":"2"},{"id":"3","power_min":"-700","power_max":"0","dispatch_priority":"3"}]
When I tried to validate it as (where virtualAssetConfigParam is a String that I get from the database):
Json.parse(virtualAssetConfigParam).validate[List[VirtualAssetConfigParam]]
I get the following as the validated result:
JsError(
List(
((0)/dispatchPriority,List(ValidationError(List(error.path.missing),WrappedArray()))),
((0)/pMin,List(ValidationError(List(error.path.missing),WrappedArray()))),
((0)/pMax,List(ValidationError(List(error.path.missing),WrappedArray()))),
((0)/id,List(ValidationError(List(error.expected.jsnumber),WrappedArray()))),
((1)/dispatchPriority,List(ValidationError(List(error.path.missing),WrappedArray()))),
((1)/pMin,List(ValidationError(List(error.path.missing),WrappedArray()))),
((1)/pMax,List(ValidationError(List(error.path.missing),WrappedArray()))),
((1)/id,List(ValidationError(List(error.expected.jsnumber),WrappedArray()))),
((2)/dispatchPriority,List(ValidationError(List(error.path.missing),WrappedArray()))),
((2)/pMin,List(ValidationError(List(error.path.missing),WrappedArray()))),
((2)/pMax,List(ValidationError(List(error.path.missing),WrappedArray()))),
((2)/id,List(ValidationError(List(error.expected.jsnumber),WrappedArray())))
)
)
What is the problem? I could not see why this should fail!
If you look at your error you will notice that it is trying to find paths - dispatchPriority
, pMin
, pMax
and id
which is different from the paths that you wanted in your read
. This indicates that the read implementation that you have shown in code is not being used here.
this leads me to think that probably you are not importing your implicit read into the scope of your code.
case class VirtualAssetConfigParam(
id: Long,
pMin: Double,
pMax: Double,
dispatchPriority: Int
)
object VirtualAssetConfigParam {
implicit val virtualAssetConfigParamReads: Reads[VirtualAssetConfigParam] = (
(JsPath \ "id").read[Long] and
(JsPath \ "power_min").read[Double] and
(JsPath \ "power_max").read[Double] and
(JsPath \ "dispatch_priority").read[Int]
)(VirtualAssetConfigParam.apply _)
}
object Demo extends App {
// I think you are missing this import
import VirtualAssetConfigParam.virtualAssetConfigParamReads
val virtualAssetConfigParam = [{"id":"1","power_min":"200","power_max":"400","dispatch_priority":"1"},{"id":"2","power_min":"200","power_max":"400","dispatch_priority":"2"},{"id":"3","power_min":"-700","power_max":"0","dispatch_priority":"3"}]
// It should work if you have that import
val jsonValidate = Json.parse(virtualAssetConfigParam).validate[List[VirtualAssetConfigParam]]
}