Skip to content

Commit a96bf4a

Browse files
brianmariojeremy
authored andcommitted
Add yajl-ruby as a JSON parsing backend
[#2666 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
1 parent c548e21 commit a96bf4a

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

activesupport/CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
*Rails 3.0 (pending)*
22

3+
* JSON backend for YAJL. #2666 [Brian Lopez]
4+
5+
6+
*Rails 3.0.0 [beta] (February 4, 2010)*
7+
38
* 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]
49

510
* 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]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

activesupport/test/json/decoding_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TestJSONDecoding < ActiveSupport::TestCase
4949

5050
backends = %w(Yaml)
5151
backends << "JSONGem" if defined?(::JSON)
52+
backends << "Yajl" if defined?(::Yajl)
5253

5354
backends.each do |backend|
5455
TESTS.each do |json, expected|

0 commit comments

Comments
 (0)