Skip to content

Instantly share code, notes, and snippets.

@kakakazuma
Created May 5, 2016 15:44
Show Gist options
  • Save kakakazuma/1b573f1dd5a59a56b8c780da6147ba88 to your computer and use it in GitHub Desktop.
Save kakakazuma/1b573f1dd5a59a56b8c780da6147ba88 to your computer and use it in GitHub Desktop.
play-jsonでnullの含まれるListをパースする ref: http://qiita.com/kakakazuma/items/7cf60811f616d1c40147
No Json deserializer found for type Seq[Option[String]]. Try to implement an implicit Reads or Format for this type.
[error] (JsPath \ "params").read[Seq[Option[String]]] | Reads.pure(Seq.empty[Option[String]])
Sample(aaa,18,ListBuffer(Some(test1), None, Some(test2)))
{
"id":"aaa",
"age":18,
"params":[
"test1",
null,
"test2"
]
}
case class Sample(id:String, age:Int, params:Seq[Option[String]])
object Sample extends ((String,Int,Seq[Option[String]]) => Sample) {
implicit val SeqOpStringRead = new Reads[Seq[Option[String]]] {
override def reads(json: JsValue): JsResult[Seq[Option[String]]] = {
json match {
case JsArray(seq) => JsSuccess(seq.map(jsvalue => jsvalue.asOpt[String]))
case _ => JsError("Invalid array")
}
}
}
implicit val SampleRead: Reads[Sample] = (
(JsPath \ "id").read[String] and
(JsPath \ "age").read[Int] and
(JsPath \ "params").read[Seq[Option[String]]] | Reads.pure(Seq.empty[Option[String]])
)(Sample)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment