Skip to content

Commit

Permalink
get regexp working
Browse files Browse the repository at this point in the history
  • Loading branch information
benwyrosdick committed Feb 22, 2011
1 parent 1f3495c commit 5ab0793
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/crack/json.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down
7 changes: 5 additions & 2 deletions test/json_test.rb
Expand Up @@ -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,
Expand All @@ -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

Expand Down

0 comments on commit 5ab0793

Please sign in to comment.