scala json serialization missing fields (json4s)

401 views Asked by At

I am using json4s to serialize some scala map objects.

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))

Correct result would be:

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

strangely I get only for the serialized class

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

How to get it serialize correctly?

1

There are 1 answers

0
Georg Heiler On BEST ANSWER

It turns out json4s was lacking the correct serializer. Maybe it would be better to be notified via an exception?

Anyway: adding

+ FieldSerializer[StatCounter]()

to the DefaultFormats fixes the problem