This project is archived and is in readonly mode.
yajl-ruby as a JSON parsing backend
-
Rick
- Milestone cleared.
Doh, I wrote a Rails 3 plugin before I saw this. I'd like to get some people using this before we push this to rails core.
-
Francisco Viramontes
hey!
I keep getting a strange error rendering json in edge rails.
The problem is whith this
respond_to do |format| format.json { render :json => @devices.to_json(:only => [:id, :name, :unique_identifier], :include => { :device_variables => { :only => [:id, :upper_threshold, :lower_threshold], :methods => [:current_value, :name, :unit] } } ) } format.xml { render :xml => { :error => "not found" }, :status => 404 } format.html { render :file => "public/404.html", :status => 404 } endreturns a strange error: wrong argument type Hash (expected Data)
But not if I do a simpler
@devices.to_json()I have the yajl rails plugin installed, any ideas?
Can I help?
Here is the full stack trace:
TypeError in DevicesController#index
wrong argument type Hash (expected Data)
RAILS_ROOT: /Users/paki/Work/freshout/ugmo-v2
Application Trace | Framework Trace | Full Trace/Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:222:in
to_json' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:222:insend' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:222:inmethod_missing' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:392:inmethod_missing_without_paginate' /Users/paki/.gem/ruby/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:inmethod_missing' /Users/paki/Work/freshout/ugmo-v2/app/controllers/devices_controller.rb:13:inindex' /Users/paki/Work/freshout/ugmo-v2/app/controllers/devices_controller.rb:11:inindex'/Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:222:in
to_json' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:222:insend' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:222:inmethod_missing' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:392:inmethod_missing_without_paginate' /Users/paki/.gem/ruby/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:inmethod_missing' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/mime_responds.rb:191:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/mime_responds.rb:191:inrespond_to' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/base.rb:43:insend_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/base.rb:43:insend_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/base.rb:113:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/logger.rb:32:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/callbacks.rb:20:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activesupport/lib/active_support/new_callbacks.rb:422:in_run__815259351__process_action__199225275__callbacks' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activesupport/lib/active_support/new_callbacks.rb:375:insend' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activesupport/lib/active_support/new_callbacks.rb:375:in_run_process_action_callbacks' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/callbacks.rb:19:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/rendering_controller.rb:9:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/compatibility.rb:83:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/flash.rb:140:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/rescuable.rb:46:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/base.rb:91:inprocess' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/filter_parameter_logging.rb:69:insass_old_process' /Users/paki/.gem/ruby/1.8/gems/haml-2.2.2/lib/sass/plugin/rails.rb:19:inprocess' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal.rb:72:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/rack_convenience.rb:15:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal.rb:93:inaction' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/routing/route_set.rb:449:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/routing/route_set.rb:449:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/query_cache.rb:29:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:incache' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/query_cache.rb:9:incache' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/query_cache.rb:28:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:363:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/head.rb:9:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/methodoverride.rb:24:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/params_parser.rb:17:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb:93:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/rescue.rb:8:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:35:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:47:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/lock.rb:11:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/lock.rb:11:insynchronize' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/lock.rb:11:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/railties/lib/rails/rack/static.rb:31:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/urlmap.rb:46:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/urlmap.rb:40:ineach' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/urlmap.rb:40:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/railties/lib/rails/rack/log_tailer.rb:17:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/content_length.rb:13:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/chunked.rb:15:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/handler/mongrel.rb:61:inprocess' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:inprocess_client' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:ineach' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:inprocess_client' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:inrun' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:ininitialize' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:innew' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:inrun' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:ininitialize' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:innew' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:inrun' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/handler/mongrel.rb:34:inrun' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/railties/lib/commands/server.rb:103 /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:31:ingem_original_require' /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:31:inrequire' script/server:3/Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:222:in
to_json' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:222:insend' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:222:inmethod_missing' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:392:inmethod_missing_without_paginate' /Users/paki/.gem/ruby/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:inmethod_missing' /Users/paki/Work/freshout/ugmo-v2/app/controllers/devices_controller.rb:13:inindex' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/mime_responds.rb:191:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/mime_responds.rb:191:inrespond_to' /Users/paki/Work/freshout/ugmo-v2/app/controllers/devices_controller.rb:11:inindex' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/base.rb:43:insend_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/base.rb:43:insend_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/base.rb:113:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/logger.rb:32:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/callbacks.rb:20:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activesupport/lib/active_support/new_callbacks.rb:422:in_run__815259351__process_action__199225275__callbacks' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activesupport/lib/active_support/new_callbacks.rb:375:insend' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activesupport/lib/active_support/new_callbacks.rb:375:in_run_process_action_callbacks' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/callbacks.rb:19:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/rendering_controller.rb:9:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/compatibility.rb:83:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/flash.rb:140:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/rescuable.rb:46:inprocess_action' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/abstract_controller/base.rb:91:inprocess' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/filter_parameter_logging.rb:69:insass_old_process' /Users/paki/.gem/ruby/1.8/gems/haml-2.2.2/lib/sass/plugin/rails.rb:19:inprocess' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal.rb:72:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal/rack_convenience.rb:15:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/metal.rb:93:inaction' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/routing/route_set.rb:449:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_controller/routing/route_set.rb:449:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/query_cache.rb:29:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:incache' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/query_cache.rb:9:incache' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/query_cache.rb:28:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:363:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/head.rb:9:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/methodoverride.rb:24:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/params_parser.rb:17:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb:93:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/rescue.rb:8:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:35:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:47:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/lock.rb:11:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/lock.rb:11:insynchronize' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/lock.rb:11:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/railties/lib/rails/rack/static.rb:31:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/urlmap.rb:46:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/urlmap.rb:40:ineach' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/urlmap.rb:40:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/railties/lib/rails/rack/log_tailer.rb:17:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/content_length.rb:13:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/chunked.rb:15:incall' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/handler/mongrel.rb:61:inprocess' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:inprocess_client' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:ineach' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:inprocess_client' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:inrun' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:ininitialize' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:innew' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:inrun' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:ininitialize' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:innew' /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:inrun' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/handler/mongrel.rb:34:inrun' /Users/paki/Work/freshout/ugmo-v2/vendor/rails/railties/lib/commands/server.rb:103 /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:31:ingem_original_require' /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:31:inrequire' script/server:3Request
Parameters:
{"format"=>"json", "property_id"=>"3"}
Show session dump
Response
Headers:
None
-
Rick
1) make sure your edge rails is up to date.
2) UseActiveSupport::JSON.encode(@devices, :only....).Every JSON library implements #to_json differently, so you can't rely
on it anymore. For more info, check the changelogs, or wait for the
release announcement when the Rails 3 changes are blogged. -
Brian Lopez
So the strange thing is, yajl-ruby only overrides to_json if you specifically require 'yajl/json_gem' somewhere. Your yajl-rails plugin doesn't do that, so I'd also assume to_json would work the way Francisco has it above.
That being said,
ActiveSupport::JSON.encode(@devices, :only....)should work fine. -
Rick
Sure, that may be true. Maybe another json lib is being loaded? This
part of rails has changed a lot even between 2.3 and 3.0. There'll be
some clear guidance on how to proceed when Rails 3 is released. -
Brian Lopez
What do you guys think about getting this into core?
My fork of Rick's plugin should be up to date with both changes to the yajl-ruby API and the Rails JSON backend API.
http://github.com/brianmario/yajl-railsI think it's safe to say enough people are using it (it was also mentioned in the 2.3.3 release notes as being one of the JSON backends) to justify finally getting it into core. Thoughts?
-
Brian Lopez
Here's the patch, hopefully now there's no excuse ;)
-
Brian Lopez
Btw, I'm pretty sure it'll apply cleanly to the 2.3.x and 3.x branches
-
Lakshan Perera
+1 for the above patch.
-
Brian Lopez
Sorry, the author line was wrong in that patch - use this one instead
-
Repository
- State changed from open to committed
(from [a96bf4ab5e73fccdafb78b99e8a122cc2172b505]) Add yajl-ruby as a JSON parsing backend
[#2666 yajl-ruby as a JSON parsing backend state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/a96bf4ab5e73fccdafb78b99e8a122... -
Repository
(from [83b4c161fc1a7405c0abeeebda37b9cbfe899a7b]) Add yajl-ruby as a JSON parsing backend
[#2666 yajl-ruby as a JSON parsing backend state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/83b4c161fc1a7405c0abeeebda37b9...
