Sha256: a50fd9e955c29cdcf0f864bcabb19f01b2f0465f89acb208b9e0f03a6e11a094

Contents?: true

Size: 1.35 KB

Versions: 151

Compression:

Stored size: 1.35 KB

Contents

require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/module/delegation'
require 'multi_json'

module ActiveSupport
  # Look for and parse json strings that look like ISO 8601 times.
  mattr_accessor :parse_json_times

  module JSON
    class << self
      def decode(json, options ={})
        data = MultiJson.decode(json, options)
        if ActiveSupport.parse_json_times
          convert_dates_from(data)
        else
          data
        end
      end

      def engine
        MultiJson.engine
      end
      alias :backend :engine

      def engine=(name)
        MultiJson.engine = name
      end
      alias :backend= :engine=

      def with_backend(name)
        old_backend, self.backend = backend, name
        yield
      ensure
        self.backend = old_backend
      end

      def parse_error
        MultiJson::DecodeError
      end

      private

      def convert_dates_from(data)
        case data
        when nil
          nil
        when DATE_REGEX
          begin
            DateTime.parse(data)
          rescue ArgumentError
            data
          end
        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

Version data entries

151 entries across 138 versions & 7 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
classiccms-0.7.4 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
classiccms-0.7.3 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
classiccms-0.7.2 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
classiccms-0.7.1 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
classiccms-0.7.0 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
activesupport-3.1.10 lib/active_support/json/decoding.rb
activesupport-3.1.9 lib/active_support/json/decoding.rb
classiccms-0.6.9 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
dirty_history-0.7.3 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/json/decoding.rb
dirty_history-0.7.2 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/json/decoding.rb
dirty_history-0.7.1 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/json/decoding.rb
dirty_history-0.7.0 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/json/decoding.rb
classiccms-0.6.8 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
classiccms-0.6.7 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
classiccms-0.6.6 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
classiccms-0.6.5 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
classiccms-0.6.4 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/json/decoding.rb
dirty_history-0.6.7 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/json/decoding.rb
dirty_history-0.6.6 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/json/decoding.rb