Permalink
Browse files

get regexp working

  • Loading branch information...
1 parent 1f3495c commit 5ab079322dee52cec11c62f45be89025ce3ab034 @benwyrosdick benwyrosdick committed Feb 22, 2011
Showing with 15 additions and 3 deletions.
  1. +10 −1 lib/crack/json.rb
  2. +5 −2 test/json_test.rb
View
@@ -24,8 +24,9 @@ def self.unescape(str)
# Ensure that ":" and "," are always followed by a space
def self.convert_json_to_yaml(json) #:nodoc:
+ json = String.new(json) #can't modify a frozen string
scanner, quoting, marks, pos, times = StringScanner.new(json), false, [], nil, []
- while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
+ while scanner.scan_until(/(\\['"]|['":,\/\\]|\\.)/)
case char = scanner[1]
when '"', "'"
if !quoting
@@ -41,10 +42,18 @@ def self.convert_json_to_yaml(json) #:nodoc:
end
quoting = false
end
+ when "/"
+ if !quoting
+ json[scanner.pos - 1] = "!ruby/regexp /"
+ scanner.pos += 13
+ scanner.scan_until(/\/[mix]*/)
+ end
when ":",","
marks << scanner.pos - 1 unless quoting
when "\\"
scanner.skip(/\\/)
+ else
+ puts char
end
end
View
@@ -19,6 +19,9 @@ class JsonTest < Test::Unit::TestCase
# no time zone
%({a: "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"},
%({"bio": "1985-01-29: birthdate"}) => {'bio' => '1985-01-29: birthdate'},
+ %({"regex": /foo.*/}) => {'regex' => /foo.*/},
+ %({"regex": /foo.*/i}) => {'regex' => /foo.*/i},
+ %({"regex": /foo.*/mix}) => {'regex' => /foo.*/mix},
%([]) => [],
%({}) => {},
%(1) => 1,
@@ -32,9 +35,9 @@ class JsonTest < Test::Unit::TestCase
TESTS.each do |json, expected|
should "should decode json (#{json})" do
- lambda {
+ #lambda {
Crack::JSON.parse(json).should == expected
- }.should_not raise_error
+ #}.should_not raise_error
end
end

0 comments on commit 5ab0793

Please sign in to comment.