@@ -986,16 +986,10 @@ static inline VALUE vstate_get(struct generate_json_data *data)
986
986
return data -> vstate ;
987
987
}
988
988
989
- enum key_types {
990
- KEY_UNKNOWN ,
991
- KEY_SYMBOL ,
992
- KEY_STRING ,
993
- };
994
-
995
989
struct hash_foreach_arg {
996
990
VALUE hash ;
997
991
struct generate_json_data * data ;
998
- enum key_types keys ;
992
+ int first_key_type ;
999
993
bool first ;
1000
994
};
1001
995
@@ -1025,8 +1019,11 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1025
1019
1026
1020
long depth = state -> depth ;
1027
1021
1028
- if (RB_UNLIKELY (arg -> first )) {
1022
+ int key_type = rb_type (key );
1023
+
1024
+ if (arg -> first ) {
1029
1025
arg -> first = false;
1026
+ arg -> first_key_type = key_type ;
1030
1027
}
1031
1028
else {
1032
1029
fbuffer_append_char (buffer , ',' );
@@ -1043,13 +1040,10 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1043
1040
bool as_json_called = false;
1044
1041
1045
1042
start :
1046
- switch (rb_type ( key ) ) {
1043
+ switch (key_type ) {
1047
1044
case T_STRING :
1048
- if (arg -> keys == KEY_UNKNOWN ) {
1049
- arg -> keys = KEY_STRING ;
1050
- }
1051
- else if (RB_UNLIKELY (arg -> keys == KEY_SYMBOL )) {
1052
- fprintf (stderr , "mixed keys\n" );
1045
+ if (RB_UNLIKELY (arg -> first_key_type != T_STRING )) {
1046
+ fprintf (stderr , "mixed keys (str)\n" );
1053
1047
}
1054
1048
1055
1049
if (RB_LIKELY (RBASIC_CLASS (key ) == rb_cString )) {
@@ -1059,11 +1053,8 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1059
1053
}
1060
1054
break ;
1061
1055
case T_SYMBOL :
1062
- if (arg -> keys == KEY_UNKNOWN ) {
1063
- arg -> keys = KEY_SYMBOL ;
1064
- }
1065
- else if (RB_UNLIKELY (arg -> keys == KEY_STRING )) {
1066
- fprintf (stderr , "mixed keys\n" );
1056
+ if (RB_UNLIKELY (arg -> first_key_type != T_SYMBOL )) {
1057
+ fprintf (stderr , "mixed keys (sym)\n" );
1067
1058
}
1068
1059
1069
1060
key_to_s = rb_sym2str (key );
@@ -1072,6 +1063,7 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1072
1063
if (data -> state -> strict ) {
1073
1064
if (RTEST (data -> state -> as_json ) && !as_json_called ) {
1074
1065
key = rb_proc_call_with_block (data -> state -> as_json , 1 , & key , Qnil );
1066
+ key_type = rb_type (key );
1075
1067
as_json_called = true;
1076
1068
goto start ;
1077
1069
} else {
0 commit comments