summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/json/generator/generator.c11
-rw-r--r--test/json/json_generator_test.rb6
2 files changed, 16 insertions, 1 deletions
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
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
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