From b17d8d727fb510ad8b6eb4302984d290dc2e53b0 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Fri, 11 Feb 2011 16:47:25 +0100 Subject: Fixes an issue when decoding a json string which looks like a date but 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 --- activesupport/lib/active_support/json/backends/jsongem.rb | 6 +++++- activesupport/lib/active_support/json/backends/yajl.rb | 6 +++++- activesupport/lib/active_support/json/backends/yaml.rb | 12 +++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/json/backends/jsongem.rb b/activesupport/lib/active_support/json/backends/jsongem.rb index cfe28d7bb9..533ba25da3 100644 --- a/activesupport/lib/active_support/json/backends/jsongem.rb +++ b/activesupport/lib/active_support/json/backends/jsongem.rb @@ -26,7 +26,11 @@ module ActiveSupport 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 diff --git a/activesupport/lib/active_support/json/backends/yajl.rb b/activesupport/lib/active_support/json/backends/yajl.rb index 64e50e0d87..58818658c7 100644 --- a/activesupport/lib/active_support/json/backends/yajl.rb +++ b/activesupport/lib/active_support/json/backends/yajl.rb @@ -23,7 +23,11 @@ module ActiveSupport 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 diff --git a/activesupport/lib/active_support/json/backends/yaml.rb b/activesupport/lib/active_support/json/backends/yaml.rb index b1dd2a8107..077eda548a 100644 --- a/activesupport/lib/active_support/json/backends/yaml.rb +++ b/activesupport/lib/active_support/json/backends/yaml.rb @@ -36,7 +36,7 @@ module ActiveSupport 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 @@ -94,6 +94,16 @@ module ActiveSupport 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 -- cgit v1.2.3