~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activesupport/lib/active_support/json/encoding.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# encoding: utf-8
2
 
require 'active_support/core_ext/module/delegation'
3
 
require 'active_support/deprecation'
4
 
 
5
 
module ActiveSupport
6
 
  class << self
7
 
    delegate :use_standard_json_time_format, :use_standard_json_time_format=,
8
 
      :escape_html_entities_in_json, :escape_html_entities_in_json=,
9
 
      :to => :'ActiveSupport::JSON::Encoding'
10
 
  end
11
 
 
12
 
  module JSON
13
 
    # matches YAML-formatted dates
14
 
    DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/
15
 
 
16
 
    class << self
17
 
      delegate :encode, :to => :'ActiveSupport::JSON::Encoding'
18
 
    end
19
 
 
20
 
    module Encoding #:nodoc:
21
 
      class CircularReferenceError < StandardError
22
 
      end
23
 
 
24
 
      ESCAPED_CHARS = {
25
 
        "\010" =>  '\b',
26
 
        "\f"   =>  '\f',
27
 
        "\n"   =>  '\n',
28
 
        "\r"   =>  '\r',
29
 
        "\t"   =>  '\t',
30
 
        '"'    =>  '\"',
31
 
        '\\'   =>  '\\\\',
32
 
        '>'    =>  '\u003E',
33
 
        '<'    =>  '\u003C',
34
 
        '&'    =>  '\u0026' }
35
 
 
36
 
      class << self
37
 
        # If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
38
 
        attr_accessor :use_standard_json_time_format
39
 
 
40
 
        attr_accessor :escape_regex
41
 
        attr_reader :escape_html_entities_in_json
42
 
 
43
 
        def escape_html_entities_in_json=(value)
44
 
          self.escape_regex = \
45
 
            if @escape_html_entities_in_json = value
46
 
              /[\010\f\n\r\t"\\><&]/
47
 
            else
48
 
              /[\010\f\n\r\t"\\]/
49
 
            end
50
 
        end
51
 
 
52
 
        def escape(string)
53
 
          string = string.dup.force_encoding(::Encoding::BINARY) if string.respond_to?(:force_encoding)
54
 
          json = string.
55
 
            gsub(escape_regex) { |s| ESCAPED_CHARS[s] }.
56
 
            gsub(/([\xC0-\xDF][\x80-\xBF]|
57
 
                   [\xE0-\xEF][\x80-\xBF]{2}|
58
 
                   [\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s|
59
 
            s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/n, '\\\\u\&')
60
 
          }
61
 
          %("#{json}")
62
 
        end
63
 
 
64
 
        # Converts a Ruby object into a JSON string.
65
 
        def encode(value, options = nil)
66
 
          options = {} unless Hash === options
67
 
          seen = (options[:seen] ||= [])
68
 
          raise CircularReferenceError, 'object references itself' if seen.include?(value)
69
 
          seen << value
70
 
          value.to_json(options)
71
 
        ensure
72
 
          seen.pop
73
 
        end
74
 
      end
75
 
 
76
 
      self.escape_html_entities_in_json = true
77
 
    end
78
 
 
79
 
    CircularReferenceError = Deprecation::DeprecatedConstantProxy.new('ActiveSupport::JSON::CircularReferenceError', Encoding::CircularReferenceError)
80
 
  end
81
 
end
82
 
 
83
 
# Hack to load json gem first so we can overwrite its to_json.
84
 
begin
85
 
  require 'json'
86
 
rescue LoadError
87
 
end
88
 
 
89
 
require 'active_support/json/variable'
90
 
require 'active_support/json/encoders/date'
91
 
require 'active_support/json/encoders/date_time'
92
 
require 'active_support/json/encoders/enumerable'
93
 
require 'active_support/json/encoders/false_class'
94
 
require 'active_support/json/encoders/hash'
95
 
require 'active_support/json/encoders/nil_class'
96
 
require 'active_support/json/encoders/numeric'
97
 
require 'active_support/json/encoders/object'
98
 
require 'active_support/json/encoders/regexp'
99
 
require 'active_support/json/encoders/string'
100
 
require 'active_support/json/encoders/symbol'
101
 
require 'active_support/json/encoders/time'
102
 
require 'active_support/json/encoders/true_class'