Play Json: Transforming a Reads[T] to Reads[Seq[T]] without implicits

余生长醉 提交于 2019-12-05 16:50:47

问题


I hava a Reads[T]. I would like to parse a Json object which is expected to be an array of T's. Is there a simple way to obtain a Reads[Seq[T]] without defining my Reads[T] as implicit? Essentially, I am looking for a function that takes Reads[T] and returns Reads[Seq[T]].

I came across Reads.TraversableReads, and thought that I can pass the implicit reader it needs explicitly, but this function also wants a CanBuildForm[...], which does not sound like fun.


回答1:


There is a method for this in the Reads companion object: Reads.seq. Its parameter is usually implicit, but you can always call it explicitly if you want:

val a: Reads[T] = ...
val b: Reads[Seq[T]] = Reads.seq(a)


来源:https://stackoverflow.com/questions/17597659/play-json-transforming-a-readst-to-readsseqt-without-implicits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!