I have the following case class defined in Scala
case class BitmexTradeData(
foreignNotional: Option[Int],
grossValue: Option[Int],
homeNotional: Option[Double],
price: Double,
side: Side,
size: Double,
symbol: String,
tickDirection: String,
timestamp: Instant,
trdMatchID: String
)
Now when I get the following json string which i wanted to deserialize back to BitmexTradeData:
{
"timestamp": "2019-10-17T01:34:00.000Z",
"symbol": ".BTRXXBT",
"side": "Buy",
"size": 0,
"price": 0.00000187,
"tickDirection": "PlusTick",
"trdMatchID": "00000000-0000-0000-0000-000000000000",
"grossValue": null,
"homeNotional": null,
"foreignNotional": null
}
I get the following error:
spray.json.DeserializationException: Expected Int as JsNumber, but got null
the way I am deserializing it is following:
msg.parseJson.convertTo[BitmexTradeData]
The ouput that I am expecting is BitmexTradeData Object with foreignNotional as optional.empty as some times these value are null.
If you are not locked to use spray-json only then add jsoniter-scala to your dependency list:
Add imports, define your data structures and derive a codec:
After that you can parse and print your input:
Output will be: