Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotate System.Text.Json for nullable #528

Merged
merged 28 commits into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a649816
Annotate System.Text.Json for nullable
buyaa-n Nov 18, 2019
5015584
Suppressing null warnings on non NetCoreApp targets
buyaa-n Nov 19, 2019
44d82fd
Suppress more error in other env
buyaa-n Nov 19, 2019
3a7254b
Annotate netstandard dependent file
buyaa-n Nov 19, 2019
6917d64
Some more update
buyaa-n Nov 19, 2019
150691f
Enebling nullability on file which fialing for other projects
buyaa-n Nov 20, 2019
0ab2dd6
Disabling nullable warning on netfx, netstandard
buyaa-n Nov 20, 2019
fba9feb
Addressing feedback
buyaa-n Nov 21, 2019
4ba6fe1
Applying more feedback
buyaa-n Dec 5, 2019
7391b89
Updating ref assemblies
buyaa-n Dec 5, 2019
474efe4
Empty path instead of banging on null
buyaa-n Dec 6, 2019
c576bf9
Fix test, JsonException Path not nullable anymore
buyaa-n Dec 6, 2019
f37abb5
More review update
buyaa-n Dec 7, 2019
de20ef4
Addressing more feedback
buyaa-n Dec 10, 2019
ee302fb
Applying more feedback
buyaa-n Dec 10, 2019
cf61bab
Applying feedback, removing IEquatable workaround
buyaa-n Dec 13, 2019
5118790
More feedback
buyaa-n Dec 14, 2019
af8efe8
Options not null for Converter.Read/Write
buyaa-n Dec 16, 2019
e30fc73
One more converter
buyaa-n Dec 16, 2019
36d65a2
Merging conflict
buyaa-n Dec 16, 2019
d315c66
Merge branch 'master' of https://github.com/dotnet/runtime into json_…
buyaa-n Dec 16, 2019
599626a
Addressing more feedback
buyaa-n Dec 17, 2019
b5fc1ed
More feedback
buyaa-n Dec 17, 2019
dc33a6b
Addressing review comment
buyaa-n Dec 17, 2019
7bd8f78
Addressing feedback
buyaa-n Dec 27, 2019
ba97941
Addressing feedback
buyaa-n Dec 28, 2019
462b755
Merging conflict
buyaa-n Dec 28, 2019
e10f732
Update src/libraries/System.Text.Json/src/System/Text/Json/Serializat…
buyaa-n Dec 31, 2019
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
169 changes: 87 additions & 82 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;$(NetCoreAppCurrent)-Debug;$(NetCoreAppCurrent)-Release;$(NetFrameworkCurrent)-Debug;$(NetFrameworkCurrent)-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,14 +3,15 @@
// 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)
{
Expand All @@ -22,7 +23,7 @@ public static bool TryPeek<T>(this Stack<T> stack, out T result)
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)
{
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 @@ -118,7 +119,7 @@ public static JsonDocument Parse(Stream utf8Json, JsonDocumentOptions options =
}

ArraySegment<byte> drained = ReadToEnd(utf8Json);

Debug.Assert(drained.Array != null);
try
{
return Parse(drained.AsMemory(), options.GetReaderOptions(), drained.Array);
Expand Down Expand Up @@ -167,7 +168,7 @@ private static async Task<JsonDocument> ParseAsyncCore(
CancellationToken cancellationToken = default)
{
ArraySegment<byte> drained = await ReadToEndAsync(utf8Json, cancellationToken).ConfigureAwait(false);

Debug.Assert(drained.Array != null);
try
{
return Parse(drained.AsMemory(), options.GetReaderOptions(), drained.Array);
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 @@ -24,7 +24,7 @@ internal StackRowStack(int initialSize)
public void Dispose()
{
byte[] toReturn = _rentedBuffer;
_rentedBuffer = null;
_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,14 +244,15 @@ 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)
{
Debug.Assert(lastString != null);
return lastString;
}

Expand Down Expand Up @@ -278,6 +280,7 @@ internal string GetString(int index, JsonTokenType expectedType)
lastString = JsonReaderHelper.TranscodeHelper(segment);
}

Debug.Assert(lastString != null);
_lastIndexAndString = (index, lastString);
return lastString;
}
Expand All @@ -288,14 +291,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 +374,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 @@ -24,6 +24,7 @@ internal ArrayEnumerator(JsonElement target)
{
_target = target;
_curIdx = -1;
Debug.Assert(target._parent != null);

if (target._parent is JsonDocument document)
{
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,13 +19,14 @@ 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)
{
_target = target;
_curIdx = -1;
_current = null;
Debug.Assert(target._parent != null);

if (target._parent is JsonDocument document)
{
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Text.Json
{
Expand Down Expand Up @@ -62,6 +63,7 @@ public JsonValueKind ValueKind
return TokenType.ToValueKind();
}

Debug.Assert(_parent != null);
var jsonNode = (JsonNode)_parent;

return jsonNode.ValueKind;
Expand Down Expand Up @@ -328,7 +330,7 @@ public bool TryGetProperty(ReadOnlySpan<char> propertyName, out JsonElement valu

if (jsonNode is JsonObject jsonObject)
{
if (jsonObject.TryGetPropertyValue(propertyName.ToString(), out JsonNode nodeValue))
if (jsonObject.TryGetPropertyValue(propertyName.ToString(), out JsonNode? nodeValue))
{
value = nodeValue.AsJsonElement();
return true;
Expand Down Expand Up @@ -383,7 +385,7 @@ public bool TryGetProperty(ReadOnlySpan<byte> utf8PropertyName, out JsonElement

if (jsonNode is JsonObject jsonObject)
{
if (jsonObject.TryGetPropertyValue(JsonHelpers.Utf8GetString(utf8PropertyName), out JsonNode nodeValue))
if (jsonObject.TryGetPropertyValue(JsonHelpers.Utf8GetString(utf8PropertyName), out JsonNode? nodeValue))
{
value = nodeValue.AsJsonElement();
return true;
Expand Down Expand Up @@ -448,7 +450,7 @@ public bool GetBoolean()
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
/// <seealso cref="ToString"/>
public string GetString()
public string? GetString()
{
CheckValidInstance();

Expand Down Expand Up @@ -484,7 +486,7 @@ public string GetString()
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
public bool TryGetBytesFromBase64(out byte[] value)
public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value)
{
CheckValidInstance();

Expand Down Expand Up @@ -522,7 +524,7 @@ public bool TryGetBytesFromBase64(out byte[] value)
/// <seealso cref="ToString"/>
public byte[] GetBytesFromBase64()
{
if (TryGetBytesFromBase64(out byte[] value))
if (TryGetBytesFromBase64(out byte[]? value))
{
return value;
}
Expand Down Expand Up @@ -1492,7 +1494,7 @@ internal string GetPropertyRawText()
/// This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and
/// the result of calling <see cref="GetString" />, but avoids creating the string instance.
/// </remarks>
public bool ValueEquals(string text)
public bool ValueEquals(string? text)
{
// CheckValidInstance is done in the helper

Expand Down Expand Up @@ -1720,7 +1722,7 @@ public ObjectEnumerator EnumerateObject()
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
public override string ToString()
public override string? ToString()
{
if (_parent is JsonNode jsonNode)
{
Expand Down
Loading