Created
May 5, 2016 15:44
-
-
Save kakakazuma/1b573f1dd5a59a56b8c780da6147ba88 to your computer and use it in GitHub Desktop.
play-jsonでnullの含まれるListをパースする ref: http://qiita.com/kakakazuma/items/7cf60811f616d1c40147
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
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]]) |
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
Sample(aaa,18,ListBuffer(Some(test1), None, Some(test2))) |
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
{ | |
"id":"aaa", | |
"age":18, | |
"params":[ | |
"test1", | |
null, | |
"test2" | |
] | |
} |
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
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