diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-01-05 12:34:04 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2016-01-05 12:34:04 -0800 |
commit | 15c6ab5602ea580010e439de272f95c9aae6b577 (patch) | |
tree | e146d0f00dd85fda10eb2eb6e932c9330c7a14b1 /lib | |
parent | e5294ab56f26ccb35338032f656ac2e7362b61f1 (diff) | |
download | ffi-yajl-15c6ab5602ea580010e439de272f95c9aae6b577.tar.gz |
fix validate_utf8: false encoding coercion
the string we get back from ffi is tagged as ascii encoded by default,
so we must force encode it first.
then we don't want to convert from binary to utf-8 since that will wind
up mangling all the utf-8 characters, we want to convert from utf-8
to utf-8 while replacing invalid characters.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ffi_yajl/encoder.rb | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index f4dd8c8..4c3c570 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -41,7 +41,7 @@ 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.encode!("utf-8", "binary", undef: :replace) unless yajl_gen_opts[:yajl_gen_validate_utf8] + str.force_encoding("UTF-8").encode!("UTF-8", undef: :replace, invalid: :replace) unless yajl_gen_opts[:yajl_gen_validate_utf8] str end @@ -56,7 +56,7 @@ 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.encode("utf-8", "binary", undef: :replace) + token = token.to_s.force_encoding("UTF-8").encode("utf-8", undef: :replace, invalid: :replace) 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" |