Skip to content
This repository
Browse code

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
Brian Lopez authored January 26, 2010 jeremy committed February 05, 2010
5  activesupport/CHANGELOG
... ...
@@ -1,5 +1,10 @@
1 1
 *Rails 3.0 (pending)*
2 2
 
  3
+* JSON backend for YAJL.  #2666 [Brian Lopez]
  4
+
  5
+
  6
+*Rails 3.0.0 [beta] (February 4, 2010)*
  7
+
3 8
 * 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]
4 9
 
5 10
 * 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]
40  activesupport/lib/active_support/json/backends/yajl.rb
... ...
@@ -0,0 +1,40 @@
  1
+require 'yajl-ruby' unless defined?(Yajl)
  2
+
  3
+module ActiveSupport
  4
+  module JSON
  5
+    module Backends
  6
+      module Yajl
  7
+        ParseError = ::Yajl::ParseError
  8
+        extend self
  9
+
  10
+        # Parses a JSON string or IO and convert it into an object
  11
+        def decode(json)
  12
+          data = ::Yajl::Parser.new.parse(json)
  13
+          if ActiveSupport.parse_json_times
  14
+            convert_dates_from(data)
  15
+          else
  16
+            data
  17
+          end
  18
+        end
  19
+
  20
+      private
  21
+        def convert_dates_from(data)
  22
+          case data
  23
+          when nil
  24
+            nil
  25
+          when DATE_REGEX
  26
+            DateTime.parse(data)
  27
+          when Array
  28
+            data.map! { |d| convert_dates_from(d) }
  29
+          when Hash
  30
+            data.each do |key, value|
  31
+              data[key] = convert_dates_from(value)
  32
+            end
  33
+          else
  34
+            data
  35
+          end
  36
+        end
  37
+      end
  38
+    end
  39
+  end
  40
+end
1  activesupport/test/json/decoding_test.rb
@@ -49,6 +49,7 @@ class TestJSONDecoding < ActiveSupport::TestCase
49 49
 
50 50
   backends = %w(Yaml)
51 51
   backends << "JSONGem" if defined?(::JSON)
  52
+  backends << "Yajl" if defined?(::Yajl)
52 53
 
53 54
   backends.each do |backend|
54 55
     TESTS.each do |json, expected|

2 notes on commit a96bf4a

Josh Delsman

V. cool ;)

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