Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

JSON encoding breaks when JSON gem is loaded before Active Support

#2196

When you require JSON before Active Support certain methods defined by the JSON gem are overridden by Active Support. When this happens Active Support drops options hashes in the to_json methods instead of a Generator object like the JSON gem expects.


require 'rubygems'
require 'json'
require 'active_support'

p({'a' => 34.0}.to_json)

/Library/Ruby/Gems/1.8/gems/activesupport-2.3.1/lib/active_support/json/encoding.rb:11:in `to_json': wrong argument type Hash (expected Data) (TypeError)

Reported by Manfred Stienstra · March 10th, 2009 @ 04:32 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. findchris
    findchris

    Seems like a fix/hack is to create an initializer containing just this: require 'json/add/rails'

    See here for background: http://support.fiveruns.com/disc...

    April 29th, 2009 @ 05:46 AM

  2. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  3. Dylan Markow
    Dylan Markow

    Having a similar problem. Using Beta 3, with no other gems installed (including json_pure). Calling a simple .to_json on an array of ActiveRecord objects works fine, but as soon as I add any parameters (for example, .to_json(:only => ...)) I get the same "wrong argument type Hash (expected Data)" error. Exact same code worked perfectly in Rails 2.3.5

    May 10th, 2010 @ 06:52 PM

  4. Neeraj Singh
    Neeraj Singh

    I am unable to find where in rails source code base a dependency on json_pure is defined. I know gemcutter has a dependency on json_pure.

    May 10th, 2010 @ 07:27 PM

  5. Neeraj Singh
    Neeraj Singh

    Ok. Without json_pure rails edge is working fine for me.

    Only when I incllude json_pure in Gemfile my test blows up.

    May 10th, 2010 @ 08:58 PM

  6. Sebastian
    Sebastian

    I have the same problem with Rails 3 beta 4 & the json_pure gem.

    The problem arises because cucumber depends on json_pure. Any suggestions?

    June 15th, 2010 @ 03:02 PM

  7. Aslak Hellesøy
    Aslak Hellesøy

    I could remove the json_pure dependency from Cucumber (it is only needed when you use the wire-protocol, which most Rails people don't). However, this seems like the wrong solution.

    active_support and json_pure need to figure out a way to coexist.

    June 15th, 2010 @ 08:18 PM

  8. David Trasbo
    David Trasbo
    • Importance changed from to

    Works fine on Rails 3.0.0 RC.

    ruby-1.8.7-p299 > require 'rubygems'
     => true 
    ruby-1.8.7-p299 > require 'json'
     => true 
    ruby-1.8.7-p299 > require 'active_support'
     => true 
    ruby-1.8.7-p299 > p({'a' => 34.0}.to_json)
    "{\"a\":34.0}"
     => nil
    

    August 1st, 2010 @ 02:08 PM

  9. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:57 AM

  10. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:25 AM

  11. Aslak Hellesøy
    Aslak Hellesøy

    FWIW, Cucumber/Gherkin gems no longer depend on json_pure, but json instead.

    October 19th, 2010 @ 08:44 AM

  12. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    April 5th, 2011 @ 01:00 AM

  13. rails
    rails
    • State changed from open to stale

    April 5th, 2011 @ 01:00 AM