【问题标题】:scala json serialization missing fields (json4s)scala json序列化缺少字段(json4s)
【发布时间】:2017-05-19 07:51:59
【问题描述】:

我正在使用 json4s 序列化一些 scala 映射对象。

import org.apache.spark.util.StatCounter
import org.json4s.DefaultFormats

val m: scala.collection.Map[String, Map[String, StatCounter]] = Map("key" -> Map("secondKey" -> StatCounter()))

implicit val format = DefaultFormats
import org.json4s.jackson.Serialization.write

println(m)
println(write(m))

正确的结果是:

Map(key -> Map(secondKey -> (count: 0, mean: 0,000000, stdev: NaN, max: -Infinity, min: Infinity)))

奇怪的是我只得到序列化类

{"key":{"secondKey":{}}}
res1: Unit = ()

如何正确序列化?

【问题讨论】:

    标签: json scala serialization json4s lift-json


    【解决方案1】:

    原来 json4s 缺少正确的序列化程序。也许通过异常通知会更好?

    无论如何:添加

    + FieldSerializer[StatCounter]()
    

    DefaultFormats 解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多