Lift JSON JValue Extract issue

942 views Asked by At
import net.liftweb.json._
import net.liftweb.json.JsonAST._
import net.liftweb.json.Extraction._
import net.liftweb.json.Printer._

implicit val formats = net.liftweb.json.DefaultFormats

val jV = JArray(List(JInt(10),JString("ahem"),JBool(false)))

I am dealing with a situation of mixed types and attempting to convert Jv to a List[Strings] using

jV.extract[List[String]]

The extraction does not work.

Can someone tell me how should I go about doing this

1

There are 1 answers

0
Aaron On

Lift JSON doesn't have a conversion between Strings and JBools defined in the serialisers.

Does the List inside of the Array always have the same shape? If so, then you can do something like:

case class Datum(id: BigInt, comment: String, bool: Boolean)

val data = jv.extract[List[Datum]]

If that won't work for you as there isn't a uniform shape but you still just want a list of Strings, then you can transform the JBools into JStrings before trying to do the extraction:

jv.map({
  case JBool(bool) => if (bool) JString("true") else JString("false")
  case x => x
}).extract[List[String]]

In general though, I'd encourage you think about why you are discarding the type information here. Quite a lot of Scala's power comes from it's type system so it's better to use it than to lose it by string-typing things here.