@@ -60,6 +60,7 @@ struct generate_json_data {
6060 JSON_Generator_State * state ;
6161 VALUE obj ;
6262 generator_func func ;
63+ long depth ;
6364};
6465
6566static VALUE cState_from_state_s (VALUE self , VALUE opts );
@@ -1145,7 +1146,7 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
11451146 FBuffer * buffer = data -> buffer ;
11461147 JSON_Generator_State * state = data -> state ;
11471148
1148- long depth = state -> depth ;
1149+ long depth = data -> depth ;
11491150 int key_type = rb_type (key );
11501151
11511152 if (arg -> first ) {
@@ -1219,9 +1220,9 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
12191220static inline long increase_depth (struct generate_json_data * data )
12201221{
12211222 JSON_Generator_State * state = data -> state ;
1222- long depth = ++ state -> depth ;
1223+ long depth = ++ data -> depth ;
12231224 if (RB_UNLIKELY (depth > state -> max_nesting && state -> max_nesting )) {
1224- rb_raise (eNestingError , "nesting of %ld is too deep. Did you try to serialize objects with circular references?" , -- state -> depth );
1225+ rb_raise (eNestingError , "nesting of %ld is too deep. Did you try to serialize objects with circular references?" , -- data -> depth );
12251226 }
12261227 return depth ;
12271228}
@@ -1232,7 +1233,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
12321233
12331234 if (RHASH_SIZE (obj ) == 0 ) {
12341235 fbuffer_append (buffer , "{}" , 2 );
1235- -- data -> state -> depth ;
1236+ -- data -> depth ;
12361237 return ;
12371238 }
12381239
@@ -1245,7 +1246,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
12451246 };
12461247 rb_hash_foreach (obj , json_object_i , (VALUE )& arg );
12471248
1248- depth = -- data -> state -> depth ;
1249+ depth = -- data -> depth ;
12491250 if (RB_UNLIKELY (data -> state -> object_nl )) {
12501251 fbuffer_append_str (buffer , data -> state -> object_nl );
12511252 if (RB_UNLIKELY (data -> state -> indent )) {
@@ -1261,7 +1262,7 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
12611262
12621263 if (RARRAY_LEN (obj ) == 0 ) {
12631264 fbuffer_append (buffer , "[]" , 2 );
1264- -- data -> state -> depth ;
1265+ -- data -> depth ;
12651266 return ;
12661267 }
12671268
@@ -1277,7 +1278,7 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
12771278 }
12781279 generate_json (buffer , data , RARRAY_AREF (obj , i ));
12791280 }
1280- data -> state -> depth = -- depth ;
1281+ data -> depth = -- depth ;
12811282 if (RB_UNLIKELY (data -> state -> array_nl )) {
12821283 fbuffer_append_str (buffer , data -> state -> array_nl );
12831284 if (RB_UNLIKELY (data -> state -> indent )) {
@@ -1358,7 +1359,7 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
13581359 if (casted_obj != obj ) {
13591360 increase_depth (data );
13601361 generate_json (buffer , data , casted_obj );
1361- data -> state -> depth -- ;
1362+ data -> depth -- ;
13621363 return ;
13631364 }
13641365 }
@@ -1477,6 +1478,7 @@ static VALUE generate_json_ensure(VALUE d)
14771478{
14781479 struct generate_json_data * data = (struct generate_json_data * )d ;
14791480 fbuffer_free (data -> buffer );
1481+ data -> state -> depth = data -> depth ;
14801482
14811483 return Qundef ;
14821484}
@@ -1495,6 +1497,7 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func func,
14951497 .buffer = & buffer ,
14961498 .vstate = self ,
14971499 .state = state ,
1500+ .depth = state -> depth ,
14981501 .obj = obj ,
14991502 .func = func
15001503 };
@@ -1541,6 +1544,7 @@ static VALUE cState_generate_new(int argc, VALUE *argv, VALUE self)
15411544 .buffer = & buffer ,
15421545 .vstate = Qfalse ,
15431546 .state = & new_state ,
1547+ .depth = new_state .depth ,
15441548 .obj = obj ,
15451549 .func = generate_json
15461550 };
@@ -2061,6 +2065,7 @@ static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
20612065 .buffer = & buffer ,
20622066 .vstate = Qfalse ,
20632067 .state = & state ,
2068+ .depth = state .depth ,
20642069 .obj = obj ,
20652070 .func = generate_json ,
20662071 };
0 commit comments