Skip to content
Closed
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
191 changes: 103 additions & 88 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configurations>net461-Debug;net461-Release;netcoreapp-Debug;netcoreapp-Release;netfx-Debug;netfx-Release;netstandard2.0-Debug;netstandard2.0-Release</Configurations>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Text.Json.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<!-- https://github.com/dotnet/arcade/blob/ea6addfdc65e5df1b2c036f11614a5f922e36267/src/Microsoft.DotNet.Arcade.Sdk/tools/ProjectDefaults.props#L90 -->
<!-- For this project, we want warnings if there are public APIs/types without properly formatted XML comments (particularly CS1591). -->
<NoWarn />
<NoWarn Condition="'$(TargetsNetCoreApp)' != 'true'">$(NoWarn);nullable</NoWarn>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Text\Json\BitStack.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System.Collections.Generic
{
/// <summary>Polyfills for <see cref="Stack{T}"/>.</summary>
internal static class StackExtensions
{
public static bool TryPeek<T>(this Stack<T> stack, out T result)
public static bool TryPeek<T>(this Stack<T> stack, [MaybeNullWhen(false)] out T result)
{
if (stack.Count > 0)
{
result = stack.Peek();
return true;
}

result = default;
result = default!;
return false;
}

public static bool TryPop<T>(this Stack<T> stack, out T result)
public static bool TryPop<T>(this Stack<T> stack, [MaybeNullWhen(false)] out T result)
{
if (stack.Count > 0)
{
result = stack.Pop();
return true;
}

result = default;
result = default!;
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;

#nullable enable
namespace System.Text.Json
{
internal struct BitStack
Expand All @@ -15,7 +16,7 @@ internal struct BitStack

private const int DefaultInitialArraySize = 2;

private int[] _array;
private int[]? _array;

// This ulong container represents a tiny stack to track the state during nested transitions.
// The first bit represents the state of the current depth (1 == object, 0 == array).
Expand Down Expand Up @@ -135,6 +136,7 @@ private bool PopFromArray()

private void DoubleArray(int minSize)
{
Debug.Assert(_array != null);
Debug.Assert(_array.Length < int.MaxValue / 2, $"Array too large - arrayLength: {_array.Length}");
Debug.Assert(minSize >= 0 && minSize >= _array.Length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ internal MetadataDb(MetadataDb source, bool useArrayPools)

public void Dispose()
{
byte[] data = Interlocked.Exchange(ref _data, null);
byte[]? data = Interlocked.Exchange(ref _data, null!);
if (data == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Buffers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -127,7 +128,7 @@ public static JsonDocument Parse(Stream utf8Json, JsonDocumentOptions options =
{
// Holds document content, clear it before returning it.
drained.AsSpan().Clear();
ArrayPool<byte>.Shared.Return(drained.Array);
ArrayPool<byte>.Shared.Return(drained.Array!);
throw;
}
}
Expand Down Expand Up @@ -176,7 +177,7 @@ private static async Task<JsonDocument> ParseAsyncCore(
{
// Holds document content, clear it before returning it.
drained.AsSpan().Clear();
ArrayPool<byte>.Shared.Return(drained.Array);
ArrayPool<byte>.Shared.Return(drained.Array!);
throw;
}
}
Expand Down Expand Up @@ -284,7 +285,7 @@ public static JsonDocument Parse(string json, JsonDocumentOptions options = defa
/// <exception cref="JsonException">
/// A value could not be read from the reader.
/// </exception>
public static bool TryParseValue(ref Utf8JsonReader reader, out JsonDocument document)
public static bool TryParseValue(ref Utf8JsonReader reader, [NotNullWhen(true)] out JsonDocument? document)
{
return TryParseValue(ref reader, out document, shouldThrow: false);
}
Expand Down Expand Up @@ -326,12 +327,12 @@ public static bool TryParseValue(ref Utf8JsonReader reader, out JsonDocument doc
/// </exception>
public static JsonDocument ParseValue(ref Utf8JsonReader reader)
{
bool ret = TryParseValue(ref reader, out JsonDocument document, shouldThrow: true);
bool ret = TryParseValue(ref reader, out JsonDocument? document, shouldThrow: true);
Debug.Assert(ret, "TryParseValue returned false with shouldThrow: true.");
return document;
return document!;
}

private static bool TryParseValue(ref Utf8JsonReader reader, out JsonDocument document, bool shouldThrow)
private static bool TryParseValue(ref Utf8JsonReader reader, [NotNullWhen(true)] out JsonDocument? document, bool shouldThrow)
{
JsonReaderState state = reader.CurrentState;
CheckSupportedOptions(state.Options, nameof(reader));
Expand Down Expand Up @@ -551,7 +552,7 @@ private static bool TryParseValue(ref Utf8JsonReader reader, out JsonDocument do
private static JsonDocument Parse(
ReadOnlyMemory<byte> utf8Json,
JsonReaderOptions readerOptions,
byte[] extraRentedBytes)
byte[]? extraRentedBytes)
{
ReadOnlySpan<byte> utf8JsonSpan = utf8Json.Span;
var database = new MetadataDb(utf8Json.Length);
Expand All @@ -577,7 +578,7 @@ private static JsonDocument Parse(
private static ArraySegment<byte> ReadToEnd(Stream stream)
{
int written = 0;
byte[] rented = null;
byte[]? rented = null;

ReadOnlySpan<byte> utf8Bom = JsonConstants.Utf8Bom;

Expand Down Expand Up @@ -659,7 +660,7 @@ private static async
CancellationToken cancellationToken)
{
int written = 0;
byte[] rented = null;
byte[]? rented = null;

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ internal StackRowStack(int initialSize)

public void Dispose()
{
byte[] toReturn = _rentedBuffer;
_rentedBuffer = null;
byte[]? toReturn = _rentedBuffer;
_rentedBuffer = null!;
_topOfStack = 0;

if (toReturn != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private bool TryGetNamedPropertyValue(
{
int remaining = currentPropertyName.Length - idx;
int written = 0;
byte[] rented = null;
byte[]? rented = null;

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Diagnostics.CodeAnalysis;

namespace System.Text.Json
{
Expand All @@ -25,8 +26,8 @@ public sealed partial class JsonDocument : IDisposable
{
private ReadOnlyMemory<byte> _utf8Json;
private MetadataDb _parsedData;
private byte[] _extraRentedBytes;
private (int, string) _lastIndexAndString = (-1, null);
private byte[]? _extraRentedBytes;
private (int, string?) _lastIndexAndString = (-1, null);

internal bool IsDisposable { get; }

Expand All @@ -38,7 +39,7 @@ public sealed partial class JsonDocument : IDisposable
private JsonDocument(
ReadOnlyMemory<byte> utf8Json,
MetadataDb parsedData,
byte[] extraRentedBytes,
byte[]? extraRentedBytes,
bool isDisposable = true)
{
Debug.Assert(!utf8Json.IsEmpty);
Expand Down Expand Up @@ -67,7 +68,7 @@ public void Dispose()

// When "extra rented bytes exist" they contain the document,
// and thus need to be cleared before being returned.
byte[] extraRentedBytes = Interlocked.Exchange(ref _extraRentedBytes, null);
byte[]? extraRentedBytes = Interlocked.Exchange(ref _extraRentedBytes, null);

if (extraRentedBytes != null)
{
Expand Down Expand Up @@ -243,11 +244,11 @@ private ReadOnlyMemory<byte> GetPropertyRawValue(int valueIndex)
return _utf8Json.Slice(start, end - start);
}

internal string GetString(int index, JsonTokenType expectedType)
internal string? GetString(int index, JsonTokenType expectedType)
{
CheckNotDisposed();

(int lastIdx, string lastString) = _lastIndexAndString;
(int lastIdx, string? lastString) = _lastIndexAndString;

if (lastIdx == index)
{
Expand Down Expand Up @@ -288,14 +289,14 @@ internal bool TextEquals(int index, ReadOnlySpan<char> otherText, bool isPropert

int matchIndex = isPropertyName ? index - DbRow.Size : index;

(int lastIdx, string lastString) = _lastIndexAndString;
(int lastIdx, string? lastString) = _lastIndexAndString;

if (lastIdx == matchIndex)
{
return otherText.SequenceEqual(lastString.AsSpan());
}

byte[] otherUtf8TextArray = null;
byte[]? otherUtf8TextArray = null;

int length = checked(otherText.Length * JsonConstants.MaxExpansionFactorWhileTranscoding);
Span<byte> otherUtf8Text = length <= JsonConstants.StackallocThreshold ?
Expand Down Expand Up @@ -371,10 +372,10 @@ internal bool TextEquals(int index, ReadOnlySpan<byte> otherUtf8Text, bool isPro
internal string GetNameOfPropertyValue(int index)
{
// The property name is one row before the property value
return GetString(index - DbRow.Size, JsonTokenType.PropertyName);
return GetString(index - DbRow.Size, JsonTokenType.PropertyName)!;
}

internal bool TryGetValue(int index, out byte[] value)
internal bool TryGetValue(int index, [NotNullWhen(true)] out byte[]? value)
{
CheckNotDisposed();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal ArrayEnumerator(JsonElement target)
}
else
{
Debug.Assert(target._parent != null);
var jsonArray = (JsonArray)target._parent;
_endIdxOrVersion = jsonArray._version;
}
Expand All @@ -58,7 +59,7 @@ public JsonElement Current
return jsonArray[_curIdx].AsJsonElement();
}

var document = (JsonDocument)_target._parent;
var document = (JsonDocument?)_target._parent;
return new JsonElement(document, _curIdx);
}
}
Expand Down Expand Up @@ -128,6 +129,7 @@ public bool MoveNext()
}
else
{
Debug.Assert(_target._parent != null);
var document = (JsonDocument)_target._parent;
_curIdx = document.GetEndIndex(_curIdx, includeEndElement: true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct ObjectEnumerator : IEnumerable<JsonProperty>, IEnumerator<JsonProp
private readonly JsonElement _target;
private int _curIdx;
private readonly int _endIdxOrVersion;
private JsonObjectProperty _current;
private JsonObjectProperty? _current;

internal ObjectEnumerator(JsonElement target)
{
Expand All @@ -34,6 +34,7 @@ internal ObjectEnumerator(JsonElement target)
}
else
{
Debug.Assert(target._parent != null);
var jsonObject = (JsonObject)target._parent;
_endIdxOrVersion = jsonObject._version;
}
Expand All @@ -59,7 +60,7 @@ public JsonProperty Current
return default;
}

var document = (JsonDocument)_target._parent;
var document = (JsonDocument?)_target._parent;
return new JsonProperty(new JsonElement(document, _curIdx));
}
}
Expand Down Expand Up @@ -143,6 +144,7 @@ public bool MoveNext()
}
else
{
Debug.Assert(_target._parent != null);
var document = (JsonDocument)_target._parent;
_curIdx = document.GetEndIndex(_curIdx, includeEndElement: true);
}
Expand Down
Loading