简体   繁体   中英

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM