Skip to content

Instantly share code, notes, and snippets.

@trane
Created July 15, 2016 01:11
Show Gist options
  • Save trane/f1ba48cadd8fbe2de6378808b6c73cd8 to your computer and use it in GitHub Desktop.
Save trane/f1ba48cadd8fbe2de6378808b6c73cd8 to your computer and use it in GitHub Desktop.
def writes(o: T): JsObject = {
val canonicalName = Option(clazz.getCanonicalName()).getOrElse(throw new IllegalArgumentException("Cannot serialize this class because its canonical name is null. This is most likely because it is an Array or a Local or Anonymous class."))
JsObject(Seq("className" -> JsString(canonicalName)))
}
// i find using the container type in one place better for understanding
// since we are threading transformations and operations on the Option until
// the end when we either have a result or we throw an exception
def writes(o: T): JsObject =
Option(clazz.getCanonicalName())
.map(JsObject(Seq("className" -> JsString(_))))
.getOrElse(throw new IllegalArgumentException("Cannot serialize this class because its canonical name is null. This is most likely because it is an Array or a Local or Anonymous class.")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment