@@ -15,7 +15,7 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
1515#endif
1616 mFloat , mString , mString_Extend ,
1717 mTrueClass , mFalseClass , mNilClass , eGeneratorError ,
18- eNestingError , CRegexp_MULTILINE , CJSON_SAFE_STATE_PROTOTYPE ,
18+ eNestingError ,
1919 i_SAFE_STATE_PROTOTYPE ;
2020
2121static ID i_to_s , i_to_json , i_new , i_indent , i_space , i_space_before ,
@@ -696,7 +696,7 @@ static VALUE cState_aref(VALUE self, VALUE name)
696696 if (RTEST (rb_funcall (self , i_respond_to_p , 1 , name ))) {
697697 return rb_funcall (self , i_send , 1 , name );
698698 } else {
699- return rb_ivar_get (self , rb_intern_str (rb_str_concat (rb_str_new2 ("@" ), name )));
699+ return rb_attr_get (self , rb_intern_str (rb_str_concat (rb_str_new2 ("@" ), name )));
700700 }
701701}
702702
@@ -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+
849858static 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
@@ -1073,10 +1082,8 @@ static VALUE cState_from_state_s(VALUE self, VALUE opts)
10731082 } else if (rb_obj_is_kind_of (opts , rb_cHash )) {
10741083 return rb_funcall (self , i_new , 1 , opts );
10751084 } else {
1076- if (NIL_P (CJSON_SAFE_STATE_PROTOTYPE )) {
1077- CJSON_SAFE_STATE_PROTOTYPE = rb_const_get (mJSON , i_SAFE_STATE_PROTOTYPE );
1078- }
1079- return rb_funcall (CJSON_SAFE_STATE_PROTOTYPE , i_dup , 0 );
1085+ VALUE prototype = rb_const_get (mJSON , i_SAFE_STATE_PROTOTYPE );
1086+ return rb_funcall (prototype , i_dup , 0 );
10801087 }
10811088}
10821089
@@ -1392,6 +1399,8 @@ void Init_generator(void)
13921399
13931400 eGeneratorError = rb_path2class ("JSON::GeneratorError" );
13941401 eNestingError = rb_path2class ("JSON::NestingError" );
1402+ rb_gc_register_mark_object (eGeneratorError );
1403+ rb_gc_register_mark_object (eNestingError );
13951404
13961405 cState = rb_define_class_under (mGenerator , "State" , rb_cObject );
13971406 rb_define_alloc_func (cState , cState_s_allocate );
@@ -1457,7 +1466,6 @@ void Init_generator(void)
14571466 mNilClass = rb_define_module_under (mGeneratorMethods , "NilClass" );
14581467 rb_define_method (mNilClass , "to_json" , mNilClass_to_json , -1 );
14591468
1460- CRegexp_MULTILINE = rb_const_get (rb_cRegexp , rb_intern ("MULTILINE" ));
14611469 i_to_s = rb_intern ("to_s" );
14621470 i_to_json = rb_intern ("to_json" );
14631471 i_new = rb_intern ("new" );
@@ -1488,5 +1496,4 @@ void Init_generator(void)
14881496 i_encode = rb_intern ("encode" );
14891497#endif
14901498 i_SAFE_STATE_PROTOTYPE = rb_intern ("SAFE_STATE_PROTOTYPE" );
1491- CJSON_SAFE_STATE_PROTOTYPE = Qnil ;
14921499}
0 commit comments