This project is archived and is in readonly mode.

#6077 ✓committed
Mark A. Richman

ActiveSupport::JSON.encode fails for Struct types

Reported by Mark A. Richman | November 28th, 2010 @ 05:46 AM | in 3.x

I am using DataMapper's repository.adapter.select to retrieve results of an arbitrary query. The results are returned as an array of structs, each of which looks like the following:

#<struct matches_id=52276, begin_date=Sun, 28 Nov 2010 20:30:00 -0500, windows_time_zone="E. South America Standard Time", team1_id=1430, team2_id=373, team1="Avaí Futebol Clube", team2="Santos Futebol Clube", status="N", match_day_name="Rodada 37", season_name="Brasileirão Série A", round_name="Brasileirão Série A", competition_name="Brasileirão Série A", group_name="", use_gsm="Y", team1_score=0, team2_score=0>

Calling #to_json on this struct throws the following exception:

NoMethodError: undefined method `encode_json' for Sun, 28 Nov 2010 20:30:00 -0500:DateTime
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:214:in `block in encode_json'
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:214:in `map'
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:214:in `encode_json'
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:47:in `block in encode'
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:77:in `check_for_circular_references'
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:45:in `encode'
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.3/lib/active_support/json/encoding.rb:30:in `encode'
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.3/lib/active_support/core_ext/object/to_json.rb:20:in `to_json'
    from (irb):8
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
    from /Users/mark/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

The exception does not seem to be related to the DateTime type, since I can convert this struct to a hash and that will serialize to JSON without error:

Hash[row.members.zip(row.values)] => 

{:matches_id=>52276, :begin_date=>Sun, 28 Nov 2010 20:30:00 -0500, :windows_time_zone=>"E. South America Standard Time", :team1_id=>1430, :team2_id=>373, :team1=>"Avaí Futebol Clube", :team2=>"Santos Futebol Clube", :status=>"N", :match_day_name=>"Rodada 37", :season_name=>"Brasileirão Série A", :round_name=>"Brasileirão Série A", :competition_name=>"Brasileirão Série A", :group_name=>"", :use_gsm=>"Y", :team1_score=>0, :team2_score=>0}

which produces correct JSON on #to_json:

=> "{\"matches_id\":52276,\"begin_date\":\"2010-11-28T20:30:00-05:00\",\"windows_time_zone\":\"E. South America Standard Time\",\"team1_id\":1430,\"team2_id\":373,\"team1\":\"Ava\\u00ed Futebol Clube\",\"team2\":\"Santos Futebol Clube\",\"status\":\"N\",\"match_day_name\":\"Rodada 37\",\"season_name\":\"Brasileir\\u00e3o S\\u00e9rie A\",\"round_name\":\"Brasileir\\u00e3o S\\u00e9rie A\",\"competition_name\":\"Brasileir\\u00e3o S\\u00e9rie A\",\"group_name\":\"\",\"use_gsm\":\"Y\",\"team1_score\":0,\"team2_score\":0}"

Struct support should be added OR perhaps a pointer to some documentation on how I can expect AS::JSON in my own project to work with Struct. I think it's ok to do a local hack like the one I did temporarily, but I'd rather see this handled in Rails, or at the very least in some project specific override.

Thanks,
Mark

Comments and changes to this ticket

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>

Referenced by

Pages