This project is archived and is in readonly mode.
Ruby 1.9.2-head's JSON support breaks to_json for arrays of records
-
MikZ
can confirm this error in ruby 1.9.1 and 1.9.2 with rails3 beta3
commenting this line http://github.com/rails/rails/commit/00ee990443189649e481b2c30945e7... fixes it
-
Jeremy Kemper
The fix is to override the json lib's new
Array#to_jsonalso.We have no choice but to load the library, if present, to override its
to_jsonpollution. -
Rohit Arondekar
An issue has been created by somebody on json's github: http://github.com/flori/json/issues#issue/21 let's see what happens there because it doesn't only break Rails. Also this commit has been referenced as the one to introduce the bug: http://github.com/flori/json/commit/82ed5b724fb36dcba884cdb2251d953...
-
Rasmus Rønn Nielsen
I just installed ruby1.9.2-head and beta4 today. I think I'm getting the same error.
{ "test" => "test" } works, but { "test" => :test } fails...
rasmus@machine1:/var/www/app1/current$ ruby -v ruby 1.9.2dev (2010-06-12 revision 28290) [x86_64-linux] rasmus@machine1:/var/www/app1/current$ rails c Loading development environment (Rails 3.0.0.beta4) ruby-1.9.2-head > { "test" => "test" }.to_json => "{\"test\":\"test\"}" ruby-1.9.2-head > { "test" => :test }.to_json ArgumentError: wrong number of arguments (2 for 1) from /home/rasmus/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.0.beta4/lib/active_support/json/encoding.rb:133:in `to_json' from (irb):2:in `to_json' from (irb):2 from /home/rasmus/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.beta4/lib/rails/commands/console.rb:47:in `start' from /home/rasmus/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.beta4/lib/rails/commands/console.rb:8:in `start' from /home/rasmus/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.beta4/lib/rails/commands.rb:23:in `<top (required)>' from script/rails:9:in `require' from script/rails:9:in `<main>' -
Alex MacCaw
Broken for me too. The issue is that the json lib is defining to_json methods on String/Hash/etc, which aren't ever being undefined. The following fixes it:
module JSONFix def self.included(base) base.class_eval do undef to_json def to_json(options = nil) ActiveSupport::JSON.encode(self, options) end end end end [ Hash, Array, String, Numeric, TrueClass, FalseClass, BigDecimal ].each {|c| c.send(:include, JSONFix) } -
DavidBackeus
- Importance changed from to Low
Broken in ruby 1.8.7 as well.
$ irb -r rubygems >> require 'active_support/json' => true >> {:asdf => :asdf}.to_json ArgumentError: wrong number of arguments (2 for 1) from (irb):2:in `to_json' from (irb):2 -
Tim
What steps do I need to take to solve this problem? Please be verbose. I've attempted the suggestions here with no luck.
I'm using Ruby 1.8.7 and Rails 2.3.8.
Thanks!
Sincerely,
Tim -
Nate Wiger
- Tag changed from json to activerecord 3.0, rails 3.0.0.beta4, activesupport, active_support, json
The only way I've found to workaround this is to put this in environment.rb, after the initializer block
class Array def to_json(*a) ActiveSupport::JSON.encode(self) end endThis also works for Sinatra/etc if you put it in the main application.rb file (or whatever yours is called).
I still don't understand why Rails needs to forcibly override to_json, when the JSON gem provides callbacks that can be used. The comments in the ActiveSupport source code even outline how they do nasty things to ensure the JSON gem's is clobbered.
-Nate
-
Jeremy Kemper
- Milestone set to 2.3.9
- State changed from new to open
- Assigned user set to José Valim
Nate, we begrudgingly chose to override jsongem's
to_jsonafter dealing with years of bugs due to its broken API.It assumes responsibility for all
to_jsonusage and clobbers the method signature, using it as an internal builder API as well as a public JSON serialization API, then requiring users to pass along an*blob_of_important_but_opaque_argswhen defining a customto_json.This means that any library that doesn't conform to the
*blob_of_important_but_opaque_argsmethod signature breaks when you require 'json'. That's fundamentally antisocial API behavior, and it caused enough issues that it was worth neutering.The jsongem library should keep its internal builder API tucked away someplace private, use a separate method like
as_jsonto provide custom JSON representations of classes (that doesn't need the crazy method signature), and leaveto_jsonas a simple, easily-overrideable wrapper method.So yes, the clobbering mentioned in the source comments is a fully intentional conspiracy to eradicate the
to_jsonthat jsongem wrought upon the Ruby world.José, you fixed this on master, right? Could you backport to 2-3-stable?
-
José Valim
Yes, this is fixed in master. I tried to fix this on 2-3-stable though and could not reproduce the error in ActiveSupport test suite. If anyone could provide a failing test in AS, that would help a lot.
-
José Valim
- State changed from open to stale