Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8399 (closed defect: fixed)

Opened 1 year ago

Last modified 1 year ago

[PATCH] Add to_json support to Date and Time

Reported by: wycats Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveSupport Version: edge
Severity: normal Keywords: json
Cc:

Description

to_json does not properly serialize dates and times. As a result, doing something like @ar_object.attributes.to_json results in the creation of "{}" where Dates and Times exist inside the attributes hash.

This patch simply adds simple support for to_json in Date and Time, which also causes hashes and other enumerables containing Dates and Times to encode to JSON correctly.

Attachments

patch_for_date_time_json.diff (1.4 kB) - added by wycats on 05/18/07 17:54:29.

Change History

05/18/07 17:54:29 changed by wycats

  • attachment patch_for_date_time_json.diff added.

05/18/07 19:02:19 changed by bitsweat

  • keywords set to json.

Would

def to_json() to_s(:db) end

work? I'm not sure what's nice to have + easy to parse in Javascript.

05/18/07 19:34:32 changed by wycats

Date#to_s(:db) returns something like: "2007-05-18", which parses to NaN in JS via Date.parse. Time#to_s(:db) also returns something not easily parsable by JS. new Date also returns an "Invalid Date" object

The format I provided works perfectly for Date.parse as well as new Date().

05/18/07 21:20:39 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [6773]) Date, Time, and DateTime#to_json. Closes #8399.