4
votes

I have a Seq of these:

case class IntPane(
                    override val duration: FiniteDuration,
                    override val values: mutable.Map[String, Int],
                    override val default: Int)
  extends BasePane[Int](duration, values, default)

I can serialize them by calling writes(), using this formatter:

implicit val formats: Formats = DefaultFormats +
    FieldSerializer[scala.collection.mutable.Map[String, Int]]() +

However, I wish to ignore some fields, so I add these lines:

implicit val formats: Formats = DefaultFormats +
FieldSerializer[scala.collection.mutable.Map[String, Int]]() +
FieldSerializer[IntPane](ignore("duration")) +
FieldSerializer[IntPane](ignore("default"))

Now, serialization fails with this cryptic exception:

org.json4s.package$MappingException: Classes defined in method bodies are not supported. at org.json4s.reflect.package$.fail(package.scala:93) at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$createConstructorDescriptors$3$$anonfun$15.apply(Reflector.scala:139) at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$createConstructorDescriptors$3$$anonfun$15.apply(Reflector.scala:135) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59) at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47) at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) at scala.collection.AbstractTraversable.map(Traversable.scala:105) at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$createConstructorDescriptors$3.apply(Reflector.scala:135)

2
BEWARE: Both Json4s and default implementations of Scala maps are vulnerable under DoS/DoW attacks!Andriy Plokhotnyuk

2 Answers

0
votes

I've given it a quick try and the following seems to work:

val intPaneFieldSerializer = FieldSerializer[IntPane](ignore("duration") orElse ignore("default"))

implicit val formats: Formats = DefaultFormats + FieldSerializer[scala.collection.mutable.Map[String, Int]]() + intPaneFieldSerializer
0
votes

Classes defined in method bodies are not supported.

The error message indicates that the case class IntPane is defined inside a method.

  def myMethod() {
    case class IntPane(...)
  }

If the the IntPane case class definition is moved outside of the method, it could fix the issue.

  case class IntPane(...)

  def myMethod() {
    ...
  }