Play JSON fmwk unable to deserialize JSON coming from MongoDB

Hello Friends,

Our application uses Play, ReactiveMongo Scala driver and Mongo DB. We have the following versions of these components:

“com.typesafe.play” %% “play-json” % “2.5.18”
“ai.x” %% “play-json-extensions” % “0.9.0”
“com.typesafe.play” %% “play” % “2.5.18”
“com.typesafe.play” %% “play-ws” % “2.5.18”
“org.reactivemongo” %% “play2-reactivemongo” % “0.20.3-play25”
“org.reactivemongo” %% “reactivemongo-play-json” % “0.20.3-play25”
“org.reactivemongo” % “reactivemongo-bson_2.11” % “0.20.3”
“org.reactivemongo” %% “reactivemongo” % “0.20.3”

There is a JSON object in MongoDB that looks like this:
“status” : {
“siteStatus” : “InProgress”,
"timestamp" : NumberLong(“1585696318513”),
“timestampLocalDateTime” : “2020-03-31T23:11:58”
}

When our application tries to read this object, we get this error:
JsResultException(errors:List((,List(ValidationError(List(error.expected.jsnumber),WrappedArray())))))

The attribute that is causing this error is this: “timestamp” : NumberLong(“1585696318513”). Play Framework expects it to be in this format: “timestamp” : “1585696318513” but Mongo DB stores it by adding some additional type information which they call Extended-JSON.

The Scala class corresponding to this JSON is:

case class TemplateSiteStatus(siteStatus: SiteStatus, timestamp: Long, timestampLocalDateTime: Option[LocalDateTime] = None, msg: Option[String] = None)

object TemplateSiteStatus {
implicit val format: OFormat[TemplateSiteStatus] = Json.format[TemplateSiteStatus]
}

We never had any issues parsing this JSON when we were using the older version of the reactivemongo driver that is compatible with the following play-reactiveMongo framework:

“org.reactivemongo” %% “play2-reactivemongo” % “0.12.4”

This error only started happening after we upgraded to “play2-reactivemongo” % “0.20.3-play25”. Any idea how to work around this? We need to use the newer version for different reasons and cannot stay on the older version. Please help.

Thank you.

You are trying to map NumberLong Jsobject with type Long. That is why you are getting error. You can create a NumberLong($numberLong:Long) case class and use that as timestamp’s type instead of Long.