diff options
author | Mikel Lindsaar <raasdnil@gmail.com> | 2010-03-11 22:07:48 +1100 |
---|---|---|
committer | Mikel Lindsaar <raasdnil@gmail.com> | 2010-03-11 22:07:48 +1100 |
commit | 965fe59bff249ad91131a444e1fbd63dc4411db3 (patch) | |
tree | ec2d54d5eaad9e32bce1758e6a67c364279bbd39 /activesupport/lib/active_support/json | |
parent | 79f02a473cb6aef00003745f23802314c8c89e7d (diff) | |
parent | 4adcbb6b2d6cef49ac28df4254ac74e09f14dcf7 (diff) | |
download | rails-965fe59bff249ad91131a444e1fbd63dc4411db3.tar.gz rails-965fe59bff249ad91131a444e1fbd63dc4411db3.tar.bz2 rails-965fe59bff249ad91131a444e1fbd63dc4411db3.zip |
Merge branch 'master' of github.com:lifo/docrails
Diffstat (limited to 'activesupport/lib/active_support/json')
-rw-r--r-- | activesupport/lib/active_support/json/backends/yajl.rb | 40 | ||||
-rw-r--r-- | activesupport/lib/active_support/json/decoding.rb | 17 |
2 files changed, 56 insertions, 1 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 diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index a5908365af..e357b6837a 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -6,12 +6,15 @@ module ActiveSupport mattr_accessor :parse_json_times module JSON + # Listed in order of preference. + DECODERS = %w(Yajl JSONGem Yaml) + class << self attr_reader :parse_error delegate :decode, :to => :backend def backend - self.backend = "Yaml" unless defined?(@backend) + set_default_backend unless defined?(@backend) @backend end @@ -31,6 +34,18 @@ module ActiveSupport ensure self.backend = old_backend end + + def set_default_backend + DECODERS.find do |name| + begin + self.backend = name + true + rescue LoadError + # Try next decoder. + false + end + end + end end end end |