aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/json/backends/yajl.rb
blob: d76f8b03e4dd43174b1ae5db4b405da147004427 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'yajl-ruby' unless defined?(Yajl)

module ActiveSupport
  module JSON
    module Backends
      module Yajl
        ParseError = ::Yajl::ParseError
        extend self

        # Parses a JSON string or IO and convert it into an object
        def decode(json)
          data = ::Yajl::Parser.new.parse(json)
          if ActiveSupport.parse_json_times
            convert_dates_from(data)
          else
            data
          end
        end

      private
        def convert_dates_from(data)
          case data
          when nil
            nil
          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