Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby 1.9.1 built-in JSON troubles

Reply
Thread Tools

Ruby 1.9.1 built-in JSON troubles

 
 
Aaron D. Gifford
Guest
Posts: n/a
 
      03-06-2010
I'm puzzled. On a box running Ruby 1.9.1 I try this:

user@host:/path$ irb
irb(main):001:0> require 'json'
=> true
irb(main):002:0> JSON.parse(JSON.generate('\\'))
JSON:arserError: 598: unexpected token at '"\\"'
from /usr/local/lib/ruby/1.9/json/common.rb:122:in `parse'
from /usr/local/lib/ruby/1.9/json/common.rb:122:in `parse'
from (irb):2
from /usr/local/bin/irb:12:in `<main>'
irb(main):003:0> RUBY_VERSION
=> "1.9.1"
irb(main):004:0> ^D

This is the JSON module that came with 1.9--I don't have a JSON gem installed.

Any ideas where to go looking? A little web searching showed me
various similar problems, but most were under 1.8.

Looking for a clue,
Aaron out.

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      03-08-2010
Aaron D. Gifford wrote:
> irb(main):002:0> JSON.parse(JSON.generate('\\'))
> JSON:arserError: 598: unexpected token at '"\\"'


This is simply because JSON.parse only parses JSON *objects* and
*arrays*, not strings or numbers.

>> JSON.parse('{"foo":"bar"}')

=> {"foo"=>"bar"}
>> JSON.parse('["foo","bar"]')

=> ["foo", "bar"]
>> JSON.parse('"bar"')

JSON:arserError: 574: unexpected token at '"bar"'

I use the following workaround:

>> json = JSON.generate('\\')

=> "\"\\\\\""
>> JSON.parse("[" + json + "]").first

=> "\\"

HTH,

Brian.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Aaron D. Gifford
Guest
Posts: n/a
 
      03-08-2010
On Mon, Mar 8, 2010 at 5:39 AM, Brian Candler <(E-Mail Removed)> wrote:
> Aaron D. Gifford wrote:
>> irb(main):002:0> JSON.parse(JSON.generate('\\'))
>> JSON:arserError: 598: unexpected token at '"\\"'

>
> This is simply because JSON.parse only parses JSON *objects* and
> *arrays*, not strings or numbers.
>
>>> JSON.parse('{"foo":"bar"}')

> => {"foo"=>"bar"}
>>> JSON.parse('["foo","bar"]')

> => ["foo", "bar"]
>>> JSON.parse('"bar"')

> JSON:arserError: 574: unexpected token at '"bar"'
>
> I use the following workaround:
>
>>> json = JSON.generate('\\')

> => "\"\\\\\""
>>> JSON.parse("[" + json + "]").first

> => "\\"
>
> HTH,
>
> Brian.


Thanks for the note, Brian.

The JSON documentation does NOT make that clear.

And that limitation is a terrible limitation. Ugly!

I ended up rolling my own pure Ruby JSON parser that doesn't have that
work-around requirement. It looks like I could have avoided that with
your work-around (even though it's sad that the JSON library requires
such an ugly kludge).

Aaron out.

 
Reply With Quote
 
Aaron D. Gifford
Guest
Posts: n/a
 
      03-08-2010
As per Brian's suggestion, I used his workaround this way:
(IMHO, JSON ought to handle this automagically.)

require 'json'
def json_parse_kludge(data)
return JSON.parse('[' + data + ']')[0] if data.is_a?(String) &&
data[0,1] == '"'
JSON.parse(data)
end

Fixes my issue: json_parse_kludge(JSON.generate('\\'))

irb(main):001:0> require 'json'
=> true
irb(main):002:0> def json_parse_kludge(data)
irb(main):003:1> return JSON.parse('[' + data + ']')[0] if
data.is_a?(String) && data[0,1] == '"'
irb(main):004:1> JSON.parse(data)
irb(main):005:1> end
=> nil
irb(main):006:0> json_parse_kludge(JSON.generate('//'))
=> "//"
irb(main):007:0>

Thanks again, Brian!

Aaron out.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      03-08-2010
Aaron D. Gifford wrote:
> Thanks for the note, Brian.
>
> The JSON documentation does NOT make that clear.


I could understand if it only allowed an object at the top level (which
is what CouchDB requires), but I agree it doesn't make sense to allow
two types of values but not the other types.

Your kludge is a bit messy, it won't parse ' "foo"' for example (with a
leading space). If you are afraid of building an extra string, then how
about:

def jparse(str)
return JSON.parse(str) if str =~ /\A\s*[{\[]/
JSON.parse("[#{str}]")[0]
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Aaron D. Gifford
Guest
Posts: n/a
 
      03-09-2010
On Mon, Mar 8, 2010 at 12:19 PM, Brian Candler <(E-Mail Removed)> wrote:
>... If you are afraid of building an extra string, then how
> about:
>
> def jparse(str)
> =A0return JSON.parse(str) if str =3D~ /\A\s*[{\[]/
> =A0JSON.parse("[#{str}]")[0]
> end


Definitely more robust, since that will handle numbers too. Thanks

Aaron out.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Acácio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at json.org Tuğrul Topuz ASP General 1 06-27-2008 11:37 PM
[ANN] Orbjson, a JSON-RPC ORB for JavaScript/Ruby interaction James Britt Ruby 6 03-04-2005 05:50 AM



Advertisments