I've seen some libraries in Scala that can (de)serialize automatically any case class that has supported member types automatically to i.e. JSON.
In the Android world, I'd like to be able to do so with Intent and Bundle.
Example, I'd like this boilerplate code to be generated:
case class Ambitos(idInc: Long, idGrupo: String, ambitos: Map[String, Seq[String]])
def serialize(b: Bundle) {
b.putString("grupo", idGrupo)
b.putLong("inc", idInc)
b.putStringArray("ambitos", ambitos.keys.toArray)
ambitos.foreach { case (a, det) ⇒
b.putStringArray(a, det.toArray)
}
}
def serialize(b: Intent) {
b.putExtra("grupo", idGrupo)
b.putExtra("inc", idInc)
b.putExtra("ambitos", ambitos.keys.toArray)
ambitos.foreach { case (a, det) ⇒
b.putExtra(a, det.toArray)
}
}
}
object Ambitos {
def apply(b: Intent): Ambitos =
Ambitos(b.getLongExtra("inc", -1), b.getStringExtra("grupo"),
b.getStringArrayExtra("ambitos").map{ a ⇒ (a, b.getStringArrayExtra(a).toSeq) }.toMap)
def apply(b: Bundle): Ambitos =
Ambitos(b.getLong("inc"), b.getString("grupo"),
b.getStringArray("ambitos").map{ a ⇒ (a, b.getStringArray(a).toSeq) }.toMap)
}
Does exist such a library or do I have to make it by myself?
For passing complex information between Activities and for handling Activity onSaveInstanceState()
and onRestoreInstanceState()
, this tool would be awesome.