This project is archived and is in readonly mode.

#2196 ✓stale
Manfred Stienstra

JSON encoding breaks when JSON gem is loaded before Active Support

Reported by Manfred Stienstra | March 10th, 2009 @ 04:32 PM | in 3.x

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)

Comments and changes to this ticket

  • findchris

    findchris April 29th, 2009 @ 05:46 AM

    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...

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Dylan Markow

    Dylan Markow May 10th, 2010 @ 06:52 PM

    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

  • Neeraj Singh

    Neeraj Singh May 10th, 2010 @ 07:27 PM

    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.

  • Neeraj Singh

    Neeraj Singh May 10th, 2010 @ 08:58 PM

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

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

  • Sebastian

    Sebastian June 15th, 2010 @ 03:02 PM

    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?

  • Aslak Hellesøy

    Aslak Hellesøy June 15th, 2010 @ 08:18 PM

    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.

  • David Trasbo

    David Trasbo August 1st, 2010 @ 02:08 PM

    • 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
    
  • Ryan Bigg

    Ryan Bigg October 11th, 2010 @ 10:57 AM

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:25 AM

    Automatic cleanup of spam.

  • Aslak Hellesøy

    Aslak Hellesøy October 19th, 2010 @ 08:44 AM

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

  • rails

    rails April 5th, 2011 @ 01:00 AM

    • 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.

  • rails

    rails April 5th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages