Compile error in code snippet using lift-json 3.0.1 to convert json arrays to comma separated strings
I am trying to use json formatted text and convert it to xml. I am using lift-json . According to the lift-json documentation here ( def toXml
), I should be able to convert the elements of json arrays to 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.
Two errors are reported:
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(",")))
Please note that the lift-json version I am using is:
"net.liftweb" % "lift-json_2.12" % "3.0.1"
with scala 2.12
source to share
The problem here is that Lift 3.0 changed some of the inconsistencies in the use of lifting map
. JField
was once a JValue
, but this is no longer the case, since it did not make conceptual sense. To map fields you must use mapField
. Changing map
to mapField
in the above code should be enough and I have issued a PR to update the documentation .
(Note that you'll usually get faster responses on the Boost Google group .)
source to share