This project is archived and is in readonly mode.

#4730 ✓committed
August Lilleaas

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

Reported by August Lilleaas | May 30th, 2010 @ 01:59 AM | in 3.0.2

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.

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh May 30th, 2010 @ 04:33 AM

    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.

  • Santiago Pastorino

    Santiago Pastorino May 30th, 2010 @ 05:39 AM

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

    August Lilleaas May 30th, 2010 @ 12:22 PM

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

  • Neeraj Singh

    Neeraj Singh May 30th, 2010 @ 02:50 PM

    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.

  • Repository

    Repository June 26th, 2010 @ 08:15 PM

    • State changed from “open” to “committed”
    • Importance changed from “” to “Medium”

    (from [926ca9c102db5034d4eb1ab0f915ea643145e284]) Load JSON additions (as to_json) on active_support/all.

    [#4730 state:committed]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/926ca9c102db5034d4eb1ab0f915ea...

  • seamusabshere

    seamusabshere July 16th, 2010 @ 02:31 PM

    • 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

  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
  • csnk

    csnk May 18th, 2011 @ 08:16 AM

    → Milestone changed from “” to “3.0.2”clothing factory

  • csnk

    csnk May 18th, 2011 @ 08:30 AM

    We are the professional clothing manufacturer and clothing supplier, so we manufacture kinds of custom clothing manufacturer. welcome you to come to our china clothing manufacturer and clothing factory.

  • klkk

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>

Attachments

Referenced by

Pages