Skip to content

Commit 60f871b

Browse files
tkp1nahsonkhan
authored andcommitted
Utf8JsonReader should store if parsed number contains exponent (dotnet#34386)
* Store if number contains exponent * Store number format directly
1 parent 4d82935 commit 60f871b

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public struct JsonReaderState
2222
internal int _maxDepth;
2323
internal bool _inObject;
2424
internal bool _isNotPrimitive;
25+
internal char _numberFormat;
2526
internal JsonTokenType _tokenType;
2627
internal JsonTokenType _previousTokenType;
2728
internal JsonReaderOptions _readerOptions;
@@ -69,6 +70,7 @@ public JsonReaderState(int maxDepth = DefaultMaxDepth, JsonReaderOptions options
6970
_maxDepth = maxDepth;
7071
_inObject = default;
7172
_isNotPrimitive = default;
73+
_numberFormat = default;
7274
_tokenType = default;
7375
_previousTokenType = default;
7476
_readerOptions = options;

src/System.Text.Json/src/System/Text/Json/Utf8JsonReader.MultiSegment.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public Utf8JsonReader(in ReadOnlySequence<byte> jsonData, bool isFinalBlock, Jso
3434
_maxDepth = state._maxDepth == 0 ? JsonReaderState.DefaultMaxDepth : state._maxDepth; // If max depth is not set, revert to the default depth.
3535
_inObject = state._inObject;
3636
_isNotPrimitive = state._isNotPrimitive;
37+
_numberFormat = state._numberFormat;
3738
_tokenType = state._tokenType;
3839
_previousTokenType = state._previousTokenType;
3940
_readerOptions = state._readerOptions;
@@ -1108,6 +1109,7 @@ private bool TryGetNumberMultiSegment(ReadOnlySpan<byte> data, out int consumed)
11081109
// TODO: https://github.com/dotnet/corefx/issues/33294
11091110
Debug.Assert(data.Length > 0);
11101111

1112+
_numberFormat = default;
11111113
SequencePosition startPosition = _currentPosition;
11121114
int startConsumed = _consumed;
11131115
consumed = 0;
@@ -1199,6 +1201,7 @@ private bool TryGetNumberMultiSegment(ReadOnlySpan<byte> data, out int consumed)
11991201

12001202
Debug.Assert(nextByte == 'E' || nextByte == 'e');
12011203
i++;
1204+
_numberFormat = 'e';
12021205
_bytePositionInLine++;
12031206

12041207
signResult = ConsumeSignMultiSegment(ref data, ref i);

src/System.Text.Json/src/System/Text/Json/Utf8JsonReader.TryGet.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ public bool TryGetSingleValue(out float value)
125125
}
126126

127127
ReadOnlySpan<byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan;
128-
char standardFormat = span.IndexOfAny((byte)'e', (byte)'E') >= 0 ? 'e' : default;
129-
return Utf8Parser.TryParse(span, out value, out int bytesConsumed, standardFormat) && span.Length == bytesConsumed;
128+
return Utf8Parser.TryParse(span, out value, out int bytesConsumed, _numberFormat) && span.Length == bytesConsumed;
130129
}
131130

132131
/// <summary>
@@ -147,8 +146,7 @@ public bool TryGetDoubleValue(out double value)
147146
}
148147

149148
ReadOnlySpan<byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan;
150-
char standardFormat = span.IndexOfAny((byte)'e', (byte)'E') >= 0 ? 'e' : default;
151-
return Utf8Parser.TryParse(span, out value, out int bytesConsumed, standardFormat) && span.Length == bytesConsumed;
149+
return Utf8Parser.TryParse(span, out value, out int bytesConsumed, _numberFormat) && span.Length == bytesConsumed;
152150
}
153151

154152
/// <summary>
@@ -169,8 +167,7 @@ public bool TryGetDecimalValue(out decimal value)
169167
}
170168

171169
ReadOnlySpan<byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan;
172-
char standardFormat = span.IndexOfAny((byte)'e', (byte)'E') >= 0 ? 'e' : default;
173-
return Utf8Parser.TryParse(span, out value, out int bytesConsumed, standardFormat) && span.Length == bytesConsumed;
170+
return Utf8Parser.TryParse(span, out value, out int bytesConsumed, _numberFormat) && span.Length == bytesConsumed;
174171
}
175172
}
176173
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public ref partial struct Utf8JsonReader
3030
private int _maxDepth;
3131
private bool _inObject;
3232
private bool _isNotPrimitive;
33+
private char _numberFormat;
3334
private JsonTokenType _tokenType;
3435
private JsonTokenType _previousTokenType;
3536
private JsonReaderOptions _readerOptions;
@@ -130,6 +131,7 @@ public SequencePosition Position
130131
_maxDepth = _maxDepth,
131132
_inObject = _inObject,
132133
_isNotPrimitive = _isNotPrimitive,
134+
_numberFormat = _numberFormat,
133135
_tokenType = _tokenType,
134136
_previousTokenType = _previousTokenType,
135137
_readerOptions = _readerOptions,
@@ -160,6 +162,7 @@ public Utf8JsonReader(ReadOnlySpan<byte> jsonData, bool isFinalBlock, JsonReader
160162
_maxDepth = state._maxDepth == 0 ? JsonReaderState.DefaultMaxDepth : state._maxDepth; // If max depth is not set, revert to the default depth.
161163
_inObject = state._inObject;
162164
_isNotPrimitive = state._isNotPrimitive;
165+
_numberFormat = state._numberFormat;
163166
_tokenType = state._tokenType;
164167
_previousTokenType = state._previousTokenType;
165168
_readerOptions = state._readerOptions;
@@ -900,6 +903,7 @@ private bool TryGetNumber(ReadOnlySpan<byte> data, out int consumed)
900903
// TODO: https://github.com/dotnet/corefx/issues/33294
901904
Debug.Assert(data.Length > 0);
902905

906+
_numberFormat = default;
903907
consumed = 0;
904908
int i = 0;
905909

@@ -977,6 +981,7 @@ private bool TryGetNumber(ReadOnlySpan<byte> data, out int consumed)
977981

978982
Debug.Assert(nextByte == 'E' || nextByte == 'e');
979983
i++;
984+
_numberFormat = 'e';
980985

981986
signResult = ConsumeSign(ref data, ref i);
982987
if (signResult == ConsumeNumberResult.NeedMoreData)

0 commit comments

Comments
 (0)