@@ -203,19 +203,13 @@ public void PushSettings(Action<BsonWriterSettings> configurator)
203
203
/// Writes the end of a BSON array to the writer.
204
204
/// </summary>
205
205
public virtual void WriteEndArray ( )
206
- {
207
- _serializationDepth -- ;
208
- }
206
+ => ExitSerializationScope ( ) ;
209
207
210
208
/// <summary>
211
209
/// Writes the end of a BSON document to the writer.
212
210
/// </summary>
213
211
public virtual void WriteEndDocument ( )
214
- {
215
- _serializationDepth -- ;
216
-
217
- PopElementNameValidator ( ) ;
218
- }
212
+ => ExitSerializationScope ( ) ;
219
213
220
214
/// <summary>
221
215
/// Writes a BSON Int32 to the writer.
@@ -362,28 +356,13 @@ public virtual void WriteRawBsonDocument(IByteBuffer slice)
362
356
/// Writes the start of a BSON array to the writer.
363
357
/// </summary>
364
358
public virtual void WriteStartArray ( )
365
- {
366
- _serializationDepth ++ ;
367
- if ( _serializationDepth > _settings . MaxSerializationDepth )
368
- {
369
- throw new BsonSerializationException ( "Maximum serialization depth exceeded (does the object being serialized have a circular reference?)." ) ;
370
- }
371
- }
359
+ => EnterSerializationScope ( ) ;
372
360
373
361
/// <summary>
374
362
/// Writes the start of a BSON document to the writer.
375
363
/// </summary>
376
364
public virtual void WriteStartDocument ( )
377
- {
378
- _serializationDepth ++ ;
379
- if ( _serializationDepth > _settings . MaxSerializationDepth )
380
- {
381
- throw new BsonSerializationException ( "Maximum serialization depth exceeded (does the object being serialized have a circular reference?)." ) ;
382
- }
383
-
384
- var childValidator = _useChildValidator ? _elementNameValidator . GetValidatorForChildContent ( _name ) : _elementNameValidator ;
385
- PushElementNameValidator ( childValidator ) ;
386
- }
365
+ => EnterSerializationScope ( ) ;
387
366
388
367
/// <summary>
389
368
/// Writes a BSON String to the writer.
@@ -470,5 +449,23 @@ protected void ThrowInvalidState(string methodName, params BsonWriterState[] val
470
449
methodName , validStatesString , _state ) ;
471
450
throw new InvalidOperationException ( message ) ;
472
451
}
452
+
453
+ private void EnterSerializationScope ( )
454
+ {
455
+ _serializationDepth ++ ;
456
+ if ( _serializationDepth > _settings . MaxSerializationDepth )
457
+ {
458
+ throw new BsonSerializationException ( "Maximum serialization depth exceeded (does the object being serialized have a circular reference?)." ) ;
459
+ }
460
+
461
+ var childValidator = _useChildValidator ? _elementNameValidator . GetValidatorForChildContent ( _name ) : _elementNameValidator ;
462
+ PushElementNameValidator ( childValidator ) ;
463
+ }
464
+
465
+ private void ExitSerializationScope ( )
466
+ {
467
+ _serializationDepth -- ;
468
+ PopElementNameValidator ( ) ;
469
+ }
473
470
}
474
471
}
0 commit comments