jsonscalalift-json

Compilation error in code snippet by lift-json 3.0.1 to convert json arrays into comma-separated-strings


I'm trying to consume a json-formatted text and convert it into xml. I'm utilising lift-json for that matter. According to the lift-json documentation here (def toXml), I should be able to convert elements of json arrays into comma separated strings using:

toXml(json map {
  case JField("nums",JArray(ns)) => JField("nums",JString(ns.map(_.values).mkString(",")))
  case x => x
})

So I wrote the following code:

case work: ActiveMQTextMessage => 
  println("work.getText: " + work.getText)
  val workAsJson: JValue = parse(work.getText)
  val processedArraysJson = workAsJson map {
    case JField(label, JArray(ns)) => JField(label, JString(ns.map(_.values).mkString(",")))
    case x => x
  }
  val workAsXml: scala.xml.NodeSeq = toXml(processedArraysJson)

But for some reason it doesn't compile.

It reports two errors:

Error:(55, 14) constructor cannot be instantiated to expected type;
 found   : net.liftweb.json.JsonAST.JField
 required: net.liftweb.json.JsonAST.JValue
        case JField(label, JArray(ns)) => JField(label, JString(ns.map(_.values).mkString(",")))

Error:(55, 49) type mismatch;
 found   : net.liftweb.json.JsonAST.JField
 required: net.liftweb.json.JsonAST.JValue
        case JField(label, JArray(ns)) => JField(label, JString(ns.map(_.values).mkString(",")))

Notice, version of lift-json I'm using is:

"net.liftweb" % "lift-json_2.12" % "3.0.1"

with scala 2.12


Solution

  • The issue here is that Lift 3.0 changed some inconsistencies in lift-json's usage of map. JField was once a JValue, but that is no longer the case as it didn't make conceptual sense. To map over fields, you now must use mapField. Changing map to mapField in the above code should be enough, and I've issued a PR to update the documentation as well.

    (Note that you'll usually get quicker answers on the Lift Google group.)