0

I am trying to read the below json into scala case class. I am able to bind the case class to json using json4s.

The problem is the expectedTypes would change for every table. It could be more or less number of elements and the name would be different. How to create a case class for this requirement?

{
  "filepattern": "product*.gzip",
  "replaceheader": "productid,name,market",
  "dataType": [
    {
      "expectedTypes": {
        "productId": "DOUBLE",
        "name": "STRING"
      }
    }
  ]
}

case class ExpectedTypes(
  productid: String,
  name: String
)
case class DataType(
  expectedTypes: ExpectedTypes
)
case class table(
  filepattern: String,
  replaceheader: Option[String],
  dataType: List[DataType]
)

tkroman
  • 4,811
  • 1
  • 26
  • 46
rakesh jayaram
  • 63
  • 1
  • 10
  • BEWARE: Json4s is [vulnerable under DoS/DoW attacks](https://github.com/json4s/json4s/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+denial)! – Andriy Plokhotnyuk May 06 '20 at 05:48

1 Answers1

2

If it's not predictable how many fields are you going to have in expectedTypes, you can use Map:

case class Root(
filepattern: String,
replaceheader: Option[String],
dataType: List[DataType]
)

case class DataType(
  expectedTypes: Map[String, String]
)
Bob
  • 1,351
  • 11
  • 28
  • BEWARE: Scala's default `Map` implementation is vulnerable under DoS/DoW attacks that exploit it's weak hash collision resolving algorithm: https://github.com/scala/bug/issues/11203 Moreover, Json4s suffers from that itself: https://github.com/json4s/json4s/issues/553 – Andriy Plokhotnyuk Apr 19 '20 at 14:42