Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add ReadOnlySequence support to Utf8JsonReader with unit tests #33462

Merged
merged 15 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public partial struct JsonReaderState
public long BytesConsumed { get { throw null; } }
public int MaxDepth { get { throw null; } }
public System.Text.Json.JsonReaderOptions Options { get { throw null; } }
public System.SequencePosition Position { get { throw null; } }
}
public enum JsonTokenType : byte
{
Expand All @@ -51,11 +52,15 @@ public enum JsonTokenType : byte
public ref partial struct Utf8JsonReader
{
private object _dummy;
public Utf8JsonReader(in System.Buffers.ReadOnlySequence<byte> jsonData, bool isFinalBlock, System.Text.Json.JsonReaderState state) { throw null; }
public Utf8JsonReader(System.ReadOnlySpan<byte> jsonData, bool isFinalBlock, System.Text.Json.JsonReaderState state) { throw null; }
public long BytesConsumed { get { throw null; } }
public int CurrentDepth { get { throw null; } }
public System.Text.Json.JsonReaderState CurrentState { get { throw null; } }
public bool HasValueSequence { get { throw null; } }
public System.SequencePosition Position { get { throw null; } }
public System.Text.Json.JsonTokenType TokenType { get { throw null; } }
public System.Buffers.ReadOnlySequence<byte> ValueSequence { get { throw null; } }
public System.ReadOnlySpan<byte> ValueSpan { get { throw null; } }
public bool GetBooleanValue() { throw null; }
public string GetStringValue() { throw null; }
Expand Down
10 changes: 8 additions & 2 deletions src/System.Text.Json/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,18 @@
<data name="InvalidCast" xml:space="preserve">
<value>Cannot get the value of a token type '{0}' as a {1}.</value>
</data>
<data name="InvalidCharacterAfterEscapeWithinString" xml:space="preserve">
<value>'{0}' is an invalid escapable character within a JSON string. The string should be correctly escaped.</value>
</data>
<data name="InvalidCharacterWithinString" xml:space="preserve">
<value>'{0}' is invalid within a JSON string. The string should be correctly escaped.</value>
</data>
<data name="InvalidEndOfJsonNonPrimitive" xml:space="preserve">
<value>'{0}' is an invalid token type for the end of the JSON payload. Expected either 'EndArray' or 'EndObject'.</value>
</data>
<data name="InvalidHexCharacterWithinString" xml:space="preserve">
<value>'{0}' is not a hex digit following '\u' within a JSON string. The string should be correctly escaped.</value>
</data>
<data name="MaxDepthMustBePositive" xml:space="preserve">
<value>Max depth must be positive.</value>
</data>
Expand All @@ -181,10 +187,10 @@
<value>CurrentDepth ({0}) is larger than the maximum configured depth of {1}. Cannot read next JSON object.</value>
</data>
<data name="RequiredDigitNotFoundAfterDecimal" xml:space="preserve">
<value>'{0}' is invalid within a number immediately after a decimal point ('.'). Expected a digit ('0'-'9').</value>
<value>'{0}' is invalid within a number, immediately after a decimal point ('.'). Expected a digit ('0'-'9').</value>
</data>
<data name="RequiredDigitNotFoundAfterSign" xml:space="preserve">
<value>'{0}' is invalid within a number immediately after a sign character ('+' or '-'). Expected a digit ('0'-'9').</value>
<value>'{0}' is invalid within a number, immediately after a sign character ('+' or '-'). Expected a digit ('0'-'9').</value>
</data>
<data name="RequiredDigitNotFoundEndOfData" xml:space="preserve">
<value>Expected a digit ('0'-'9'), but instead reached end of data.</value>
Expand Down
1 change: 1 addition & 0 deletions src/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Compile Include="System\Text\Json\JsonTokenType.cs" />
<Compile Include="System\Text\Json\ThrowHelper.cs" />
<Compile Include="System\Text\Json\Utf8JsonReader.cs" />
<Compile Include="System\Text\Json\Utf8JsonReader.MultiSegment.cs" />
<Compile Include="System\Text\Json\Utf8JsonReader.TryGet.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static bool IsHexDigit(byte nextByte) =>
// Borrowed and modified from SpanHelpers.Byte:
// https://github.com/dotnet/corefx/blob/fc169cddedb6820aaabbdb8b7bece2a3df0fd1a5/src/Common/src/CoreLib/System/SpanHelpers.Byte.cs#L473-L604
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfQuoteOrAnyControlOrBaskSlash(this ReadOnlySpan<byte> span)
public static int IndexOfQuoteOrAnyControlOrBackSlash(this ReadOnlySpan<byte> span)
{
return IndexOfOrLessThan(
ref MemoryMarshal.GetReference(span),
Expand Down
10 changes: 10 additions & 0 deletions src/System.Text.Json/src/System/Text/Json/JsonReaderState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ public struct JsonReaderState
internal JsonTokenType _previousTokenType;
internal JsonReaderOptions _readerOptions;
internal BitStack _bitStack;
internal SequencePosition _sequencePosition;

/// <summary>
/// Returns the total amount of bytes consumed by the <see cref="Utf8JsonReader"/> so far
/// for the given UTF-8 encoded input text.
/// </summary>
public long BytesConsumed => _bytesConsumed;

/// <summary>
/// Returns the current <see cref="SequencePosition"/> within the provided UTF-8 encoded
/// input ReadOnlySequence&lt;byte&gt;. If the <see cref="Utf8JsonReader"/> was constructed
/// with a ReadOnlySpan&lt;byte&gt; instead, this will always return a default <see cref="SequencePosition"/>.
/// </summary>
public SequencePosition Position => _sequencePosition;

/// <summary>
/// Constructs a new <see cref="JsonReaderState"/> instance.
/// </summary>
Expand Down Expand Up @@ -68,6 +76,8 @@ public JsonReaderState(int maxDepth = DefaultMaxDepth, JsonReaderOptions options
// Only allocate if the user reads a JSON payload beyond the depth that the _allocationFreeContainer can handle.
// This way we avoid allocations in the common, default cases, and allocate lazily.
_bitStack = default;

_sequencePosition = default;
}

/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion src/System.Text.Json/src/System/Text/Json/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void ThrowJsonReaderException(ref Utf8JsonReader json, ExceptionRe
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static JsonReaderException GetJsonReaderException(ref Utf8JsonReader json, ExceptionResource resource, byte nextByte, ReadOnlySpan<byte> bytes)
public static JsonReaderException GetJsonReaderException(ref Utf8JsonReader json, ExceptionResource resource, byte nextByte, ReadOnlySpan<byte> bytes)
{
string message = GetResourceString(ref json, resource, nextByte, Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Length));

Expand Down Expand Up @@ -132,6 +132,12 @@ private static string GetResourceString(ref Utf8JsonReader json, ExceptionResour
case ExceptionResource.InvalidCharacterWithinString:
message = SR.Format(SR.InvalidCharacterWithinString, character);
break;
case ExceptionResource.InvalidCharacterAfterEscapeWithinString:
message = SR.Format(SR.InvalidCharacterAfterEscapeWithinString, character);
break;
case ExceptionResource.InvalidHexCharacterWithinString:
message = SR.Format(SR.InvalidHexCharacterWithinString, character);
break;
case ExceptionResource.EndOfCommentNotFound:
message = SR.EndOfCommentNotFound;
break;
Expand Down Expand Up @@ -168,6 +174,8 @@ internal enum ExceptionResource
ExpectedValueAfterPropertyNameNotFound,
FoundInvalidCharacter,
InvalidCharacterWithinString,
InvalidCharacterAfterEscapeWithinString,
InvalidHexCharacterWithinString,
InvalidEndOfJsonNonPrimitive,
MismatchedObjectArray,
ObjectDepthTooLarge,
Expand Down
Loading