This project is archived and is in readonly mode.

#3193 ✓stale
nicolas_o

Rails 2.3.4, json gem : to_json issue

Reported by nicolas_o | September 12th, 2009 @ 10:54 AM | in 2.3.6

The result of to_json command evolved between 2.3.3 and 2.3.4 , breaking my web app :

New behaviour :

Loading development environment (Rails 2.3.4)
>> Event.find(9).to_json
=> "{\"attributes\":{...snip...},\"json_class\":\"Event\",\"attributes_cache\":{}}"
Old behaviour :
Loading development environment (Rails 2.3.3)
>> Event.find(9).to_json
=> "{\"event\":{...snip...}"
I am using the json gem for json processing in rails as introduced in rails 2.3.3 :
In my environment.rb there is this line : ActiveSupport::JSON.backend = 'JSONGem'

But I also tried with default json support and the behaviour is the same.

Comments and changes to this ticket

  • CancelProfileIsBroken

    CancelProfileIsBroken September 25th, 2009 @ 12:18 PM

    • Tag changed from 2-, 2-3-stable, to_json to 2-, 2-3-stable, bugmash, to_json
  • Josh Sharpe

    Josh Sharpe September 26th, 2009 @ 07:11 PM

    looks invalid to me:

    $ sc Loading development environment (Rails 2.3.3)

    Item.new.to_json SQL (0.1ms) SET SQL_AUTO_IS_NULL=0 Item Columns (2.0ms) SHOW FIELDS FROM items => "{item

  • Josh Sharpe

    Josh Sharpe September 26th, 2009 @ 07:13 PM

    dear lighthouse,
    whatever you're doing with my text, you're doing it wrong
    pastie does it pretty well though
    bai

    http://pastie.org/631729

  • rbxbx

    rbxbx September 26th, 2009 @ 09:50 PM

    Loading development environment (Rails 2.3.4)
    >> Item.new.to_json
    => "{\"item\":{\"created_at\":null,\"updated_at\":null}}"
    
    Loading development environment (Rails 2.3.3)
    >> Item.new.to_json
    => "{\"item\":{\"created_at\":null,\"updated_at\":null}}"
    

    -1 ,going to say invalid.

  • sr.iniv.t

    sr.iniv.t September 26th, 2009 @ 10:11 PM

    -1, not reproducible.

    Looks to me like there's no difference between 2.3.3 and 2.3.4:

    ts > ruby script/console 
    Loading development environment (Rails 2.3.3)
    >> Event.new(:name => "test", :when => 2.days.from_now).to_json
    => "{\"event\":{\"name\":\"test\",\"updated_at\":null,\"when\":\"2009-09-28T21:08:03Z\",\"created_at\":null}}"
    
    ts > ruby script/console 
    Loading development environment (Rails 2.3.4)
    >> Event.new(:name => "test", :when => 2.days.from_now).to_json
    => "{\"event\":{\"name\":\"test\",\"updated_at\":null,\"when\":\"2009-09-28T21:08:27Z\",\"created_at\":null}}"
    
  • John Pignata
  • CancelProfileIsBroken

    CancelProfileIsBroken September 27th, 2009 @ 12:02 PM

    • State changed from “new” to “invalid”
    • Tag changed from 2-, 2-3-stable, bugmash, to_json to 2-, 2-3-stable, to_json
  • Matías Flores

    Matías Flores September 27th, 2009 @ 05:01 PM

    • Tag changed from 2-, 2-3-stable, to_json to 2-, 2-3-stable, to_json

    -1 not reproducible on 2-3-stable

  • vlad.zar (at gmail)

    vlad.zar (at gmail) November 2nd, 2009 @ 01:13 PM

    This bug is reproducible:

    No json or json_pure gem installed in the system:

    Loading development environment (Rails 2.3.3)

    Event.find(9).to_json => "{">event

    After "gem install json":

    Loading development environment (Rails 2.3.4)

    Event.find(9).to_json => "{">attributes

  • vlad.zar (at gmail)

    vlad.zar (at gmail) November 2nd, 2009 @ 01:16 PM

    This bug is reproducible:

    No json or json_pure gem installed in the system:

    Loading development environment (Rails 2.3.3) >> Event.find(9).to_json => "{attributes

  • Gregg Dickson

    Gregg Dickson November 17th, 2009 @ 03:44 PM

    • Tag changed from 2-, 2-3-stable, to_json to 2.3.4, to_json

    I am also having an issue with to_json that is very reproducible. This is 1.0 compliant JSON:
    {"location": [{city }

    This is what freshly compiled Rails 2.3.4 produces (no pure_json or json installed) from a call to my locations model @Locations = Location.all.to_json: [{city

    The enclosing brackets cause the json library not to correctly parse the json string.

    Then as_json give this:

    <Location:0x28357f0>#<Location:0x2835674>#<Location:0x2835624>

    Something is wrong here.

  • Colin Steele
  • Wesley Moxam

    Wesley Moxam February 4th, 2010 @ 06:00 AM

    • Tag changed from 2.3.4, to_json to 2.3.4, to_json

    I have the same problem.

    Removing the json gem dependancy from environment.rb fixes the problem. Unfortunately that's not feasible for me since I am using other gems that depend on the json gem. Bummer.

    In addition to the problems stated above, the problem also affects the :include, and :except options.
    This problem is also present in Rails 2.3.5

    Could someone please remove the 'invalid' status?

  • Prem Sichanugrist (sikachu)

    Prem Sichanugrist (sikachu) February 4th, 2010 @ 06:14 AM

    • State changed from “invalid” to “open”
    • Tag changed from 2.3.4, to_json to 2.3.6, to_json

    Can somebody please provide a patch for this?

  • Jeremy Kemper

    Jeremy Kemper February 28th, 2010 @ 02:16 AM

    • Assigned user set to “Jeremy Kemper”
    • Milestone set to 2.3.6

    To fix, you need to explicitly require the json gem in config/environment.rb so Rails can override its choice of to_json implementation.

    In Rails 3, we explicitly load the json gem if present then neuter its to_json. This approach can be backported to 2-3-stable.

  • Gregg Dickson

    Gregg Dickson February 28th, 2010 @ 11:17 PM

    To confirm the approach followed in Rails 3. The approach is to move the json code to ActiveModel::Serializers::JSON, and make sure to NOT use the json gem if is is present? Where is this code? Also, the serializer in ActiveModel doesn't have to_json, right? Where is the code to override the json gem? (I am new to contributing to rails core but want to help out so please be nice ;-)

  • Jeremy Kemper

    Jeremy Kemper March 4th, 2010 @ 04:14 PM

    The JSON code you're looking for is in active_support/json, not Active Model or Record :)

    See active_support/json/encoding.rb in git master.

  • Sebastian Vogelsang

    Sebastian Vogelsang March 18th, 2010 @ 04:26 PM

    For me it was the contacts gem (http://github.com/mislav/contacts), which broke the to_json method. See the ticket on Github: http://github.com/mislav/contacts/issues/issue/3

  • Jeremy Kemper

    Jeremy Kemper March 18th, 2010 @ 05:58 PM

    • State changed from “open” to “stale”

    Lots of reports, but no patches. Not a blocker for 2.3.6 release.

  • nicolas_o

    nicolas_o March 24th, 2010 @ 03:57 PM

    I am the original reporter for this bug. Turns out I'm also using the contacts gem. Maybe that's the problem. I created a ticket :

    http://github.com/cardmagic/contacts/issues/issue/14

  • testman

    testman April 5th, 2010 @ 01:57 AM

    Yes after removing the contacts gem it also went away for me...g damn why?

  • acechase

    acechase May 26th, 2010 @ 11:22 PM

    I've been upgrading our app from Rails 2.2.2 to 2.3.8 and hit this problem (and all sorts of other JSON craziness). I was stumped, changing ActiveSupport::JSON.backend in environment.rb was giving me no love. I eventually tracked down the source to a require of 'json/add/rails', which we were including so that we could hack the way JSON::Ext::Generator::State worked.

    So, if you're hitting this problem the one thing you may want to do is search your codebase for a require of 'json/add/rails'... worked for me.

  • acechase

    acechase May 26th, 2010 @ 11:55 PM

    Heh, I spoke too soon...

    For objects from classes that descend from ActiveRecord::Base removing the 'json/add/rails'require was all that was needed to get back to vanilla to_json. However, oddly, once I remove 'json/add/rails', if I leave in 'json/add/core' then non-AR::Base objects start to jsonize oddly:

    # without 'json/add/rails', with 'json/add/core'
    >> {:foo => :bar}.to_json
    => "{\"foo\":{\"json_class\":\"Symbol\",\"s\":\"bar\"}}"
    
    # (without 'json/add/rails', without 'json/add/core') OR (with 'json/add/rails', with 'json/add/core')
    >> {:foo => :bar}.to_json
    => "{\"foo\":{\"json_class\":\"Symbol\",\"s\":\"bar\"}}"
    
  • nicolas_o

    nicolas_o July 7th, 2010 @ 11:56 AM

    • Importance changed from “” to “Low”

    updating the contacts gem solved my problem (rails 2.3.8, contacts 1.2.3)

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>

Tags

Pages