Skip to content

Instantly share code, notes, and snippets.

@nelanka
Last active August 29, 2015 14:27
Show Gist options
  • Save nelanka/241e77153edcc27b5a06 to your computer and use it in GitHub Desktop.
Save nelanka/241e77153edcc27b5a06 to your computer and use it in GitHub Desktop.
Serialize and deserialize case classes with helper object
case class IdentifierKey(idType: String, id: String) {
override def toString: String = SerializedIdentifierKey(this)
}
object SerializedIdentifierKey extends (IdentifierKey => String) {
val delimiter = ':'
def apply(i: IdentifierKey): String = i.idType + SerializedIdentifierKey.delimiter + i.id
def unapply(s: String): Option[IdentifierKey] = {
val Array(idType, id) = s.split(SerializedIdentifierKey.delimiter)
Some(IdentifierKey(idType, id))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment