Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8026

Show
Ignore:
Timestamp:
10/26/07 03:22:02 (11 months ago)
Author:
rick
Message:

Fix JSON encoding/decoding bugs dealing with /'s. Closes #9990 [Rick, theamazingrando]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r8022 r8026  
    11*SVN* 
     2 
     3* Fix JSON encoding/decoding bugs dealing with /'s.  Closes #9990 [Rick, theamazingrando] 
    24 
    35* Introduce a base class for all test cases used by rails applications. ActiveSupport::TestCase [Koz] 
  • trunk/activesupport/lib/active_support/json/decoding.rb

    r7865 r8026  
    4444 
    4545          if marks.empty? 
    46             json 
     46            json.gsub(/\\\//, '/') 
    4747          else 
    4848            # FIXME: multiple slow enumerations 
     
    5252                      join(" ") 
    5353            times.each { |i| output[i-1] = ' ' } 
     54            output.gsub!(/\\\//, '/') 
    5455            output 
    5556          end 
  • trunk/activesupport/lib/active_support/json/encoders/string.rb

    r7736 r8026  
    44      ESCAPED_CHARS = { 
    55        "\010" =>  '\b', 
    6         "\f" =>    '\f', 
    7         "\n" =>    '\n', 
    8         "\r" =>    '\r', 
    9         "\t" =>    '\t', 
    10         '"' =>     '\"', 
    11         '\\' =>    '\\\\', 
    12         ">" =>     '\076', 
    13         '<' =>     '\074' 
     6        "\f"   =>  '\f', 
     7        "\n"   =>  '\n', 
     8        "\r"   =>  '\r', 
     9        "\t"   =>  '\t', 
     10        '"'    =>  '\"', 
     11        '\\'   =>  '\\\\', 
     12        ">"    =>  '\076', 
     13        '<'    =>  '\074', 
     14        '/'    =>  '\\/' 
    1415      } 
    1516    end 
     
    1920class String 
    2021  def to_json(options = nil) #:nodoc: 
    21     '"' + gsub(/[\010\f\n\r\t"\\><]/) { |s| 
     22    '"' + gsub(/[\010\f\n\r\t"\\><\/]/) { |s| 
    2223      ActiveSupport::JSON::Encoding::ESCAPED_CHARS[s] 
    2324    }.gsub(/([\xC0-\xDF][\x80-\xBF]| 
  • trunk/activesupport/test/json/decoding_test.rb

    r7613 r8026  
    33class TestJSONDecoding < Test::Unit::TestCase 
    44  TESTS = { 
    5     %({"returnTo":{"/categories":"/"}})        => {"returnTo" => {"/categories" => "/"}}, 
    6     %({returnTo:{"/categories":"/"}})          => {"returnTo" => {"/categories" => "/"}}, 
    7     %({"return\\"To\\":":{"/categories":"/"}}) => {"return\"To\":" => {"/categories" => "/"}}, 
    8     %({"returnTo":{"/categories":1}})          => {"returnTo" => {"/categories" => 1}}, 
     5    %q({"returnTo":{"\/categories":"\/"}})        => {"returnTo" => {"/categories" => "/"}}, 
     6    %q({returnTo:{"\/categories":"\/"}})          => {"returnTo" => {"/categories" => "/"}}, 
     7    %q({"return\\"To\\":":{"\/categories":"\/"}}) => {"return\"To\":" => {"/categories" => "/"}}, 
     8    %q({"returnTo":{"\/categories":1}})          => {"returnTo" => {"/categories" => 1}}, 
    99    %({"returnTo":[1,"a"]})                    => {"returnTo" => [1, "a"]}, 
    1010    %({"returnTo":[1,"\\"a\\",", "b"]})        => {"returnTo" => [1, "\"a\",", "b"]}, 
     
    2424    %(null)  => nil, 
    2525    %(true)  => true, 
    26     %(false) => false 
     26    %(false) => false, 
     27    %q("http:\/\/test.host\/posts\/1") => "http://test.host/posts/1" 
    2728  } 
    2829   
    29   def test_json_decoding 
    30     TESTS.each do |json, expected| 
     30  TESTS.each do |json, expected| 
     31    define_method :"test_json_decoding_#{json}" do 
    3132      assert_nothing_raised do 
    3233        assert_equal expected, ActiveSupport::JSON.decode(json) 
  • trunk/activesupport/test/json/encoding_test.rb

    r8010 r8026  
    1515 
    1616  StringTests   = [[ 'this is the <string>',     %("this is the \\074string\\076")], 
    17                    [ 'a "string" with quotes', %("a \\"string\\" with quotes") ]] 
     17                   [ 'a "string" with quotes', %("a \\"string\\" with quotes") ], 
     18                   [ 'http://test.host/posts/1', %("http:\\/\\/test.host\\/posts\\/1")]] 
    1819 
    1920  ArrayTests    = [[ ['a', 'b', 'c'],          %([\"a\", \"b\", \"c\"])          ],