4

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)

  • BEWARE: Both [Json4s](https://github.com/json4s/json4s/issues?utf8=%E29C%93&q=is%3Aissue+is%3Aopen+denial) and [default implementations of Scala maps](https://github.com/scala/bug/issues/11203) are vulnerable under DoS/DoW attacks! – Andriy Plokhotnyuk Oct 22 '20 at 06:11

2 Answers2

0

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
mfirry
  • 3,634
  • 1
  • 26
  • 36
0

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() {
    ...
  }
psyanite
  • 89
  • 2
  • 10