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.

Rails 2.3.4, json gem : to_json issue

#3193

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.

Reported by nicolas_o · September 12th, 2009 @ 10:54 AM

State: stale
Milestone: 2.3.6
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: Low

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2-, 2-3-stable, to_json to 2-, 2-3-stable, bugmash, to_json

    September 25th, 2009 @ 12:18 PM

  2. Josh Sharpe
    Josh Sharpe

    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

    September 26th, 2009 @ 07:11 PM

  3. Josh Sharpe
    Josh Sharpe

    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

    September 26th, 2009 @ 07:13 PM

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

    September 26th, 2009 @ 09:50 PM

  5. sr.iniv.t
    sr.iniv.t

    -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}}"
    

    September 26th, 2009 @ 10:11 PM

  6. John Pignata
  7. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to invalid
    • Tag changed from 2-, 2-3-stable, bugmash, to_json to 2-, 2-3-stable, to_json

    September 27th, 2009 @ 12:02 PM

  8. Matías Flores
    Matías Flores
    • Tag changed from 2-, 2-3-stable, to_json to 2-, 2-3-stable, to_json

    -1 not reproducible on 2-3-stable

    September 27th, 2009 @ 05:01 PM

  9. vlad.zar (at gmail)
    vlad.zar (at gmail)

    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

    November 2nd, 2009 @ 01:13 PM

  10. vlad.zar (at gmail)
    vlad.zar (at gmail)

    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

    November 2nd, 2009 @ 01:16 PM

  11. Gregg Dickson
    Gregg Dickson
    • 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.

    November 17th, 2009 @ 03:44 PM

  12. Colin Steele
  13. Wesley Moxam
    Wesley Moxam
    • 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?

    February 4th, 2010 @ 06:00 AM

  14. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)
    • 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?

    February 4th, 2010 @ 06:14 AM

  15. Jeremy Kemper
    Jeremy Kemper
    • 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.

    February 28th, 2010 @ 02:16 AM

  16. Gregg Dickson
    Gregg Dickson

    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 ;-)

    February 28th, 2010 @ 11:17 PM

  17. Jeremy Kemper
    Jeremy Kemper

    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.

    March 4th, 2010 @ 04:14 PM

  18. Sebastian Vogelsang
    Sebastian Vogelsang

    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

    March 18th, 2010 @ 04:26 PM

  19. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to stale

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

    March 18th, 2010 @ 05:58 PM

  20. nicolas_o
    nicolas_o

    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

    March 24th, 2010 @ 03:57 PM

  21. testman
    testman

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

    April 5th, 2010 @ 01:57 AM

  22. acechase
    acechase

    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.

    May 26th, 2010 @ 11:22 PM

  23. acechase
    acechase

    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\"}}"
    

    May 26th, 2010 @ 11:55 PM

  24. nicolas_o
    nicolas_o
    • Importance changed from to Low

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

    July 7th, 2010 @ 11:56 AM