I've been fighting with the concept of the functional way of parsing a JSON string in Scala and ran flat into the wall with Option(something) being returned. I popped the questionand the helpful answers came streaming in.
The problem is, as someone being pretty new to Scala, what is the correct way?
Currently I'm doing this:
import util.parsing.json.JSON._
object JsonSoap {
def main(args: Array[String]) {
val x = parseFull("""{"name":"Joe","surname":"SOAP"}""")
val y = x collect {
case m: Map[_, _] => m collect {
case (key: String, value: String) => key -> value
}
}
val z = for (m <- y; name <- m.get("name"); surname <- m.get("surname"))
yield {
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Person>
<Name>{name}</Name>
<Surname>{surname}</Surname>
</Person>
</soap:Body>
</soap:Envelope>
}
println(z)
}
}
I'm still stuck with Some()
Is there a nice pattern to solving my problem? Surely this has to be well explored territory. How can I improve my code?