File: active_support/json/backends/jsongem.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: ActiveSupport#3
  module: JSON#4
  module: Backends#5
  module: JSONGem#6
extends
  JSONGem ( ActiveSupport::JSON::Backends )
has properties
constant: ParseError #7
method: decode / 1 #11
method: convert_dates_from / 1 #21

Code

   1  require 'json' unless defined?(JSON)
   2 
   3  module ActiveSupport
   4    module JSON
   5      module Backends
   6        module JSONGem
   7          ParseError = ::JSON::ParserError
   8          extend self
   9 
  10          # Converts a JSON string into a Ruby object.
  11          def decode(json)
  12            data = ::JSON.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 DATE_REGEX
  24                DateTime.parse(data)
  25              when Array
  26                data.map! { |d| convert_dates_from(d) }
  27              when Hash
  28                data.each do |key, value|
  29                  data[key] = convert_dates_from(value)
  30                end
  31              else data
  32            end
  33          end
  34        end
  35      end
  36    end
  37  end