diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-01-05 13:40:08 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2016-01-05 13:40:08 -0800 |
commit | f5809701af8d4f3b52452ea1c943dc97b9a65da6 (patch) | |
tree | 50db64d832b4bedc2d2ecf6c03f76ca162eaa1cb | |
parent | 614856afa5ccaf3eca783fa9997f39c38ef7852b (diff) | |
download | ffi-yajl-f5809701af8d4f3b52452ea1c943dc97b9a65da6.tar.gz |
use new ruby #scrub method where available
-rw-r--r-- | lib/ffi_yajl/encoder.rb | 17 | ||||
-rw-r--r-- | spec/ffi_yajl/encoder_spec.rb | 2 |
2 files changed, 16 insertions, 3 deletions
diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index 4c3c570..14e3801 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -41,7 +41,15 @@ module FFI_Yajl # call either the ext or ffi hook str = do_yajl_encode(obj, yajl_gen_opts, opts) # we can skip cleaning the whole string for utf-8 issues if we have yajl validate as we go - str.force_encoding("UTF-8").encode!("UTF-8", undef: :replace, invalid: :replace) unless yajl_gen_opts[:yajl_gen_validate_utf8] + + str.force_encoding("UTF-8") + unless yajl_gen_opts[:yajl_gen_validate_utf8] + if str.respond_to?(:scrub) + str.scrub! + else + str.encode!("UTF-8", 'binary', undef: :replace, invalid: :replace) + end + end str end @@ -56,7 +64,12 @@ module FFI_Yajl def self.raise_error_for_status(status, token = nil) # scrub token to valid utf-8 since we may be issuing an exception on an invalid utf-8 token - token = token.to_s.force_encoding("UTF-8").encode("utf-8", undef: :replace, invalid: :replace) + token = token.to_s.force_encoding("UTF-8") + if token.respond_to?(:scrub) + token.scrub! + else + token.encode("utf-8", 'binary', undef: :replace, invalid: :replace) + end case status when 1 # yajl_gen_keys_must_be_strings raise FFI_Yajl::EncodeError, "YAJL internal error: attempted use of non-string object as key" diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index 13d3511..724277b 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -187,7 +187,7 @@ describe "FFI_Yajl::Encoder" do } it "raises an error on invalid json" do - expect { encoder.encode(ruby) }.to raise_error(FFI_Yajl::EncodeError, /Invalid UTF-8 string 'Elan Ruusam.e': cannot encode to UTF-8/) + expect { encoder.encode(ruby) }.to raise_error(FFI_Yajl::EncodeError, /Invalid UTF-8 string 'Elan Ruusam.*': cannot encode to UTF-8/) end context "when validate_utf8 is off" do |