lift-json escaping bug

238 views
Skip to first unread message

harryh

unread,
Nov 30, 2009, 5:16:13 PM11/30/09
to Lift
scala> import net.liftweb.json._
scala> val s2 = "{ \"id\": \"America\\/New_York\" }"
s2: java.lang.String = { "id": "America\/New_York" }

scala> JsonParser.parse(s2)
res1: net.liftweb.json.JsonAST.JValue = JObject(List(JField(id,JString
(America\New_York))))

It should be America/New_York but for some reason getting a \ instead
of a /

-harryh

Peter Robinett

unread,
Nov 30, 2009, 6:18:10 PM11/30/09
to Lift
Harry, I think you're double-escaping the slash. This works:
scala> import net.liftweb.json._
scala> val s1 = "{ \"id\": \"America/New_York\" }"
s1: java.lang.String = { "id": "America/New_York" }

scala> JsonParser.parse(s1)
res0: net.liftweb.json.JsonAST.JValue = JObject(List(JField(id,JString
(America/New_York))))

Peter Robinett

Ross Mellgren

unread,
Nov 30, 2009, 6:20:04 PM11/30/09
to lif...@googlegroups.com
He's double escaping so that scala's string interpretation will put a
raw \ in there, so that it's an escaped forward slash (\/) to the JSON
parson, as I understand it. The output should be either invalid escape
or forward slash, but not backslash unless the input was \\.

-Ross
> --
>
> You received this message because you are subscribed to the Google
> Groups "Lift" group.
> To post to this group, send email to lif...@googlegroups.com.
> To unsubscribe from this group, send email to liftweb+u...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/liftweb?hl=en
> .
>
>

harryh

unread,
Nov 30, 2009, 6:30:20 PM11/30/09
to Lift
Yes, what Ross said. Further, taking a look at JsonParser.scala the
bug appears to be on line ~202 where there are a couple of missing
escape sequences: \/ as well as \f.

-harryh

Ross Mellgren

unread,
Nov 30, 2009, 6:33:26 PM11/30/09
to lif...@googlegroups.com
If you file an issue on github I'll write up a patch for you tonight.

-Ross

Nathan Folkman (Foursquare)

unread,
Nov 30, 2009, 6:34:46 PM11/30/09
to Lift
Here's the complete list from http://json.org/:

char:

Any Unicode character except " or \ or control-character.

\"
\\
\/
\b
\f
\n
\r
\t
\u four-hex-digits

- n

Ross Mellgren

unread,
Nov 30, 2009, 6:42:41 PM11/30/09
to lif...@googlegroups.com
What I find particularly interesting is that the JSON spec lacks \',
but gains \/, relative to the ECMA-262 (javascript) spec that JSON
supposedly derives from.

-Ross

harryh

unread,
Nov 30, 2009, 8:33:41 PM11/30/09
to Lift

Ross Mellgren

unread,
Nov 30, 2009, 10:22:13 PM11/30/09
to lif...@googlegroups.com
On review board: http://reviewboard.liftweb.net/r/131/

I did run across another infelicity when writing the test -- apparently JsonParser crashes when given a scalar value, as opposed to an array or object:

scala> parse("\"foobar\"")
net.liftweb.json.JsonParser$ParseException: unexpected null
Near: "foobar
at net.liftweb.json.JsonParser$Parser.fail(JsonParser.scala:166)
at net.liftweb.json.JsonParser$ValStack.peek(JsonParser.scala:153)
at net.liftweb.json.JsonParser$.newValue$1(JsonParser.scala:110)
at net.liftweb.json.JsonParser$.parse0(JsonParser.scala:125)
at net.liftweb.json.JsonParser$.parse(JsonParser.s...

I'm not sure if it should work, but even if it shouldn't support that the error case should probably give a better message. Joni/et al?

-Ross

Joni Freeman

unread,
Dec 1, 2009, 1:04:37 AM12/1/09
to Lift
Hi,

This is from JSON RFC (http://www.ietf.org/rfc/rfc4627.txt): "A JSON
text is a serialized object or array."

I checked with another parser (http://www.jsonlint.com) and it fails
too when given just JSON value:
"syntax error, unexpected TNUMBER, expecting '{' or '[' at line 1"

So perhaps a better error message is a proper fix here?

Cheers Joni

Ross Mellgren

unread,
Dec 1, 2009, 6:49:39 PM12/1/09
to lif...@googlegroups.com
On Nov 30, 2009, at 8:33 PM, harryh wrote:

Reply all
Reply to author
Forward
0 new messages