ruby-changes:58242
From: Nobuyoshi <ko1@a...>
Date: Mon, 14 Oct 2019 20:24:47 +0900 (JST)
Subject: [ruby-changes:58242] 2003755a2c (master): [flori/json] Fixed unexpected illegal/malformed utf-8 error
https://git.ruby-lang.org/ruby.git/commit/?id=2003755a2c From 2003755a2c491fd81496308cad97ce6304c51fe8 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Mon, 14 Oct 2019 19:13:20 +0900 Subject: [flori/json] Fixed unexpected illegal/malformed utf-8 error flori/json@c34d01ff6a18dac04a90b2e0f820cdb1d5c7e1b2 does not consider US-ASCII compatible but non-UTF-8 encodings, and causes an error in RDoc tests. https://github.com/flori/json/commit/4f471bf590 diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c index d98879c..f297fb5 100644 --- a/ext/json/generator/generator.c +++ b/ext/json/generator/generator.c @@ -806,11 +806,20 @@ static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_St https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.c#L806 fbuffer_append_char(buffer, ']'); } +#ifdef HAVE_RUBY_ENCODING_H +static int enc_utf8_compatible_p(rb_encoding *enc) +{ + if (enc == rb_usascii_encoding()) return 1; + if (enc == rb_utf8_encoding()) return 1; + return 0; +} +#endif + static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj) { fbuffer_append_char(buffer, '"'); #ifdef HAVE_RUBY_ENCODING_H - if (!rb_enc_str_asciicompat_p(obj)) { + if (!enc_utf8_compatible_p(rb_enc_get(obj))) { obj = rb_str_encode(obj, CEncoding_UTF_8, 0, Qnil); } #endif diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 86be398..d7f9ebe 100644 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -374,4 +374,10 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/json/json_generator_test.rb#L374 assert_equal '["foo"]', JSON.generate([s.new('foo')]) end end + + if defined?(Encoding) + def test_nonutf8_encoding + assert_equal("\"5\u{b0}\"", "5\xb0".force_encoding("iso-8859-1").to_json) + end + end end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/