(德)序列化对象自动向包 [英] (De)Serializing objects automatically to a bundle

查看:167
本文介绍了(德)序列化对象自动向包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的斯卡拉有些库可(德)自动序列化已支持的成员类型自动即<一个无论如何类href=\"http://stackoverflow.com/questions/23531065/scala-parse-json-directly-into-a-case-class\">JSON.

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.

在Android的世界里,我希望能够与意图,并捆绑这样做。

In the Android world, I'd like to be able to do so with Intent and Bundle.

例子,我想这个样板code要生成:

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?

有关传递活动之间的复杂信息和处理活动 的onSaveInstanceState() onRestoreInstanceState(),这个工具将是真棒。

For passing complex information between Activities and for handling Activity onSaveInstanceState() and onRestoreInstanceState(), this tool would be awesome.

推荐答案

这是尼克为我提供了的 Android的斯卡拉论坛

This is the answer that Nick has provided me in a Android Scala forum:

这应该是相对简单,如果你知道周围宏自己的方式:)
这里有一个序列化对(键,值)

It should be relatively straightforward if you know your way around macros :) Here’s one that serializes pairs (key, value).

从那里,你只需要添加类案​​件检查一下。 <一href=\"https://github.com/jto/validation/blob/master/validation-core/src/main/scala/play/api/data/mapping/MappingMacros.scala#L41\"相对=nofollow>例(有点绕口),也见线89的使用。
关于最后一点,我想使它类型类为基础的,而不是继承为基础的:

From there, you just need to add the case class inspection bit. Example (a bit convoluted), see also line 89 for usage. On a final note, I would make it typeclass-based rather than inheritance-based:

trait Bundleable[A] {
  def toBundle(x: A): Bundle
  def fromBundle(b: Bundle): Try[A]
}

implicit def genBundleable[A]: Bundleable[A] = macro ???

def bundle[A: Bundleable](x: A) =
  implicitly[Bundleable[A]].toBundle(x)

这样,您就可以定义的实例Bundleable [A] 手动和自动。和你的数据模型不被污染与Android无稽之谈。

This way you can define instances of Bundleable[A] both manually and automatically. And your data model is not polluted with Android nonsense.

这篇关于(德)序列化对象自动向包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆