Skip to content
Draft
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
51 changes: 51 additions & 0 deletions sdk/core/Azure.Core.Experimental/src/JsonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,57 @@ internal string ToJsonString()
/// <returns>False if the given JsonData represents the given string, and false otherwise</returns>
public static bool operator !=(string? left, JsonData? right) => !(left == right);

/// <summary>
/// Returns true if a <see cref="JsonData"/> has the same value as a given string,
/// and false otherwise.
/// </summary>
/// <param name="left">The <see cref="JsonData"/> to compare.</param>
/// <param name="right">The <see cref="int"/> to compare.</param>
/// <returns>True if the given JsonData represents the given string, and false otherwise.</returns>
public static bool operator ==(JsonData? left, int right)
{
if (left == null || left._value == null)
{
return false;
}

return left.Kind == JsonValueKind.Number && ((int)left._value) == right;
}

/// <summary>
/// Returns false if a <see cref="JsonData"/> has the same value as a given string,
/// and true otherwise.
/// </summary>
/// <param name="left">The <see cref="JsonData"/> to compare.</param>
/// <param name="right">The <see cref="string"/> to compare.</param>
/// <returns>False if the given JsonData represents the given string, and false otherwise</returns>
public static bool operator !=(JsonData? left, int right) => !(left == right);
/// <summary>
/// Returns true if a <see cref="JsonData"/> has the same value as a given string,
/// and false otherwise.
/// </summary>
/// <param name="left">The <see cref="string"/> to compare.</param>
/// <param name="right">The <see cref="JsonData"/> to compare.</param>
/// <returns>True if the given JsonData represents the given string, and false otherwise.</returns>
public static bool operator ==(int left, JsonData? right)
{
if (right == null || right._value == null)
{
return false;
}

return right.Kind == JsonValueKind.Number && ((int)right._value) == left;
}

/// <summary>
/// Returns false if a <see cref="JsonData"/> has the same value as a given string,
/// and true otherwise.
/// </summary>
/// <param name="left">The <see cref="string"/> to compare.</param>
/// <param name="right">The <see cref="JsonData"/> to compare.</param>
/// <returns>False if the given JsonData represents the given string, and false otherwise</returns>
public static bool operator !=(int left, JsonData? right) => !(left == right);

/// <summary>
/// The <see cref="JsonValueKind"/> of the value of this instance.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ public void CanGetDynamicFromBinaryData()
}
}

[Test]
public void CanTestIntEqualityWithEqualsOperator()
{
dynamic value = new BinaryData(new { foo = 2 }).ToDynamic();

Assert.IsTrue(value.foo == 2);
Assert.IsTrue(2 == value.foo);
}

[Test]
public void EqualsHandlesStringsSpecial()
{
Expand Down