Skip to content

Instantly share code, notes, and snippets.

@andyczerwonka
Created October 23, 2014 20:19
Show Gist options
  • Save andyczerwonka/debe3a55c7d7aa3224dc to your computer and use it in GitHub Desktop.
Save andyczerwonka/debe3a55c7d7aa3224dc to your computer and use it in GitHub Desktop.
I'm trying to create a JSON serializer in Play 2.3.4 and line #7 give a compiler error saying "No Json deserializer found for type Array[models.Sensor]. Try to implement an implicit Reads or Format for this type." Given there is a default serializer for Array, I assume this should work.
case class CreateParams(name: String, interval: Int, camera: Boolean, sensors: IndexedSeq[Sensor])
implicit val createParamsReads = new Reads[CreateParams] {
def reads(js: JsValue): JsResult[CreateParams] = {
val name = (js \ "name").as[String]
val interval = (js \ "interval").as[Int]
val camera = (js \ "camera").as[Boolean]
val sensors = (js \ "sensors").as[IndexedSeq[Sensor]]
JsSuccess(CreateParams(name, interval, camera, sensors))
}
}
implicit val sensorReads = new Reads[Sensor] {
def reads(js: JsValue): JsResult[Sensor] = {
val name = (js \ "name").as[String]
val unit = (js \ "unit").asOpt[String]
val scale = (js \ "scale").as[Double]
val offset = (js \ "offset").as[Double]
val internal = (js \ "internal").as[Boolean]
JsSuccess(Sensor(name, unit, scale, offset, internal))
}
}
@andyczerwonka
Copy link
Author

Ooops.... wrong order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment