@@ -89,14 +89,14 @@ static void raise_parse_error(const char *format, const char *start)
8989 rb_enc_raise (rb_utf8_encoding (), rb_path2class ("JSON::ParserError" ), format , ptr );
9090}
9191
92- static VALUE mJSON , mExt , cParser , eNestingError ;
92+ static VALUE mJSON , mExt , cParser , eNestingError , Encoding_UTF_8 ;
9393static VALUE CNaN , CInfinity , CMinusInfinity ;
9494
9595static ID i_json_creatable_p , i_json_create , i_create_id , i_create_additions ,
9696 i_chr , i_max_nesting , i_allow_nan , i_symbolize_names ,
9797 i_object_class , i_array_class , i_decimal_class ,
9898 i_deep_const_get , i_match , i_match_string , i_aset , i_aref ,
99- i_leftshift , i_new , i_try_convert , i_freeze , i_uminus ;
99+ i_leftshift , i_new , i_try_convert , i_freeze , i_uminus , i_encode ;
100100
101101static int binary_encindex ;
102102static int utf8_encindex ;
@@ -1797,16 +1797,11 @@ static VALUE convert_encoding(VALUE source)
17971797 }
17981798
17991799 if (encindex == binary_encindex ) {
1800- // For historical reason, we silently reinterpret binary strings as UTF-8 if it would work.
1801- VALUE utf8_string = rb_enc_associate_index (rb_str_dup (source ), utf8_encindex );
1802- switch (rb_enc_str_coderange (utf8_string )) {
1803- case ENC_CODERANGE_7BIT :
1804- case ENC_CODERANGE_VALID :
1805- return utf8_string ;
1806- }
1800+ // For historical reason, we silently reinterpret binary strings as UTF-8
1801+ return rb_enc_associate_index (rb_str_dup (source ), utf8_encindex );
18071802 }
18081803
1809- return rb_str_conv_enc (source , rb_enc_from_index ( encindex ), rb_utf8_encoding () );
1804+ return rb_funcall (source , i_encode , 1 , Encoding_UTF_8 );
18101805}
18111806
18121807/*
@@ -1958,15 +1953,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
19581953}
19591954
19601955
1961- #line 1962 "parser.c"
1956+ #line 1957 "parser.c"
19621957enum {JSON_start = 1 };
19631958enum {JSON_first_final = 10 };
19641959enum {JSON_error = 0 };
19651960
19661961enum {JSON_en_main = 1 };
19671962
19681963
1969- #line 870 "parser.rl"
1964+ #line 865 "parser.rl"
19701965
19711966
19721967/*
@@ -1984,16 +1979,16 @@ static VALUE cParser_parse(VALUE self)
19841979 GET_PARSER ;
19851980
19861981
1987- #line 1988 "parser.c"
1982+ #line 1983 "parser.c"
19881983 {
19891984 cs = JSON_start ;
19901985 }
19911986
1992- #line 887 "parser.rl"
1987+ #line 882 "parser.rl"
19931988 p = json -> source ;
19941989 pe = p + json -> len ;
19951990
1996- #line 1997 "parser.c"
1991+ #line 1992 "parser.c"
19971992 {
19981993 if ( p == pe )
19991994 goto _test_eof ;
@@ -2027,7 +2022,7 @@ case 1:
20272022cs = 0 ;
20282023 goto _out ;
20292024tr2 :
2030- #line 862 "parser.rl"
2025+ #line 857 "parser.rl"
20312026 {
20322027 char * np = JSON_parse_value (json , p , pe , & result , 0 );
20332028 if (np == NULL ) { p -- ; {p ++ ; cs = 10 ; goto _out ;} } else {p = (( np ))- 1 ;}
@@ -2037,7 +2032,7 @@ cs = 0;
20372032 if ( ++ p == pe )
20382033 goto _test_eof10 ;
20392034case 10 :
2040- #line 2041 "parser.c"
2035+ #line 2036 "parser.c"
20412036 switch ( (* p ) ) {
20422037 case 13 : goto st10 ;
20432038 case 32 : goto st10 ;
@@ -2126,7 +2121,7 @@ case 9:
21262121 _out : {}
21272122 }
21282123
2129- #line 890 "parser.rl"
2124+ #line 885 "parser.rl"
21302125
21312126 if (cs >= JSON_first_final && p == pe ) {
21322127 return result ;
@@ -2214,6 +2209,9 @@ void Init_parser(void)
22142209 CMinusInfinity = rb_const_get (mJSON , rb_intern ("MinusInfinity" ));
22152210 rb_gc_register_mark_object (CMinusInfinity );
22162211
2212+ rb_global_variable (& Encoding_UTF_8 );
2213+ Encoding_UTF_8 = rb_const_get (rb_path2class ("Encoding" ), rb_intern ("UTF_8" ));
2214+
22172215 i_json_creatable_p = rb_intern ("json_creatable?" );
22182216 i_json_create = rb_intern ("json_create" );
22192217 i_create_id = rb_intern ("create_id" );
@@ -2235,6 +2233,7 @@ void Init_parser(void)
22352233 i_try_convert = rb_intern ("try_convert" );
22362234 i_freeze = rb_intern ("freeze" );
22372235 i_uminus = rb_intern ("-@" );
2236+ i_encode = rb_intern ("encode" );
22382237
22392238 binary_encindex = rb_ascii8bit_encindex ();
22402239 utf8_encindex = rb_utf8_encindex ();
0 commit comments