You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

32 lines
813 B

package controllers.common
import com.alexitc.playsonify.models.pagination.PaginatedResult
import com.xsn.explorer.models._
import org.scalactic.Every
import play.api.libs.json._
object Codecs {
implicit def everyReads[T](implicit readsT: Reads[T]): Reads[Every[T]] = Reads[Every[T]] { json =>
json
.validate[List[T]]
.flatMap { list =>
Every.from(list)
.map(JsSuccess.apply(_))
.getOrElse {
JsError.apply("A non-empty list is expected")
}
}
}
implicit def paginatedResultWrites[T](implicit writesT: Writes[T]): Writes[PaginatedResult[T]] = OWrites[PaginatedResult[T]] { result =>
Json.obj(
"offset" -> result.offset,
"limit" -> result.limit,
"total" -> result.total,
"data" -> result.data
)
}
}