@@ -128,7 +128,7 @@ protected internal enum State
128128 internal DateParseHandling _dateParseHandling ;
129129 internal FloatParseHandling _floatParseHandling ;
130130 private string _dateFormatString ;
131- private readonly List < JsonPosition > _stack ;
131+ private List < JsonPosition > _stack ;
132132
133133 /// <summary>
134134 /// Gets the current reader state.
@@ -280,11 +280,15 @@ public virtual int Depth
280280 {
281281 get
282282 {
283- int depth = _stack . Count ;
283+ int depth = ( _stack != null ) ? _stack . Count : 0 ;
284284 if ( JsonTokenUtils . IsStartToken ( TokenType ) || _currentPosition . Type == JsonContainerType . None )
285+ {
285286 return depth ;
287+ }
286288 else
289+ {
287290 return depth + 1 ;
291+ }
288292 }
289293 }
290294
@@ -302,11 +306,9 @@ public virtual string Path
302306 && _currentState != State . ConstructorStart
303307 && _currentState != State . ObjectStart ) ;
304308
305- IEnumerable < JsonPosition > positions = ( ! insideContainer )
306- ? _stack
307- : _stack . Concat ( new [ ] { _currentPosition } ) ;
309+ JsonPosition ? current = insideContainer ? ( JsonPosition ? ) _currentPosition : null ;
308310
309- return JsonPosition . BuildPath ( positions ) ;
311+ return JsonPosition . BuildPath ( _stack , current ) ;
310312 }
311313 }
312314
@@ -321,8 +323,10 @@ public CultureInfo Culture
321323
322324 internal JsonPosition GetPosition ( int depth )
323325 {
324- if ( depth < _stack . Count )
326+ if ( _stack != null && depth < _stack . Count )
327+ {
325328 return _stack [ depth ] ;
329+ }
326330
327331 return _currentPosition ;
328332 }
@@ -333,7 +337,6 @@ internal JsonPosition GetPosition(int depth)
333337 protected JsonReader ( )
334338 {
335339 _currentState = State . Start ;
336- _stack = new List < JsonPosition > ( 4 ) ;
337340 _dateTimeZoneHandling = DateTimeZoneHandling . RoundtripKind ;
338341 _dateParseHandling = DateParseHandling . DateTime ;
339342 _floatParseHandling = FloatParseHandling . Double ;
@@ -351,6 +354,11 @@ private void Push(JsonContainerType value)
351354 }
352355 else
353356 {
357+ if ( _stack == null )
358+ {
359+ _stack = new List < JsonPosition > ( ) ;
360+ }
361+
354362 _stack . Add ( _currentPosition ) ;
355363 _currentPosition = new JsonPosition ( value ) ;
356364
@@ -366,7 +374,7 @@ private void Push(JsonContainerType value)
366374 private JsonContainerType Pop ( )
367375 {
368376 JsonPosition oldPosition ;
369- if ( _stack . Count > 0 )
377+ if ( _stack != null && _stack . Count > 0 )
370378 {
371379 oldPosition = _currentPosition ;
372380 _currentPosition = _stack [ _stack . Count - 1 ] ;
@@ -379,7 +387,9 @@ private JsonContainerType Pop()
379387 }
380388
381389 if ( _maxDepth != null && Depth <= _maxDepth )
390+ {
382391 _hasExceededMaxDepth = false ;
392+ }
383393
384394 return oldPosition . Type ;
385395 }
0 commit comments