Permalink
Browse files

[issue 22] Removing any \u0000 from the input before handing to YAML

  • Loading branch information...
1 parent 1f3495c commit 06ee364d8c7c544415dec76405c02816c8749891 @gus gus committed Mar 1, 2011
Showing with 16 additions and 6 deletions.
  1. +1 −0 Rakefile
  2. +1 −1 lib/crack/json.rb
  3. +14 −5 test/json_test.rb
View
@@ -1,3 +1,4 @@
+$:.unshift("lib")
require 'rubygems'
require 'rake'
require File.expand_path('../lib/crack', __FILE__)
View
@@ -16,7 +16,7 @@ def self.parse(json)
protected
def self.unescape(str)
- str.gsub(/\\u([0-9a-f]{4})/) { [$1.hex].pack("U") }
+ str.gsub(/\\u0000/, "").gsub(/\\u([0-9a-f]{4})/) { [$1.hex].pack("U") }
end
# matches YAML-formatted dates
View
@@ -27,24 +27,33 @@ class JsonTest < Test::Unit::TestCase
%(null) => nil,
%(true) => true,
%(false) => false,
- %q("http:\/\/test.host\/posts\/1") => "http://test.host/posts/1"
+ %q("http:\/\/test.host\/posts\/1") => "http://test.host/posts/1",
+
+ # \u0000 and \x00 escape sequences
+ %q({"foo":"bar\u0000"}) => {"foo" => "bar"},
+ %q({"foo":"bar\u0000baz"}) => {"foo" => "barbaz"},
+ %q(bar\u0000) => "bar",
+ %q(bar\u0000baz) => "barbaz",
+
+ %q({"foo":"bar\x00"}) => {"foo" => "bar\x00"},
+ %q({"foo":"bar\x00baz"}) => {"foo" => "bar\x00baz"}
}
TESTS.each do |json, expected|
- should "should decode json (#{json})" do
+ should "decode json (#{json})" do
lambda {
Crack::JSON.parse(json).should == expected
}.should_not raise_error
end
end
- should "should raise error for failed decoding" do
+ should "raise error for failed decoding" do
lambda {
Crack::JSON.parse(%({: 1}))
}.should raise_error(Crack::ParseError)
end
- should "should be able to parse a JSON response from a Twitter search about 'firefox'" do
+ should "be able to parse a JSON response from a Twitter search about 'firefox'" do
data = ''
File.open(File.dirname(__FILE__) + "/data/twittersearch-firefox.json", "r") { |f|
data = f.read
@@ -55,7 +64,7 @@ class JsonTest < Test::Unit::TestCase
}.should_not raise_error(Crack::ParseError)
end
- should "should be able to parse a JSON response from a Twitter search about 'internet explorer'" do
+ should "be able to parse a JSON response from a Twitter search about 'internet explorer'" do
data = ''
File.open(File.dirname(__FILE__) + "/data/twittersearch-ie.json", "r") { |f|
data = f.read

0 comments on commit 06ee364

Please sign in to comment.