Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static bool IsInSubtypeRelationshipWith(this Type type, Type other) =>
type.IsAssignableFromInternal(other) || other.IsAssignableFromInternal(type);

private static bool HasJsonConstructorAttribute(ConstructorInfo constructorInfo)
=> constructorInfo.GetCustomAttribute<JsonConstructorAttribute>() != null;
=> constructorInfo.GetCustomAttribute<JsonConstructorAttribute>() is not null;

public static bool HasRequiredMemberAttribute(this MemberInfo memberInfo)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public bool Pop()
private readonly bool PeekInArray()
{
int index = _currentDepth - AllocationFreeMaxDepth - 1;
Debug.Assert(_array != null);
Debug.Assert(_array is not null);
Debug.Assert(index >= 0, $"Get - Negative - index: {index}, arrayLength: {_array.Length}");

int elementIndex = Div32Rem(index, out int extraBits);
Expand All @@ -161,7 +161,7 @@ public readonly bool Peek()

private void DoubleArray(int minSize)
{
Debug.Assert(_array != null);
Debug.Assert(_array is not 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 @@ -54,7 +54,7 @@ internal readonly struct DbRow

internal DbRow(JsonTokenType jsonTokenType, int location, int sizeOrLength)
{
Debug.Assert(jsonTokenType > JsonTokenType.None && jsonTokenType <= JsonTokenType.Null);
Debug.Assert(jsonTokenType is > JsonTokenType.None and <= JsonTokenType.Null);
Debug.Assert((byte)jsonTokenType < 1 << 4);
Debug.Assert(location >= 0);
Debug.Assert(sizeOrLength >= UnknownSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal static MetadataDb CreateRented(int payloadLength, bool convertToAlloc)
// were more frequent anyways.
const int OneMegabyte = 1024 * 1024;

if (initialSize > OneMegabyte && initialSize <= 4 * OneMegabyte)
if (initialSize is > OneMegabyte and <= 4 * OneMegabyte)
{
initialSize = OneMegabyte;
}
Expand All @@ -151,7 +151,7 @@ internal static MetadataDb CreateLocked(int payloadLength)
public void Dispose()
{
byte[]? data = Interlocked.Exchange(ref _data, null!);
if (data == null)
if (data is null)
{
return;
}
Expand All @@ -175,7 +175,7 @@ internal void CompleteAllocations()
{
if (_convertToAlloc)
{
Debug.Assert(_data != null);
Debug.Assert(_data is not null);
byte[] returnBuf = _data;
_data = _data.AsSpan(0, Length).ToArray();
_isLocked = true;
Expand Down Expand Up @@ -217,7 +217,7 @@ internal void Append(JsonTokenType tokenType, int startLocation, int length)
{
// StartArray or StartObject should have length -1, otherwise the length should not be -1.
Debug.Assert(
(tokenType == JsonTokenType.StartArray || tokenType == JsonTokenType.StartObject) ==
(tokenType is JsonTokenType.StartArray or JsonTokenType.StartObject) ==
(length == DbRow.UnknownSize));

if (Length >= _data.Length - DbRow.Size)
Expand Down Expand Up @@ -275,7 +275,7 @@ internal void SetLength(int index, int length)
internal void SetNumberOfRows(int index, int numberOfRows)
{
AssertValidIndex(index);
Debug.Assert(numberOfRows >= 1 && numberOfRows <= 0x0FFFFFFF);
Debug.Assert(numberOfRows is >= 1 and <= 0x0FFFFFFF);

Span<byte> dataPos = _data.AsSpan(index + NumberOfRowsOffset);
int current = MemoryMarshal.Read<int>(dataPos);
Expand All @@ -299,7 +299,7 @@ internal void SetHasComplexChildren(int index)

internal int FindIndexOfFirstUnsetSizeOrLength(JsonTokenType lookupType)
{
Debug.Assert(lookupType == JsonTokenType.StartObject || lookupType == JsonTokenType.StartArray);
Debug.Assert(lookupType is JsonTokenType.StartObject or JsonTokenType.StartArray);
return FindOpenElement(lookupType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static JsonDocument Parse(Stream utf8Json, JsonDocumentOptions options =
ArgumentNullException.ThrowIfNull(utf8Json);

ArraySegment<byte> drained = ReadToEnd(utf8Json);
Debug.Assert(drained.Array != null);
Debug.Assert(drained.Array is not null);
try
{
return Parse(
Expand Down Expand Up @@ -154,10 +154,10 @@ internal static JsonDocument ParseRented(PooledByteBufferWriter utf8Json, JsonDo

internal static JsonDocument ParseValue(Stream utf8Json, JsonDocumentOptions options)
{
Debug.Assert(utf8Json != null);
Debug.Assert(utf8Json is not null);

ArraySegment<byte> drained = ReadToEnd(utf8Json);
Debug.Assert(drained.Array != null);
Debug.Assert(drained.Array is not null);

byte[] owned = new byte[drained.Count];
Buffer.BlockCopy(drained.Array, 0, owned, 0, drained.Count);
Expand Down Expand Up @@ -185,7 +185,7 @@ internal static JsonDocument ParseValue(ReadOnlySpan<byte> utf8Json, JsonDocumen

internal static JsonDocument ParseValue(string json, JsonDocumentOptions options)
{
Debug.Assert(json != null);
Debug.Assert(json is not null);
return ParseValue(json.AsSpan(), options);
}

Expand Down Expand Up @@ -221,7 +221,7 @@ private static async Task<JsonDocument> ParseAsyncCore(
CancellationToken cancellationToken = default)
{
ArraySegment<byte> drained = await ReadToEndAsync(utf8Json, cancellationToken).ConfigureAwait(false);
Debug.Assert(drained.Array != null);
Debug.Assert(drained.Array is not null);
try
{
return Parse(
Expand All @@ -245,7 +245,7 @@ internal static async Task<JsonDocument> ParseAsyncCoreUnrented(
CancellationToken cancellationToken = default)
{
ArraySegment<byte> drained = await ReadToEndAsync(utf8Json, cancellationToken).ConfigureAwait(false);
Debug.Assert(drained.Array != null);
Debug.Assert(drained.Array is not null);

byte[] owned = new byte[drained.Count];
Buffer.BlockCopy(drained.Array, 0, owned, 0, drained.Count);
Expand Down Expand Up @@ -439,7 +439,7 @@ internal static JsonDocument ParseValue(ref Utf8JsonReader reader, bool allowDup
bool ret = TryParseValue(ref reader, out JsonDocument? document, shouldThrow: true, useArrayPools: true, allowDuplicateProperties);

Debug.Assert(ret, "TryParseValue returned false with shouldThrow: true.");
Debug.Assert(document != null, "null document returned with shouldThrow: true.");
Debug.Assert(document is not null, "null document returned with shouldThrow: true.");
return document;
}

Expand Down Expand Up @@ -758,14 +758,12 @@ private static JsonDocument ParseUnrented(
{
// These tokens should already have been processed.
Debug.Assert(
tokenType != JsonTokenType.Null &&
tokenType != JsonTokenType.False &&
tokenType != JsonTokenType.True);
tokenType is not (JsonTokenType.Null or JsonTokenType.False or JsonTokenType.True));

ReadOnlySpan<byte> utf8JsonSpan = utf8Json.Span;
MetadataDb database;

if (tokenType == JsonTokenType.String || tokenType == JsonTokenType.Number)
if (tokenType is JsonTokenType.String or JsonTokenType.Number)
{
// For primitive types, we can avoid renting MetadataDb and creating StackRowStack.
database = MetadataDb.CreateLocked(utf8Json.Length);
Expand Down Expand Up @@ -859,7 +857,7 @@ private static ArraySegment<byte> ReadToEnd(Stream stream)
}
catch
{
if (rented != null)
if (rented is not null)
{
// Holds document content, clear it before returning it.
rented.AsSpan(0, written).Clear();
Expand Down Expand Up @@ -941,7 +939,7 @@ private static async
}
catch
{
if (rented != null)
if (rented is not null)
{
// Holds document content, clear it before returning it.
rented.AsSpan(0, written).Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Dispose()
_rentedBuffer = null!;
_topOfStack = 0;

if (toReturn != null)
if (toReturn is not null)
{
// The data in this rented buffer only conveys the positions and
// lengths of tokens in a document, but no content; so it does not
Expand All @@ -52,7 +52,7 @@ internal void Push(StackRow row)

internal StackRow Pop()
{
Debug.Assert(_rentedBuffer != null);
Debug.Assert(_rentedBuffer is not null);
Debug.Assert(_topOfStack <= _rentedBuffer!.Length - StackRow.Size);

StackRow row = MemoryMarshal.Read<StackRow>(_rentedBuffer.AsSpan(_topOfStack));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private unsafe bool TryGetNamedPropertyValue(
}
finally
{
if (rented != null)
if (rented is not null)
{
rented.AsSpan(0, written).Clear();
ArrayPool<byte>.Shared.Return(rented);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ private JsonDocument(

// Both rented values better be null if we're not disposable.
Debug.Assert(isDisposable ||
(extraRentedArrayPoolBytes == null && extraPooledByteBufferWriter == null));
(extraRentedArrayPoolBytes is null && extraPooledByteBufferWriter is null));

// Both rented values can't be specified.
Debug.Assert(extraRentedArrayPoolBytes == null || extraPooledByteBufferWriter == null);
Debug.Assert(extraRentedArrayPoolBytes is null || extraPooledByteBufferWriter is null);

_utf8Json = utf8Json;
_parsedData = parsedData;
Expand All @@ -69,19 +69,19 @@ public void Dispose()
_parsedData.Dispose();
_utf8Json = ReadOnlyMemory<byte>.Empty;

if (_extraRentedArrayPoolBytes != null)
if (_extraRentedArrayPoolBytes is not null)
{
byte[]? extraRentedBytes = Interlocked.Exchange<byte[]?>(ref _extraRentedArrayPoolBytes, null);

if (extraRentedBytes != null)
if (extraRentedBytes is not null)
{
// When "extra rented bytes exist" it contains the document,
// and thus needs to be cleared before being returned.
extraRentedBytes.AsSpan(0, length).Clear();
ArrayPool<byte>.Shared.Return(extraRentedBytes);
}
}
else if (_extraPooledByteBufferWriter != null)
else if (_extraPooledByteBufferWriter is not null)
{
PooledByteBufferWriter? extraBufferWriter = Interlocked.Exchange<PooledByteBufferWriter?>(ref _extraPooledByteBufferWriter, null);
extraBufferWriter?.Dispose();
Expand Down Expand Up @@ -326,7 +326,7 @@ internal unsafe bool TextEquals(int index, ReadOnlySpan<char> otherText, bool is
result = TextEquals(index, otherUtf8Text.Slice(0, written), isPropertyName, shouldUnescape: true);
}

if (otherUtf8TextArray != null)
if (otherUtf8TextArray is not null)
{
otherUtf8Text.Slice(0, written).Clear();
ArrayPool<byte>.Shared.Return(otherUtf8TextArray);
Expand Down Expand Up @@ -832,7 +832,7 @@ private ReadOnlySpan<byte> UnescapeString(in DbRow row, out ArraySegment<byte> r

private static void ClearAndReturn(ArraySegment<byte> rented)
{
if (rented.Array != null)
if (rented.Array is not null)
{
rented.AsSpan().Clear();
ArrayPool<byte>.Shared.Return(rented.Array);
Expand Down Expand Up @@ -998,7 +998,7 @@ private static void Parse(
}
else
{
Debug.Assert(tokenType >= JsonTokenType.String && tokenType <= JsonTokenType.Null);
Debug.Assert(tokenType is >= JsonTokenType.String and <= JsonTokenType.Null);
numberOfRowsForValues++;
numberOfRowsForMembers++;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static JsonElement ParseValue(ref Utf8JsonReader reader)
bool ret = JsonDocument.TryParseValue(ref reader, out JsonDocument? document, shouldThrow: true, useArrayPools: false);

Debug.Assert(ret, "TryParseValue returned false with shouldThrow: true.");
Debug.Assert(document != null, "null document returned with shouldThrow: true.");
Debug.Assert(document is not null, "null document returned with shouldThrow: true.");
return document.RootElement;
}

Expand All @@ -63,7 +63,7 @@ internal static JsonElement ParseValue(ref Utf8JsonReader reader, bool allowDupl
allowDuplicateProperties: allowDuplicateProperties);

Debug.Assert(ret, "TryParseValue returned false with shouldThrow: true.");
Debug.Assert(document != null, "null document returned with shouldThrow: true.");
Debug.Assert(document is not null, "null document returned with shouldThrow: true.");
return document.RootElement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ public bool ValueEquals(string? text)

if (TokenType == JsonTokenType.Null)
{
return text == null;
return text is null;
}

return TextEqualsHelper(text.AsSpan(), isPropertyName: false);
Expand Down Expand Up @@ -1661,7 +1661,7 @@ public override string ToString()
case JsonTokenType.StartObject:
{
// null parent should have hit the None case
Debug.Assert(_parent != null);
Debug.Assert(_parent is not null);
return _parent.GetRawValueAsString(_idx);
}
case JsonTokenType.String:
Expand Down Expand Up @@ -1704,7 +1704,7 @@ public JsonElement Clone()

private void CheckValidInstance()
{
if (_parent == null)
if (_parent is null)
{
throw new InvalidOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace System.Text.Json

private JsonEncodedText(byte[] utf8Value)
{
Debug.Assert(utf8Value != null);
Debug.Assert(utf8Value is not null);

_value = JsonReaderHelper.GetTextFromUtf8(utf8Value);
_utf8Value = utf8Value;
Expand Down Expand Up @@ -143,9 +143,9 @@ private static JsonEncodedText EncodeHelper(ReadOnlySpan<byte> utf8Value, JavaSc
/// </remarks>
public bool Equals(JsonEncodedText other)
{
if (_value == null)
if (_value is null)
{
return other._value == null;
return other._value is null;
}
else
{
Expand Down Expand Up @@ -187,6 +187,6 @@ public override string ToString()
/// Returns 0 on a default instance of <see cref="JsonEncodedText"/>.
/// </remarks>
public override int GetHashCode()
=> _value == null ? 0 : _value.GetHashCode();
=> _value is null ? 0 : _value.GetHashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static unsafe byte[] EscapeValue(

byte[] escapedString = escapedValue.Slice(0, written).ToArray();

if (valueArray != null)
if (valueArray is not null)
{
ArrayPool<byte>.Shared.Return(valueArray);
}
Expand Down Expand Up @@ -73,7 +73,7 @@ private static unsafe byte[] GetEscapedPropertyNameSection(

byte[] propertySection = GetPropertyNameSection(escapedValue.Slice(0, written));

if (valueArray != null)
if (valueArray is not null)
{
ArrayPool<byte>.Shared.Return(valueArray);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static unsafe bool TryLookupUtf8Key<TValue>(

bool success = spanLookup.TryGetValue(decodedKey, out result);

if (rentedBuffer != null)
if (rentedBuffer is not null)
{
decodedKey.Clear();
ArrayPool<char>.Shared.Return(rentedBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ internal override unsafe void GetPath(ref ValueStringBuilder path, JsonNode? chi
{
Parent?.GetPath(ref path, this);

if (child != null)
if (child is not null)
{
int index = List.IndexOf(child);
Debug.Assert(index >= 0);
Expand Down Expand Up @@ -380,7 +380,7 @@ public string Display
{
get
{
if (Value == null)
if (Value is null)
{
return $"null";
}
Expand Down
Loading
Loading