0

Need to convert NaN in Json(as it is not JSON) to Double in Json4s without using jackson.

For example, I am having following JSON:

{ "a": NaN }

I need to parse above json using JSON4S.

Can we write any deserializer if possible for this?

Devavrata
  • 1,785
  • 17
  • 30
  • 2
    There is no way to do that because json4s parser doesn't know how to read NaN values. – Sebastian Celestino Sep 14 '18 at 15:34
  • @SebastianCelestino Is there any workaround to solve this? – Devavrata Sep 15 '18 at 08:38
  • 1
    Here is a branch with changes in json4s that you need https://github.com/scelestino/json4s/tree/feature-NaN-support, but I think that is a change that cannot be done official because jackson doesn't support NaN (json4s works with both of them, native and jackson) – Sebastian Celestino Sep 16 '18 at 03:21

1 Answers1

1

You can achieve this from the version 3.6.7:

import org.json4s._
import org.json4s.native.JsonMethods._

def main(args: Array[String]): Unit = {
  val json = """{"a": "NaN"}"""
  println(parse(json))
 // Displays
 // JObject(List((a,JString(NaN))))
}
Valy Dia
  • 2,781
  • 2
  • 12
  • 32