【问题标题】:Serializing Case Class序列化案例类
【发布时间】:2016-08-08 20:00:24
【问题描述】:

这是我一直遇到的一个奇怪问题。我有一个带有许多参数的案例类,包括一个字符串,并且能够使用 Play 的格式直接将其序列化为 JSON。然后,我添加了另一个参数 - 一个字符串 - 它开始抱怨

未找到 unapply 或 unapplySeq 函数

原来的样子是这样的:

case class PushMessage(stageOne: Boolean, stageTwo: Boolean, stageThree: Boolean, stageFour: Boolean, stageFive: Boolean,
                   highestStage: Int, iOSTotal: Int, androidTotal: Int, iOSRunningCount: Int, androidRunningCount: Int,
                   vendorId: String, androidProblem: Boolean, iOSComplete: Boolean, androidComplete: Boolean,
                   totalStageThrees: Int, totalStageFours: Int, totalStageFives: Int, expectedTotals: Int,
                   latestUpdate: Long, iOSProblem: Boolean, startTime: Long, date: Long)

新的看起来像

case class PushMessage(stageOne: Boolean, stageTwo: Boolean, stageThree: Boolean, stageFour: Boolean, stageFive: Boolean,
                   highestStage: Int, iOSTotal: Int, androidTotal: Int, iOSRunningCount: Int, androidRunningCount: Int,
                   vendorId: String, androidProblem: Boolean, iOSComplete: Boolean, androidComplete: Boolean,
                   totalStageThrees: Int, totalStageFours: Int, totalStageFives: Int, expectedTotals: Int,
                   latestUpdate: Long, iOSProblem: Boolean, startTime: Long, date: Long, topics: String)

唯一的区别是参数主题。

我的序列化器看起来像:

    object PushMessage {

  implicit val pushMessageFormat = Json.format[PushMessage]

}

我们将不胜感激。谢谢。

【问题讨论】:

    标签: json scala serialization


    【解决方案1】:

    Play 使用宏和元组来派生 Json 实例。问题是 Scala 中的元组被限制为 22 个字段(至少目前如此)。

    这意味着 Play 将无法自动派生 Json 实例,尽管您可以通过手动编写来解决它。

    您可以在这里找到更多信息:Play Framework Scala format large JSON (No unapply or unapplySeq function found)

    【讨论】:

      猜你喜欢
      • 2017-03-29
      • 2011-05-03
      • 2011-12-06
      • 2016-07-02
      • 1970-01-01
      • 2013-08-12
      • 2011-06-29
      • 2013-08-04
      • 2015-08-10
      相关资源
      最近更新 更多