aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/json
diff options
context:
space:
mode:
authorBrian Lopez <seniorlopez@gmail.com>2010-01-26 08:40:35 -0800
committerJeremy Kemper <jeremy@bitsweat.net>2010-02-05 10:15:51 -0800
commita96bf4ab5e73fccdafb78b99e8a122cc2172b505 (patch)
tree3c2f89d4903b4d69f7248e1a7d36030c6ce6c04c /activesupport/lib/active_support/json
parentc548e213658386f3a5b00097bc5b30bf3736e6b4 (diff)
downloadrails-a96bf4ab5e73fccdafb78b99e8a122cc2172b505.tar.gz
rails-a96bf4ab5e73fccdafb78b99e8a122cc2172b505.tar.bz2
rails-a96bf4ab5e73fccdafb78b99e8a122cc2172b505.zip
Add yajl-ruby as a JSON parsing backend
[#2666 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activesupport/lib/active_support/json')
-rw-r--r--activesupport/lib/active_support/json/backends/yajl.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/json/backends/yajl.rb b/activesupport/lib/active_support/json/backends/yajl.rb
new file mode 100644
index 0000000000..d76f8b03e4
--- /dev/null
+++ b/activesupport/lib/active_support/json/backends/yajl.rb
@@ -0,0 +1,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