aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/json/encoders/core.rb
blob: f6cdfbcde5e6f3fa314fbc9ddfea577c985dbd59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module ActiveSupport
  module JSON #:nodoc:
    module Encoders #:nodoc:
      define_encoder Object do |object|
        object.instance_values.to_json
      end
      
      define_encoder TrueClass do
        'true'
      end
      
      define_encoder FalseClass do
        'false'
      end
      
      define_encoder NilClass do
        'null'
      end

      ESCAPED_CHARS = {
        "\010" =>  '\b',
        "\f" =>    '\f',
        "\n" =>    '\n',
        "\r" =>    '\r',
        "\t" =>    '\t',
        '"' =>     '\"',
        '\\' =>    '\\\\'
      }
      
      define_encoder String do |string|
        '"' + string.gsub(/[\010\f\n\r\t"\\]/) { |s|
          ESCAPED_CHARS[s]
        }.gsub(/([\xC0-\xDF][\x80-\xBF]|
                 [\xE0-\xEF][\x80-\xBF]{2}|
                 [\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s|
          s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&')
        } + '"'
      end
      
      define_encoder Numeric do |numeric|
        numeric.to_s
      end
      
      define_encoder Symbol do |symbol|
        symbol.to_s.to_json
      end

      define_encoder Enumerable do |enumerable|
        "[#{enumerable.map { |value| value.to_json } * ', '}]"
      end
      
      define_encoder Hash do |hash|
        returning result = '{' do
          result << hash.map do |key, value|
            key = ActiveSupport::JSON::Variable.new(key.to_s) if 
              ActiveSupport::JSON.can_unquote_identifier?(key)
            "#{key.to_json}: #{value.to_json}"
          end * ', '
          result << '}'
        end
      end

      define_encoder Regexp do |regexp|
        regexp.inspect
      end
    end
  end
end