Permalink
Browse files

Merge branch 'regex' of https://github.com/commonthread/crack into co…

…mmonthread-regex
  • Loading branch information...
2 parents cb22c02 + ebaa8e6 commit 41840699eb6495d31c437b087dcd181375b4ae96 @jnunemaker committed Sep 17, 2011
Showing with 13 additions and 1 deletion.
  1. +10 −1 lib/crack/json.rb
  2. +3 −0 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,

0 comments on commit 4184069

Please sign in to comment.