diff options
Diffstat (limited to 'activesupport/lib/active_support/json/backends/jsongem.rb')
-rw-r--r-- | activesupport/lib/active_support/json/backends/jsongem.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/json/backends/jsongem.rb b/activesupport/lib/active_support/json/backends/jsongem.rb new file mode 100644 index 0000000000..649e6301d1 --- /dev/null +++ b/activesupport/lib/active_support/json/backends/jsongem.rb @@ -0,0 +1,41 @@ +require 'json' unless defined?(JSON) + +module ActiveSupport + module JSON + ParseError = ::JSON::ParserError unless const_defined?(:ParseError) + + module Backends + module JSONGem + extend self + + # Parses a JSON string or IO and convert it into an object + def decode(json) + if json.respond_to?(:read) + json = json.read + end + data = ::JSON.parse(json) + if ActiveSupport.parse_json_times + convert_dates_from(data) + else + data + end + end + + private + def convert_dates_from(data) + case data + when DATE_REGEX + DateTime.parse(data) + when Array + data.map! { |d| convert_dates_from(d) } + when Hash + data.each do |key, value| + data[key] = convert_dates_from(value) + end + else data + end + end + end + end + end +end
\ No newline at end of file |