Created
July 24, 2014 02:33
-
-
Save farmdawgnation/5470ac82f520936b5835 to your computer and use it in GitHub Desktop.
Singleton Serializer
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
abstract class SingletonSerializer[T](implicit mf: Manifest[T]) extends Serializer[T] { | |
val SingletonClass: Class[T] = mf.runtimeClass.asInstanceOf[Class[T]] | |
def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), T] = { | |
case (TypeInfo(SingletonClass, _), json) if json.extractOpt[String].map(_ endsWith "$").getOrElse(false) => | |
val className = json.extract[String] | |
Class.forName(className).getField("MODULE$").get().asInstanceOf[T] | |
} | |
def serialize(implicit format: Formats) = { | |
case possibleSingleton if SingletonClass.isInstance(possibleSingleton) && possibleSingleton.getClass.getName().endsWith("$") => | |
possibleSingleton.getClass.getName() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment