From 598832b8190ac402db40e7991b9e74610e1e91e5 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Tue, 26 Jan 2010 08:40:35 -0800 Subject: [PATCH] add yajl-ruby as a JSON parsing backend --- .../lib/active_support/json/backends/yajl.rb | 40 ++++++++++++++++++++ activesupport/test/json/decoding_test.rb | 1 + 2 files changed, 41 insertions(+), 0 deletions(-) create mode 100644 activesupport/lib/active_support/json/backends/yajl.rb 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 0000000..d76f8b0 --- /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/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index 8fcb16a..fbd75a8 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/activesupport/test/json/decoding_test.rb @@ -49,6 +49,7 @@ class TestJSONDecoding < ActiveSupport::TestCase backends = %w(Yaml) backends << "JSONGem" if defined?(::JSON) + backends << "Yajl" if defined?(::Yajl) backends.each do |backend| TESTS.each do |json, expected| -- 1.6.6.1