How can I construct and parse a JSON string in Scala / Lift
- by David Carlson
I am using JsonResponse to send some JSON to the client.
To test that I am sending the correct response it seemed natural to me to parse the resulting JSON and validate against a data structure rather than comparing substrings.
But for some reason I am unable to parse the JSON I just constructed:
def tryToParse = {
val jsObj :JsObj = JsObj(("foo", "bar")); // 1)
val jsObjStr :String = jsObj.toJsCmd // 2) jsObjStr is: "{'foo': 'bar'}"
val result = JSON.parseFull(jsObjStr) // 3) result is: None
// the problem seems to be caused by the quotes:
val works = JSON.parseFull("{\"foo\" : \"bar\"}") // 4) result is: Some(Map(foo -> bar))
val doesntWork = JSON.parseFull("{'foo' : 'bar'}") // 5) result is: None
}
How do I programmatically construct a valid JSON message in Scala/Lift that can also be parsed again?