Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes an issue when decoding a json string which looks like a date bu…

…t is invalid. This DateTime parse error is now caught and the original string is instead passed back [#6286 state:resolved]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information...
commit e605165d16bc0eff8e4bea9865d9963b2d24dcf1 1 parent 4dc5505
@joshk joshk authored Santiago Pastorino and Emilio Tagua committed
View
6 activesupport/lib/active_support/json/backends/jsongem.rb
@@ -26,7 +26,11 @@ def convert_dates_from(data)
when nil
nil
when DATE_REGEX
- DateTime.parse(data)
+ begin
+ DateTime.parse(data)
+ rescue ArgumentError
+ data
+ end
when Array
data.map! { |d| convert_dates_from(d) }
when Hash
View
6 activesupport/lib/active_support/json/backends/yajl.rb
@@ -23,7 +23,11 @@ def convert_dates_from(data)
when nil
nil
when DATE_REGEX
- DateTime.parse(data)
+ begin
+ DateTime.parse(data)
+ rescue ArgumentError
+ data
+ end
when Array
data.map! { |d| convert_dates_from(d) }
when Hash
View
12 activesupport/lib/active_support/json/backends/yaml.rb
@@ -29,7 +29,7 @@ def convert_json_to_yaml(json) #:nodoc:
quoting = char
pos = scanner.pos
elsif quoting == char
- if json[pos..scanner.pos-2] =~ DATE_REGEX
+ if valid_date?(json[pos..scanner.pos-2])
# found a date, track the exact positions of the quotes so we can
# overwrite them with spaces later.
times << pos << scanner.pos
@@ -83,6 +83,16 @@ def convert_json_to_yaml(json) #:nodoc:
output
end
end
+
+ private
+ def valid_date?(date_string)
+ begin
+ date_string =~ DATE_REGEX && DateTime.parse(date_string)
+ rescue ArgumentError
+ false
+ end
+ end
+
end
end
end
View
2  activesupport/test/json/decoding_test.rb
@@ -19,6 +19,8 @@ class TestJSONDecoding < ActiveSupport::TestCase
%({"a": "2007-01-01 01:12:34 Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)},
# no time zone
%({"a": "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"},
+ # invalid date
+ %({"a": "1089-10-40"}) => {'a' => "1089-10-40"},
# needs to be *exact*
%({"a": " 2007-01-01 01:12:34 Z "}) => {'a' => " 2007-01-01 01:12:34 Z "},
%({"a": "2007-01-01 : it's your birthday"}) => {'a' => "2007-01-01 : it's your birthday"},
Please sign in to comment.
Something went wrong with that request. Please try again.