Skip to content

Commit

Permalink
Add yajl-ruby as a JSON parsing backend
Browse files Browse the repository at this point in the history
[#2666 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
brianmario authored and jeremy committed Feb 5, 2010
1 parent 55c1a86 commit 83b4c16
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*2.3.6 (pending)*

* JSON backend for YAJL. #2666 [Brian Lopez]

* Introduce String#html_safe for rails_xss plugin and forward-compatibility with Rails 3. [Michael Koziarski, Santiago Pastorino, José Ignacio Costa]

* Time#- with a DateTime argument behaves the same as with a Time argument, i.e. returns the difference between self and arg as a Float #3476 [Geoff Buesing]
Expand Down
40 changes: 40 additions & 0 deletions 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
1 change: 1 addition & 0 deletions activesupport/test/json/decoding_test.rb
Expand Up @@ -40,6 +40,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|
Expand Down

0 comments on commit 83b4c16

Please sign in to comment.