Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are using Lift 1.0's <code>JsCmd</code>, which produces JSON with single-quoted strings and attempting to parse it with scala's parser, which only supports double-quoted strings.</p> <p>It is important to realize that there are multiple definitions for JSON.</p> <p>Are single-quoted strings valid in JSON?</p> <ul> <li>They are according to <a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf" rel="noreferrer">ECMAScript 5th Ed</a></li> <li>They are not according to Crockford's original <a href="http://www.ietf.org/rfc/rfc4627.txt" rel="noreferrer">RFC 4627</a></li> </ul> <p>Lift and Scala provide many ways to parse JSON, sometimes with differing behavior between versions.</p> <p>The strings accepted by these parsers are not equivalent.</p> <p>Here are some comments and examples of the various methods to product and parse JSON strings.</p> <hr> <h2>Producing JSON with the <a href="https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/" rel="noreferrer">lift-json</a> library DSL</h2> <ul> <li>Recommended</li> <li>Despite its name, this is a separate project with no dependencies on the rest of Lift</li> </ul> <p>example:</p> <pre><code>scala&gt; import net.liftweb.json.JsonAST import net.liftweb.json.JsonAST scala&gt; import net.liftweb.json.JsonDSL._ import net.liftweb.json.JsonDSL._ scala&gt; import net.liftweb.json.Printer._ import net.liftweb.json.Printer._ scala&gt; val json1 = ("foo" -&gt; 4) ~ ("bar" -&gt; "baz") json1: net.liftweb.json.JsonAST.JObject = JObject(List(JField(foo,JInt(4)), JField(bar,JString(baz)))) scala&gt; compact(JsonAST.render(json1)) res0: String = {"foo":4,"bar":"baz"} scala&gt; val json2 = List(1,2,3) json2: List[Int] = List(1, 2, 3) scala&gt; compact(JsonAST.render(json2)) res1: String = [1,2,3] scala&gt; val json3 = ("foo", 4) ~ ("bar", List(1,2,3)) json3: net.liftweb.json.JsonAST.JObject = JObject(List(JField(foo,JInt(4)), JField(bar,JArray(List(JInt(1), JInt(2), JInt(3)))))) scala&gt; compact(JsonAST.render(json3)) res2: String = {"foo":4,"bar":[1,2,3]} </code></pre> <hr> <h2>Parsing JSON with the <a href="https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/" rel="noreferrer">lift-json</a> library</h2> <ul> <li>Recommended</li> <li>Provides implicit mapping to/from scala case-classes</li> <li>Case-classes defined in the console are not currently supported (will throw a <code>com.thoughtworks.paranamer.ParameterNamesNotFoundException: Unable to get class bytes</code>)</li> <li>The example below uses <code><a href="http://www.scala-lang.org/api/current/scala/xml/dtd/PublicID.html" rel="noreferrer">PublicID</a></code>, a pre-existing scala case-class so that it will work on the scala console.</li> </ul> <p>example:</p> <pre><code>scala&gt; import scala.xml.dtd.PublicID import scala.xml.dtd.PublicID scala&gt; import net.liftweb.json._ import net.liftweb.json._ scala&gt; import net.liftweb.json.JsonAST._ import net.liftweb.json.JsonAST._ scala&gt; import net.liftweb.json.JsonDSL._ import net.liftweb.json.JsonDSL._ scala&gt; implicit val formats = DefaultFormats formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@7fa27edd scala&gt; val jsonAst = ("publicId1" -&gt; "idString") ~ ("systemId" -&gt; "systemIdStr") jsonAst: net.liftweb.json.JsonAST.JObject = JObject(List(JField(publicId,JString(idString)), JField(systemId,JString(systemIdStr)))) scala&gt; jsonAst.extract[PublicID] res0: scala.xml.dtd.PublicID = PUBLIC "idString" "systemIdStr" </code></pre> <hr> <h2>Parsing JSON in scala 2.7.7 and 2.8.1</h2> <ul> <li>Not Recommended - "<a href="http://www.quora.com/How-do-I-parse-a-JSON-string-in-scala" rel="noreferrer">No longer really supported</a>"</li> <li>Scala 2.7.7's parser will not parse single-quoted JSON </li> <li>This parsing method used in the question</li> </ul> <p>example:</p> <pre><code>scala&gt;import scala.util.parsing.json.JSON._ import scala.util.parsing.json.JSON._ scala&gt; parseFull("{\"foo\" : 4 }") res1: Option[Any] = Some(Map(foo -&gt; 4.0)) scala&gt; parseFull("[ 1,2,3 ]") res2: Option[Any] = Some(List(1.0, 2.0, 3.0)) scala&gt; parseFull("{'foo' : 4 }") res3: Option[Any] = None </code></pre> <hr> <h2>Parsing JSON in Lift 2.0 and 2.2 with <a href="http://main.scala-tools.org/mvnsites/liftweb-2.0/framework/scaladocs/net/liftweb/util/JSONParser$object.html" rel="noreferrer">util.JSONParser</a></h2> <ul> <li>Neutral Recommendation</li> <li>Lift's util.JSONParser will parse single- or double-quoted JSON strings:</li> </ul> <p>example:</p> <pre><code>scala&gt; import net.liftweb.util.JSONParser._ import net.liftweb.util.JSONParser._ scala&gt; parse("{\"foo\" : 4 }") res1: net.liftweb.common.Box[Any] = Full(Map(foo -&gt; 4.0)) scala&gt; parse("[ 1,2,3 ]") res2: net.liftweb.common.Box[Any] = Full(List(1.0, 2.0, 3.0)) scala&gt; parse("{'foo' : 4}") res3: net.liftweb.common.Box[Any] = Full(Map(foo -&gt; 4.0)) </code></pre> <hr> <h2>Parsing JSON in Lift 2.0 and 2.2 with <a href="http://main.scala-tools.org/mvnsites/liftweb-2.0/framework/scaladocs/net/liftweb/json/JsonParser$object.html" rel="noreferrer">json.JsonParser</a></h2> <ul> <li>Neutral Recommendation</li> <li>Lift's json.JsonParser will not parse single-quoted JSON strings:</li> </ul> <p>example:</p> <pre><code>scala&gt; import net.liftweb.json._ import net.liftweb.json._ scala&gt; import net.liftweb.json.JsonParser._ import net.liftweb.json.JsonParser._ scala&gt; parse("{\"foo\" : 4 }") res1: net.liftweb.json.JsonAST.JValue = JObject(List(JField(foo,JInt(4)))) scala&gt; parse("[ 1,2,3 ]") res2: net.liftweb.json.JsonAST.JValue = JArray(List(JInt(1), JInt(2), JInt(3))) scala&gt; parse("{'foo' : 4}") net.liftweb.json.JsonParser$ParseException: unknown token ' Near: {'foo' : 4} at net.liftweb.json.JsonParser$Parser.fail(JsonParser.scala:216) at net.liftweb.json.JsonParser$Parser.nextToken(JsonParser.scala:308) at net.liftweb.json.JsonParser$$anonfun$1.apply(JsonParser.scala:172) at net.liftweb.json.JsonParser$$anonfun$1.apply(JsonParser.scala:129) at net.liftweb.json.JsonParse... </code></pre> <hr> <h2>Producing JSON with Lift 1.0 JsCmd</h2> <ul> <li>Not Recommended - output not valid for all JSON parsers</li> <li>Note the single-quotations around strings:</li> </ul> <p>example:</p> <pre><code>scala&gt; import net.liftweb.http.js._ import net.liftweb.http.js._ scala&gt; import net.liftweb.http.js.JE._ import net.liftweb.http.js.JE._ scala&gt; JsObj(("foo", 4), ("bar", "baz")).toJsCmd res0: String = {'foo': 4, 'bar': 'baz'} scala&gt; JsArray(1,2,3).toJsCmd res1: String = [1, 2, 3] scala&gt; JsObj(("foo", 4), ("bar", JsArray(1,2,3))).toJsCmd res2: String = {'foo': 4, 'bar': [1, 2, 3] } </code></pre> <hr> <h2>Producing JSON with Lift 2.0 JsCmd</h2> <ul> <li>Neutral Recommendation</li> <li>Note the double quotations around strings:</li> </ul> <p>example:</p> <pre><code>scala&gt; import net.liftweb.http.js._ import net.liftweb.http.js._ scala&gt; import net.liftweb.http.js.JE._ import net.liftweb.http.js.JE._ scala&gt; JsObj(("foo", 4), ("bar", "baz")).toJsCmd res0: String = {"foo": 4, "bar": "baz"} scala&gt; JsArray(1,2,3).toJsCmd res1: String = [1, 2, 3] scala&gt; JsObj(("foo", 4), ("bar", JsArray(1,2,3))).toJsCmd res3: String = {"foo": 4, "bar": [1, 2, 3] } </code></pre> <hr> <h2>Producint JSON in scala (tested with 2.10)</h2> <ul> <li>"<a href="http://www.quora.com/How-do-I-parse-a-JSON-string-in-scala" rel="noreferrer">No longer really supported</a>", but it works and it's there.</li> </ul> <p>example:</p> <pre><code>scala&gt; import scala.util.parsing.json._ import scala.util.parsing.json._ scala&gt; JSONObject (Map ("foo" -&gt; 4, "bar" -&gt; JSONArray (1 :: 2 :: 3 :: Nil))) .toString() res0: String = {"foo" : 4, "bar" : [1, 2, 3]} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload