diff options
-rw-r--r-- | lib/puppet/external/pson/pure/generator.rb | 17 | ||||
-rwxr-xr-x | spec/unit/util/pson_spec.rb (renamed from spec/unit/util/json_spec.rb) | 17 |
2 files changed, 22 insertions, 12 deletions
diff --git a/lib/puppet/external/pson/pure/generator.rb b/lib/puppet/external/pson/pure/generator.rb index ef8b36d31..4180be57d 100644 --- a/lib/puppet/external/pson/pure/generator.rb +++ b/lib/puppet/external/pson/pure/generator.rb @@ -63,22 +63,15 @@ module PSON end else def utf8_to_pson(string) # :nodoc: - string = string.gsub(/["\\\x0-\x1f]/) { MAP[$MATCH] } - string.gsub!(/( - (?: + string. + gsub(/["\\\x0-\x1f]/n) { MAP[$MATCH] }. + gsub(/((?: [\xc2-\xdf][\x80-\xbf] | [\xe0-\xef][\x80-\xbf]{2} | [\xf0-\xf4][\x80-\xbf]{3} - )+ | - [\x80-\xc1\xf5-\xff] # invalid - )/nx) { |c| - c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'" - s = PSON::UTF8toUTF16.iconv(c).unpack('H*')[0] - s.gsub!(/.{4}/n, '\\\\u\&') + )+)/nx) { |c| + PSON::UTF8toUTF16.iconv(c).unpack('H*')[0].gsub(/.{4}/n, '\\\\u\&') } - string - rescue Iconv::Failure => e - raise GeneratorError, "Caught #{e.class}: #{e}" end end module_function :utf8_to_pson diff --git a/spec/unit/util/json_spec.rb b/spec/unit/util/pson_spec.rb index 4f6cea997..d02d28517 100755 --- a/spec/unit/util/json_spec.rb +++ b/spec/unit/util/pson_spec.rb @@ -18,4 +18,21 @@ describe Puppet::Util::Pson do pson.expects(:from_pson).with("mydata") pson.pson_create("type" => "foo", "data" => "mydata") end + + + { + 'foo' => '"foo"', + 1 => '1', + "\x80" => "\"\x80\"", + [] => '[]' + }.each { |str,pson| + it "should be able to encode #{str.inspect}" do + str.to_pson.should == pson + end + } + + it "should be able to handle arbitrary binary data" do + bin_string = (1..20000).collect { |i| ((17*i+13*i*i) % 255).chr }.join + PSON.parse(%Q{{ "type": "foo", "data": #{bin_string.to_pson} }})["data"].should == bin_string + end end |