Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add yajl-ruby as a JSON parsing backend

[#2666 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit a96bf4ab5e73fccdafb78b99e8a122cc2172b505 1 parent c548e21
@brianmario brianmario authored jeremy committed
View
5 activesupport/CHANGELOG
@@ -1,5 +1,10 @@
*Rails 3.0 (pending)*
+* JSON backend for YAJL. #2666 [Brian Lopez]
+
+
+*Rails 3.0.0 [beta] (February 4, 2010)*
+
* Introduce class_attribute to declare inheritable class attributes. Writing an attribute on a subclass behaves just like overriding the superclass reader method. Unifies and replaces most usage of cattr_accessor, class_inheritable_attribute, superclass_delegating_attribute, and extlib_inheritable_attribute. [Jeremy Kemper, Yehuda Katz]
* 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]
View
40 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
View
1  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|

2 comments on commit a96bf4a

@brianmario

nice

@voxxit

V. cool ;)

Please sign in to comment.
Something went wrong with that request. Please try again.