brianmario / yajl-ruby

A streaming JSON parsing and encoding library for Ruby (C bindings to yajl)
http://rdoc.info/projects/brianmario/yajl-ruby
MIT License
1.48k stars 169 forks source link

JSON.parse('') should raise a ParserError #123

Open rtyler opened 11 years ago

rtyler commented 11 years ago
-> % irb
1.9.3p385 :001 > require 'rubygems'
 => false 
1.9.3p385 :002 > require 'yajl/json_gem'
 => true 
1.9.3p385 :003 > JSON.parse('')
 => nil 
1.9.3p385 :004 > require 'json'
 => true 
1.9.3p385 :005 > JSON.parse('')
JSON::ParserError: A JSON text must at least contain two octets!
        from /home/tyler/.rvm/rubies/ruby-1.9.3-p385/lib64/ruby/1.9.1/json/common.rb:148:in `initialize'
        from /home/tyler/.rvm/rubies/ruby-1.9.3-p385/lib64/ruby/1.9.1/json/common.rb:148:in `new'
        from /home/tyler/.rvm/rubies/ruby-1.9.3-p385/lib64/ruby/1.9.1/json/common.rb:148:in `parse'
        from (irb):5
        from /home/tyler/.rvm/rubies/ruby-1.9.3-p385/bin/irb:16:in `<main>'
1.9.3p385 :006 > 
brianmario commented 11 years ago

I want to actually drop the JSON gem compat API in yajl-ruby 2.0 (which I haven't had time to work on, heh). It's been a huge source of pain and frustration.

Unless I specifically check for an empty string being passed in and raise the error there, yajl will happily parse this because for all it knows it's just the beginning of a stream of content. I actually prefer how liberal yajl is in this regard.

Being as though I want to remove this functionality, I'm tempted to close this, but... What do you think? :)

rtyler commented 11 years ago

Well, the whole utility of the json_gem compatibility for us is that we can have Yajl speed up JSON on MRI, and rely on the JRuby JSON gem for speed on JRuby.

If you're going to remove it for 2.0 that's your call, but I think the 1.x branch where it exists should match API compat.