@@ -29,6 +29,7 @@ typedef struct JSON_Generator_StateStruct {
2929
3030 enum duplicate_key_action on_duplicate_key ;
3131
32+ bool as_json_single_arg ;
3233 bool allow_nan ;
3334 bool ascii_only ;
3435 bool script_safe ;
@@ -1033,6 +1034,13 @@ json_inspect_hash_with_mixed_keys(struct hash_foreach_arg *arg)
10331034 }
10341035}
10351036
1037+ static VALUE
1038+ json_call_as_json (JSON_Generator_State * state , VALUE object , VALUE is_key )
1039+ {
1040+ VALUE proc_args [2 ] = {object , is_key };
1041+ return rb_proc_call_with_block (state -> as_json , 2 , proc_args , Qnil );
1042+ }
1043+
10361044static int
10371045json_object_i (VALUE key , VALUE val , VALUE _arg )
10381046{
@@ -1086,7 +1094,7 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
10861094 default :
10871095 if (data -> state -> strict ) {
10881096 if (RTEST (data -> state -> as_json ) && !as_json_called ) {
1089- key = rb_proc_call_with_block (data -> state -> as_json , 1 , & key , Qnil );
1097+ key = json_call_as_json (data -> state , key , Qtrue );
10901098 key_type = rb_type (key );
10911099 as_json_called = true;
10921100 goto start ;
@@ -1328,7 +1336,7 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
13281336 /* for NaN and Infinity values we either raise an error or rely on Float#to_s. */
13291337 if (!allow_nan ) {
13301338 if (data -> state -> strict && data -> state -> as_json ) {
1331- VALUE casted_obj = rb_proc_call_with_block (data -> state -> as_json , 1 , & obj , Qnil );
1339+ VALUE casted_obj = json_call_as_json (data -> state , obj , Qfalse );
13321340 if (casted_obj != obj ) {
13331341 increase_depth (data );
13341342 generate_json (buffer , data , casted_obj );
@@ -1416,7 +1424,7 @@ static void generate_json(FBuffer *buffer, struct generate_json_data *data, VALU
14161424 general :
14171425 if (data -> state -> strict ) {
14181426 if (RTEST (data -> state -> as_json ) && !as_json_called ) {
1419- obj = rb_proc_call_with_block (data -> state -> as_json , 1 , & obj , Qnil );
1427+ obj = json_call_as_json (data -> state , obj , Qfalse );
14201428 as_json_called = true;
14211429 goto start ;
14221430 } else {
@@ -1942,6 +1950,7 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
19421950 else if (key == sym_allow_duplicate_key ) { state -> on_duplicate_key = RTEST (val ) ? JSON_IGNORE : JSON_RAISE ; }
19431951 else if (key == sym_as_json ) {
19441952 VALUE proc = RTEST (val ) ? rb_convert_type (val , T_DATA , "Proc" , "to_proc" ) : Qfalse ;
1953+ state -> as_json_single_arg = proc && rb_proc_arity (proc ) == 1 ;
19451954 state_write_value (data , & state -> as_json , proc );
19461955 }
19471956 return ST_CONTINUE ;
0 commit comments