Created
June 1, 2016 21:53
-
-
Save bigjason/094c10d3414b705ca9da36f6804adcc1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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