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

JsonNull return false when comparing against null #842

Merged
merged 4 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -43,23 +43,34 @@ public JsonNull() { }
/// </summary>
/// <param name="other">The JSON null to compare against.</param>
/// <returns><see langword="true"/></returns>
henrikse55 marked this conversation as resolved.
Show resolved Hide resolved
public bool Equals(JsonNull other) => true;
public bool Equals(JsonNull other) => !(other is null);
henrikse55 marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Compares values of two JSON nulls.
/// </summary>
/// <param name="left">The JSON null to compare.</param>
/// <param name="right">The JSON null to compare.</param>
/// <returns><see langword="true"/></returns>
public static bool operator ==(JsonNull left, JsonNull right) => true;
public static bool operator ==(JsonNull left, JsonNull right)
{
// Test "right" first to allow branch elimination when inlined for null checks (== null)
// so it can become a simple test
if (right is null)
{
// return true/false not the test result https://github.com/dotnet/coreclr/issues/914
return (left is null) ? true : false;
}

return right.Equals(left);
}

/// <summary>
/// Compares values of two JSON nulls.
/// </summary>
/// <param name="left">The JSON null to compare.</param>
/// <param name="right">The JSON null to compare.</param>
/// <returns><see langword="false"/></returns>
public static bool operator !=(JsonNull left, JsonNull right) => false;
public static bool operator !=(JsonNull left, JsonNull right) => !(left == right);

/// <summary>
/// Creates a new JSON null that is a copy of the current instance.
Expand Down
20 changes: 10 additions & 10 deletions src/libraries/System.Text.Json/tests/JsonNullTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public static void TestEquals()
Assert.False(jsonNull1.Equals(new Exception()));

// Null comparisons behave this way because of implicit conversion from null to JsonNull:
henrikse55 marked this conversation as resolved.
Show resolved Hide resolved
Assert.True(jsonNull1.Equals(null));
Assert.True(jsonNull1 == null);
Assert.False(jsonNull1 != null);

Assert.False(jsonNull1.Equals(null));
Assert.False(jsonNull1 == null);
Assert.True(jsonNull1 != null);

JsonNull jsonNullNull = null;

Assert.True(jsonNull1.Equals(jsonNullNull));
Assert.True(jsonNull1 == jsonNullNull);
Assert.False(jsonNull1 != jsonNullNull);
Assert.False(jsonNull1.Equals(jsonNullNull));
Assert.False(jsonNull1 == jsonNullNull);
Assert.True(jsonNull1 != jsonNullNull);

JsonNull otherJsonNullNull = null;
Assert.True(jsonNullNull == otherJsonNullNull);
Expand All @@ -64,7 +64,7 @@ public static void TestEquals()

JsonNode jsonNodeNull = null;
Assert.False(jsonNull1.Equals(jsonNodeNull));

JsonArray jsonArrayNull = null;
Assert.False(jsonNull1.Equals(jsonArrayNull));
}
Expand All @@ -75,10 +75,10 @@ public static void TestGetHashCode()
var jsonNull = new JsonNull();

Assert.Equal(jsonNull.GetHashCode(), new JsonNull().GetHashCode());

JsonNode jsonNodeNull = new JsonNull();
Assert.Equal(jsonNull.GetHashCode(), jsonNodeNull.GetHashCode());

IEquatable<JsonNull> jsonNullIEquatable = new JsonNull();
Assert.Equal(jsonNull.GetHashCode(), jsonNullIEquatable.GetHashCode());

Expand Down