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.

Hashes with Time instances in it breaks when to_json is called on it.

#4730

Hashes with time objects in them will cause an error when to_json is called on it.

{:date => Time.utc(2008, 5, 17)}.to_json

The stack trace is rather peculiar as well. Here's a snippet from a irb session, with backtrace silencers removed.

ruby-1.8.7-p249 >  {:foo => Time.now}.to_json
ArgumentError: wrong number of arguments (2 for 0)
      from (irb):1:in `to_json'
      from (irb):1

Attached a patch with a simple test case that will demonstrate the breakage.

Reported by August Lilleaas · May 30th, 2010 @ 01:59 AM

State: committed
Milestone: 3.0.2
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Medium

Activity

  1. Neeraj Singh
    Neeraj Singh

    I tested it with rails edge.

    I am not able to produce this problem with script/console.

    ruby-1.8.7-p249 > {:date => Time.utc(2008, 5, 17)}.to_json
     => "{\"date\":\"2008-05-17T00:00:00Z\"}" 
    ruby-1.8.7-p249 > {:date => Time.utc(2008, 5, 17)}.to_json
     => "{\"date\":\"2008-05-17T00:00:00Z\"}"
    

    I am not able to produce problem with rails runner

    $ rails runner 'User.lab'
    {"date1":"2008-05-17T00:00:00Z"}
    
    class User < ActiveRecord::Base
      def self.lab
        t = Time.utc(2008, 5, 17)
        real = {:date1 => t}.to_json
        puts real
      end
    end
    

    However the attached test code is indeed failing which is really weird. Without digging dip into it I have a feeling that in test somewhere something is getting changed which is messing up to_json.

    May 30th, 2010 @ 04:33 AM

  2. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • Tag changed from activesupport, json, rails3 to activesupport, json, patch, rails3
    • State changed from new to open
    • Assigned user set to Yehuda Katz (wycats)

    Neeraj try this and you will see the failure ...

    >> santiago@debian:/tmp/myapp$ rails c
    Loading development environment (Rails 3.0.0.beta3)
    >> {:date => Time.utc(2008, 5, 17)}.to_json
    NoMethodError: undefined method `to_json' for {:date=>Sat May 17 00:00:00 UTC 2008}:Hash
        from (irb):1
    
    Here is a fix i think the code regarding to json on AS is not properly organized, i will refactor this at some point.

    May 30th, 2010 @ 05:39 AM

  3. August Lilleaas
    August Lilleaas

    @Neeraj Singh: Perhaps you have the json gem vendored elsewhere, or directly in the Gemfile, causing Rails to use that one instead of the bundled one? I'm also on edge rails, btw: gem 'rails', :git => 'git://github.com/rails/rails.git'.

    May 30th, 2010 @ 12:22 PM

  4. Neeraj Singh
    Neeraj Singh

    This is what I have in my Gemfile.

    $ cat Gemfile 
    
    source 'http://rubygems.org'
    #gem 'rails', '3.0.0.beta3'
    gem 'rails', :git => 'git://github.com/rails/rails.git'
    gem 'sqlite3-ruby', :require => 'sqlite3'
    

    in console

    ruby-1.8.7-p249 > {:date => Time.utc(2008, 5, 17)}.to_json
     => "{\"date\":\"2008-05-17T00:00:00Z\"}"
    

    Really weird that I'm not able to reproduce it.

    May 30th, 2010 @ 02:50 PM

  5. Repository
    Repository
    • State changed from open to committed
    • Importance changed from to Medium

    June 26th, 2010 @ 08:15 PM

  6. seamusabshere
    seamusabshere
    • Assigned user changed from Yehuda Katz (wycats) to Santiago Pastorino

    che Santiago,

    I think the original problem remains:

    $ gem list json
    
    *** LOCAL GEMS ***
    
    json (1.4.3)
    json_pure (1.4.3)
    $ gem list activesupport
    
    *** LOCAL GEMS ***
    
    activesupport (3.0.0.beta4, 2.3.8, 2.3.5)
    $ irb
    ruby-1.8.7-head > require 'rubygems'
     => true 
    ruby-1.8.7-head > require 'active_support/json'
     => true 
    ruby-1.8.7-head > { :hello => Time.now }.to_json
    ArgumentError: wrong number of arguments (2 for 1)
        from (irb):3:in `to_json'
        from (irb):3
    

    Best,
    Seamus

    July 16th, 2010 @ 02:31 PM

  7. Jeremy Kemper