All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.api.libs.json.ops.v4.ImplicitEmptyIterableReads.scala Maven / Gradle / Ivy

package play.api.libs.json.ops.v4

import play.api.libs.json.{JsArray, JsError, JsSuccess, Json, Reads, Writes}

/**
 * Creates implicits for reading empty collection types, such as List[Nothing].
 *
 * Useful for avoiding compile-time errors when decoding as an empty collection,
 * where the compiler cannot or does not need to infer the item type of the collection.
 */
private[ops] trait ImplicitEmptyIterableReads {

  implicit val readsEmptyIterable: Reads[Iterable[Nothing]] = Reads {
    case JsArray(arr) if arr.isEmpty =>
      JsSuccess(Iterable.empty)
    case unexpected =>
      JsError(s"Unexpected value for empty iterable reader: $unexpected")
  }

  implicit val readsEmptySeq: Reads[Seq[Nothing]] = readsEmptyIterable.map(_.toSeq)

  implicit val readsEmptyList: Reads[List[Nothing]] = readsEmptyIterable.map(_.toList)

  implicit val writesEmptyIterable: Writes[Iterable[Nothing]] = Writes(_ => Json.arr())
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy