Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add yajl-ruby as a JSON parsing backend
[#2666 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
- Loading branch information
1 parent
55c1a86
commit 83b4c16
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters