Skip to content

Commit d250dcc

Browse files
Make JsonReaderState a readonly struct. (#97421)
1 parent 85e9e7b commit d250dcc

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

src/libraries/System.Text.Json/ref/System.Text.Json.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ public partial struct JsonReaderOptions
190190
public System.Text.Json.JsonCommentHandling CommentHandling { readonly get { throw null; } set { } }
191191
public int MaxDepth { readonly get { throw null; } set { } }
192192
}
193-
public partial struct JsonReaderState
193+
public readonly partial struct JsonReaderState
194194
{
195-
private object _dummy;
196-
private int _dummyPrimitive;
195+
private readonly object _dummy;
196+
private readonly int _dummyPrimitive;
197197
public JsonReaderState(System.Text.Json.JsonReaderOptions options = default(System.Text.Json.JsonReaderOptions)) { throw null; }
198198
public System.Text.Json.JsonReaderOptions Options { get { throw null; } }
199199
}

src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderState.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ namespace System.Text.Json
1111
/// this type can survive across async/await boundaries and hence this type is required to provide
1212
/// support for reading in more data asynchronously before continuing with a new instance of the <see cref="Utf8JsonReader"/>.
1313
/// </summary>
14-
public struct JsonReaderState
14+
public readonly struct JsonReaderState
1515
{
16-
internal long _lineNumber;
17-
internal long _bytePositionInLine;
18-
internal bool _inObject;
19-
internal bool _isNotPrimitive;
20-
internal bool _valueIsEscaped;
21-
internal bool _trailingCommaBeforeComment;
22-
internal JsonTokenType _tokenType;
23-
internal JsonTokenType _previousTokenType;
24-
internal JsonReaderOptions _readerOptions;
25-
internal BitStack _bitStack;
16+
internal readonly long _lineNumber;
17+
internal readonly long _bytePositionInLine;
18+
internal readonly bool _inObject;
19+
internal readonly bool _isNotPrimitive;
20+
internal readonly bool _valueIsEscaped;
21+
internal readonly bool _trailingCommaBeforeComment;
22+
internal readonly JsonTokenType _tokenType;
23+
internal readonly JsonTokenType _previousTokenType;
24+
internal readonly JsonReaderOptions _readerOptions;
25+
internal readonly BitStack _bitStack;
2626

2727
/// <summary>
2828
/// Constructs a new <see cref="JsonReaderState"/> instance.
@@ -53,6 +53,30 @@ public JsonReaderState(JsonReaderOptions options = default)
5353
_bitStack = default;
5454
}
5555

56+
internal JsonReaderState(
57+
long lineNumber,
58+
long bytePositionInLine,
59+
bool inObject,
60+
bool isNotPrimitive,
61+
bool valueIsEscaped,
62+
bool trailingCommaBeforeComment,
63+
JsonTokenType tokenType,
64+
JsonTokenType previousTokenType,
65+
JsonReaderOptions readerOptions,
66+
BitStack bitStack)
67+
{
68+
_lineNumber = lineNumber;
69+
_bytePositionInLine = bytePositionInLine;
70+
_inObject = inObject;
71+
_isNotPrimitive = isNotPrimitive;
72+
_valueIsEscaped = valueIsEscaped;
73+
_trailingCommaBeforeComment = trailingCommaBeforeComment;
74+
_tokenType = tokenType;
75+
_previousTokenType = previousTokenType;
76+
_readerOptions = readerOptions;
77+
_bitStack = bitStack;
78+
}
79+
5680
/// <summary>
5781
/// Gets the custom behavior when reading JSON using
5882
/// the <see cref="Utf8JsonReader"/> that may deviate from strict adherence

src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public ref partial struct Utf8JsonReader
4747
private SequencePosition _currentPosition;
4848
private readonly ReadOnlySequence<byte> _sequence;
4949

50-
private bool IsLastSpan => _isFinalBlock && (!_isMultiSegment || _isLastSegment);
50+
private readonly bool IsLastSpan => _isFinalBlock && (!_isMultiSegment || _isLastSegment);
5151

52-
internal ReadOnlySequence<byte> OriginalSequence => _sequence;
52+
internal readonly ReadOnlySequence<byte> OriginalSequence => _sequence;
5353

54-
internal ReadOnlySpan<byte> OriginalSpan => _sequence.IsEmpty ? _buffer : default;
54+
internal readonly ReadOnlySpan<byte> OriginalSpan => _sequence.IsEmpty ? _buffer : default;
5555

5656
internal readonly int ValueLength => HasValueSequence ? checked((int)ValueSequence.Length) : ValueSpan.Length;
5757

@@ -184,18 +184,18 @@ public readonly SequencePosition Position
184184
/// in more data asynchronously before continuing with a new instance of the <see cref="Utf8JsonReader"/>.
185185
/// </summary>
186186
public readonly JsonReaderState CurrentState => new JsonReaderState
187-
{
188-
_lineNumber = _lineNumber,
189-
_bytePositionInLine = _bytePositionInLine,
190-
_inObject = _inObject,
191-
_isNotPrimitive = _isNotPrimitive,
192-
_valueIsEscaped = ValueIsEscaped,
193-
_trailingCommaBeforeComment = _trailingCommaBeforeComment,
194-
_tokenType = _tokenType,
195-
_previousTokenType = _previousTokenType,
196-
_readerOptions = _readerOptions,
197-
_bitStack = _bitStack,
198-
};
187+
(
188+
lineNumber: _lineNumber,
189+
bytePositionInLine: _bytePositionInLine,
190+
inObject: _inObject,
191+
isNotPrimitive: _isNotPrimitive,
192+
valueIsEscaped: ValueIsEscaped,
193+
trailingCommaBeforeComment: _trailingCommaBeforeComment,
194+
tokenType: _tokenType,
195+
previousTokenType: _previousTokenType,
196+
readerOptions: _readerOptions,
197+
bitStack: _bitStack
198+
);
199199

200200
/// <summary>
201201
/// Constructs a new <see cref="Utf8JsonReader"/> instance.

0 commit comments

Comments
 (0)