diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2015-04-24 11:22:59 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-04-24 11:22:59 -0700 |
commit | 97349120810fbb32b6ebaaff91f40156ef5b739d (patch) | |
tree | 112f3aeabedc8803990392bcd1adfa76b31e5e9f | |
parent | a1e388d59d98616d5d8ff79d7ae8a35030005ce8 (diff) | |
parent | 2c679d13f7c94893e6d1d4a62fe8cf3a8a9f96cf (diff) | |
download | ffi-yajl-97349120810fbb32b6ebaaff91f40156ef5b739d.tar.gz |
Merge pull request #57 from chef/lcg/utf8-string-replacement
change :validate_utf8=false to still emit utf8
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | lib/ffi_yajl/encoder.rb | 5 | ||||
-rw-r--r-- | spec/ffi_yajl/encoder_spec.rb | 4 |
3 files changed, 12 insertions, 5 deletions
@@ -71,7 +71,7 @@ hash = parser.parse( json ) * `:check_utf8` * `:dont_validate_strings` * `:symbolize_keys` (default = false): -* `:symbolize_names` (default = false): alias for `:symbolize_keys` +* `:symbolize_names` (default = false): Alias for `:symbolize_keys`. * `:allow_trailing_garbage` * `:allow_multiple_values` * `:allow_partial_values` @@ -79,8 +79,10 @@ hash = parser.parse( json ) ## Encoder Options -* `:pretty` (default = false): -* `:validate_utf8` +* `:pretty` (default = false): Produces more human readable 'pretty' output. +* `:validate_utf8` (default = true): Will raise an exception when trying to + encode strings that are invalid UTF-8. When set to false this still will + produce valid UTF-8 JSON but will replace invalid characters. ## Forcing FFI or C Extension diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index a89bcd3..8c6dce6 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -40,7 +40,8 @@ module FFI_Yajl # call either the ext or ffi hook str = do_yajl_encode(obj, yajl_gen_opts, opts) - str.force_encoding('UTF-8') if defined? Encoding + # 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 end @@ -55,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.encode!("utf-8", "binary", :undef => :replace) + token = token.to_s.encode("utf-8", "binary", :undef => :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" diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index 5691bf6..7dbe4d0 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -196,6 +196,10 @@ describe "FFI_Yajl::Encoder" do it "does not raise an error" do expect{ encoder.encode(ruby) }.not_to raise_error end + + it "returns valid utf8" do + expect( encoder.encode(ruby).valid_encoding? ).to be true + end end end end |