Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httparty fails to parse certain json data #75

Closed
jtoy opened this issue Mar 5, 2011 · 10 comments
Closed

httparty fails to parse certain json data #75

jtoy opened this issue Mar 5, 2011 · 10 comments

Comments

@jtoy
Copy link

jtoy commented Mar 5, 2011

url="http://gdata.youtube.com/feeds/api/users/dutchlove1976?alt=json"
JSON.parse(open(url){|r| p r.read }) #no problem
r = HTTParty.send(:get,url ) =>
Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/crack-0.1.8/lib/crack/json.rb:19:in gsub' from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/crack-0.1.8/lib/crack/json.rb:19:inunescape'
from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/crack-0.1.8/lib/crack/json.rb:12:in parse' from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty/parser.rb:116:injson'
from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty/parser.rb:136:in parse_supported_format' from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty/parser.rb:103:inparse'
from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty/parser.rb:66:in call' from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty/request.rb:217:inparse_response'
from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty/request.rb:189:in handle_response' from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty/request.rb:71:inperform'
from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty.rb:394:in perform_request' from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty.rb:346:inget'
from /Users/jtoy/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty.rb:426:in `get'

@Dagnan
Copy link

Dagnan commented Apr 13, 2011

Hi. Does anyone have a way to handle this? I have the same problem.

@Dagnan
Copy link

Dagnan commented Apr 13, 2011

For a reason I do not know, httparty uses Crack::JSON, but JSON with JSON.parse works fine. I going to monkey patch this on my version. I anyone has a clue please comment :)

def json Crack::JSON.parse(body) end

@erikostling
Copy link

I'm also having a problem with Crack (huhu) and Ruby 1.9.2…

 Failure/Error: Unable to find matching line from backtrace
 SystemStackError:
   stack level too deep
 # /Users/erikostling/.rvm/gems/ruby-1.9.2-p180/gems/crack-0.1.8/lib/crack/json.rb:54

Seems not be the same as yours however.

@Dagnan
Copy link

Dagnan commented Apr 17, 2011

Indeed.

@tfwright
Copy link

I'm also having this problem. Confim using JSON lib solves.

@metamn
Copy link

metamn commented May 29, 2011

The same here on larger data sets:

/home/cs/.rvm/gems/ruby-1.9.2-head/gems/crack-0.1.8/lib/crack/json.rb:54: stack level too deep (SystemStackError)

@metamn
Copy link

metamn commented May 29, 2011

Note: the same JSON data it is received without problems by rest-client or curl

@ericgj
Copy link

ericgj commented May 29, 2011

I was having some JSON issues with Crack too (different ones). I do wish Crack wasn't the default, it seems really out of date now. But note you don't actually have to monkeypatch to get around it, HTTParty lets you specify the parser like

# using native json parser instead of Crack
class JsonParser < HTTParty::Parser
  def json
    ::JSON.parse(body)
  end
end

class MyClient
  include HTTParty
  parser JsonParser
  # ...
end

@froots
Copy link

froots commented Jul 19, 2011

See this pull request as well: #89

Until that's incorporated, monkey patching with the following works well for me:

module HTTParty
  class Parser
    def json
      MultiJson.decode(body)
    end
  end
end

And in your Gemfile:

gem 'multi_json'

@jnunemaker
Copy link
Owner

This should be fixed as multi json is the default. Just use it with a good json gem and you are fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants