0

I have JSON in following format:

{
  "id": 1913548255,
  "notification": "NotificationReceived",
  "deviceGuid": "e60d6085-2aba-48e9-b1c3-73c673e414be",
  "timestamp": "2016-01-28T20:34:34.167",
  "parameters": {
    "jsonString": "{\"mac\":\"bc6a29abd973\",\"uuid\":\"f000aa1104514000b000000000000000\",\"value\":0.27328648477047685}"
  }
}

I want to deserialize it to get following classes, so that :

case class Parameters(mac: String, uuid: String, value: Double)
case class Notification(id: BigInt, notification: String, deviceGuid: String, timestamp: String, perameters: Parameters)

I know i need to write CustomSerializer. But i don't have much experience. Please, guide me. Thanks for help.

Łukasz
  • 8,555
  • 2
  • 28
  • 51

1 Answers1

0

I decided no to deal with deserializer, but do it in ordinary way. I am posting the code so that it may help someone.

case class Parameters(mac: String, uuid: String, value: Double)
case class Notification(id: Int, notification: String, deviceGuid: String, timestamp: String, parameters: Map[String, String])
case class FinalNotification(id: Int, notification: String, device_guid: String, timestamp: String, mac: String, uuid: String, value: Double)

 implicit val formats = DefaultFormats
 val n = parse(v).extract[Notification]

 def convertJson(json: Option[String]): Parameters = json match {
    case None => throw new IllegalArgumentException("Json can't be converted. ")
    case Some(j) => parse(j).extract[Parameters]
}

val param = convertJson(n.parameters.get("jsonString"))

FinalNotification(n.id, n.notification, n.deviceGuid, n.timestamp, param.mac, param.uuid, param.value)