Skip to content

Commit 4f471bf

Browse files
committed
Fixed unexpected illegal/malformed utf-8 error
c34d01f does not consider US-ASCII compatible but non-UTF-8 encodings, and causes an error in RDoc tests.
1 parent 8d8e1aa commit 4f471bf

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎ext/json/ext/generator/generator.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,11 +846,20 @@ static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
846846
fbuffer_append_char(buffer, ']');
847847
}
848848

849+
#ifdef HAVE_RUBY_ENCODING_H
850+
static int enc_utf8_compatible_p(rb_encoding *enc)
851+
{
852+
if (enc == rb_usascii_encoding()) return 1;
853+
if (enc == rb_utf8_encoding()) return 1;
854+
return 0;
855+
}
856+
#endif
857+
849858
static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
850859
{
851860
fbuffer_append_char(buffer, '"');
852861
#ifdef HAVE_RUBY_ENCODING_H
853-
if (!rb_enc_str_asciicompat_p(obj)) {
862+
if (!enc_utf8_compatible_p(rb_enc_get(obj))) {
854863
obj = rb_str_encode(obj, CEncoding_UTF_8, 0, Qnil);
855864
}
856865
#endif

‎tests/json_generator_test.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,10 @@ def to_s; self; end
374374
assert_equal '["foo"]', JSON.generate([s.new('foo')])
375375
end
376376
end
377+
378+
if defined?(Encoding)
379+
def test_nonutf8_encoding
380+
assert_equal("\"5\u{b0}\"", "5\xb0".force_encoding("iso-8859-1").to_json)
381+
end
382+
end
377383
end

0 commit comments

Comments
 (0)