I am attempting to use JSON to send data between the browser and my app.
I am attempting to use Lift 1.0 to create and parse JSON strings, but for some reason I am unable to parse the JSON I just constructed:
scala>import scala.util.parsing.json.JSON._
import scala.util.parsing.json.JSON._
scala> import net.liftweb.http.js._
import net.liftweb.http.js._
scala> import net.liftweb.http.js.JE._
import net.liftweb.http.js.JE._
scala> val json = JsObj(("foo", 4), ("bar", "baz")).toJsCmd
json: String = {'foo': 4, 'bar': 'baz'}
scala> parseFull(json)
res3: Option[Any] = None
How do I programmatically construct a valid JSON message in Scala/Lift that can also be parsed again?
You are using Lift 1.0’s
JsCmd, which produces JSON with single-quoted strings and attempting to parse it with scala’s parser, which only supports double-quoted strings.It is important to realize that there are multiple definitions for JSON.
Are single-quoted strings valid in JSON?
Lift and Scala provide many ways to parse JSON, sometimes with differing behavior between versions.
The strings accepted by these parsers are not equivalent.
Here are some comments and examples of the various methods to product and parse JSON strings.
Producing JSON with the lift-json library DSL
example:
Parsing JSON with the lift-json library
com.thoughtworks.paranamer.ParameterNamesNotFoundException: Unable to get class bytes)PublicID, a pre-existing scala case-class so that it will work on the scala console.example:
Parsing JSON in scala 2.7.7 and 2.8.1
example:
Parsing JSON in Lift 2.0 and 2.2 with util.JSONParser
example:
Parsing JSON in Lift 2.0 and 2.2 with json.JsonParser
example:
Producing JSON with Lift 1.0 JsCmd
example:
Producing JSON with Lift 2.0 JsCmd
example:
Producing JSON in scala (tested with 2.10)
example: