1 require 'yajl' unless defined?(Yajl)
2
3 module ActiveSupport
4 module JSON
5 module Backends
6 module Yajl
7 ParseError = ::Yajl::ParseError
8 extend self
9
10 # Parses a JSON string or IO and convert it into an object
11 def decode(json)
12 data = ::Yajl::Parser.new.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 nil
24 nil
25 when DATE_REGEX
26 DateTime.parse(data)
27 when Array
28 data.map! { |d| convert_dates_from(d) }
29 when Hash
30 data.each do |key, value|
31 data[key] = convert_dates_from(value)
32 end
33 else
34 data
35 end
36 end
37 end
38 end
39 end
40 end