Created
November 18, 2012 06:25
-
-
Save arschles/4103868 to your computer and use it in GitHub Desktop.
making lift-json easier
This file contains 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
import scalaz._ | |
import Scalaz._ | |
import net.liftweb.json._ | |
import net.liftweb.json.scalaz.JsonScalaz._ | |
def json[T](serialize: T => JValue, deserialize: JValue => Result[T]): JSON[T] = new JSON[T] { | |
def write(t: T) = serialize(t) | |
def read(jval: JValue): Result[T] = deserialize(jval) | |
} | |
//usage | |
case class MyClass(i: Int) | |
implicit val myClassSerializer = json[MyClass]( | |
serialize = C => i: JValue, | |
deserialize = j => ???? | |
) |
nothing that sophisticated. I wrote this to help avoid writing the boilerplate of writing new JSON[T] {....}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is nicer, but I'd still have to do
There's still the possibility to get this wrong with a stupid mistake, like copying a line and ending up with the wrong string tied to the wrong field. Perhaps something like this that gets the field types from reflection? Presumably that's what other case class serializers do