~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activesupport/lib/active_support/json/backends/jsongem.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'json' unless defined?(JSON)
2
 
 
3
 
module ActiveSupport
4
 
  module JSON
5
 
    module Backends
6
 
      module JSONGem
7
 
        ParseError = ::JSON::ParserError
8
 
        extend self
9
 
 
10
 
        # Converts a JSON string into a Ruby object.
11
 
        def decode(json)
12
 
          data = ::JSON.parse(json)
13
 
          if ActiveSupport.parse_json_times
14
 
            convert_dates_from(data)
15
 
          else
16
 
            data
17
 
          end
18
 
        end
19
 
 
20
 
      private
21
 
        def convert_dates_from(data)
22
 
          case data
23
 
            when DATE_REGEX
24
 
              DateTime.parse(data)
25
 
            when Array
26
 
              data.map! { |d| convert_dates_from(d) }
27
 
            when Hash
28
 
              data.each do |key, value|
29
 
                data[key] = convert_dates_from(value)
30
 
              end
31
 
            else data
32
 
          end
33
 
        end
34
 
      end
35
 
    end
36
 
  end
37
 
end