Skip to content

Instantly share code, notes, and snippets.

@bigjason
Created June 1, 2016 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigjason/094c10d3414b705ca9da36f6804adcc1 to your computer and use it in GitHub Desktop.
Save bigjason/094c10d3414b705ca9da36f6804adcc1 to your computer and use it in GitHub Desktop.
package object {
implicit def nelFormat[A](implicit format: Format[A]): Format[scalaz.NonEmptyList[A]] = new Format[scalaz.NonEmptyList[A]] {
import scalaz._, Scalaz._
override def writes(o: NonEmptyList[A]) = JsArray(o.toList.map(Json.toJson(_)))
override def reads(json: JsValue) = Json.fromJson[List[A]](json) match {
case JsSuccess(Nil, _) => JsError("empty list not valid")
case JsSuccess(head :: rest, _) => JsSuccess(NonEmptyList.nel(head, rest))
case JsError(errors) => JsError(errors)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment