summaryrefslogtreecommitdiff
path: root/ext/json/generator
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-10-14 19:13:20 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-10-14 19:54:48 +0900
commit2003755a2c491fd81496308cad97ce6304c51fe8 (patch)
tree348e080cb58bbe60c69e8cd8119276bac4331567 /ext/json/generator
parentd7fa7e2c860430a3dfa1b08be96734925b6c81d8 (diff)
[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
Diffstat (limited to 'ext/json/generator')
-rw-r--r--ext/json/generator/generator.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c
index d98879c60d9..f297fb53f78 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