Strict operator support added

This commit is contained in:
WillSullivan
2018-10-07 16:22:13 +13:00
committed by James Newton-King
parent 700444a996
commit 3a9dfebc2d
4 changed files with 300 additions and 3 deletions
@@ -786,6 +786,11 @@ namespace Newtonsoft.Json.Linq.JsonPath
throw new JsonException("Path ended with open query.");
}
if (Match("==="))
{
return QueryOperator.StrictEquals;
}
if (Match("=="))
{
return QueryOperator.Equals;
@@ -796,6 +801,11 @@ namespace Newtonsoft.Json.Linq.JsonPath
return QueryOperator.RegexEquals;
}
if (Match("!=="))
{
return QueryOperator.StrictNotEquals;
}
if (Match("!=") || Match("<>"))
{
return QueryOperator.NotEquals;
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@@ -24,7 +24,9 @@ namespace Newtonsoft.Json.Linq.JsonPath
GreaterThanOrEquals = 7,
And = 8,
Or = 9,
RegexEquals = 10
RegexEquals = 10,
StrictEquals = 11,
StrictNotEquals = 12
}
internal abstract class QueryExpression
@@ -121,7 +123,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
return false;
}
private bool MatchTokens(JToken leftResult, JToken rightResult)
{
if (leftResult is JValue leftValue && rightResult is JValue rightValue)
@@ -140,12 +142,24 @@ namespace Newtonsoft.Json.Linq.JsonPath
return true;
}
break;
case QueryOperator.StrictEquals:
if (EqualsWithoutStringCoercion(leftValue, rightValue))
{
return true;
}
break;
case QueryOperator.NotEquals:
if (!EqualsWithStringCoercion(leftValue, rightValue))
{
return true;
}
break;
case QueryOperator.StrictNotEquals:
if (!EqualsWithoutStringCoercion(leftValue, rightValue))
{
return true;
}
break;
case QueryOperator.GreaterThan:
if (leftValue.CompareTo(rightValue) > 0)
{
@@ -257,5 +271,66 @@ namespace Newtonsoft.Json.Linq.JsonPath
return string.Equals(currentValueString, queryValueString, StringComparison.Ordinal);
}
private bool EqualsWithoutStringCoercion(JValue value, JValue queryValue)
{
return IsStrictMatch(value, queryValue);
}
internal static bool IsStrictMatch(JToken value, JToken other)
{
// I've made this internal and static for testing purposes because I really don't get how to call the IsMatch method :/
/*
* If Type(x) is different from Type(y), return false.
* If Type(x) is Undefined, return true.
* If Type(x) is Null, return true.
* If Type(x) is Number, then
* If x is NaN, return false.
* If y is NaN, return false.
* If x is the same Number value as y, return true.
* If x is +0 and y is −0, return true.
* If x is −0 and y is +0, return true.
* Return false.
* If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
* If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
* Return true if x and y refer to the same object. Otherwise, return false.
*/
if(value == null)
throw new ArgumentNullException(nameof(value));
if (other == null)
throw new ArgumentNullException(nameof(other));
// we handle floats and integers the exact same way, so they are pseudo equivalent
if (value.Type != other.Type &&
((value.Type != JTokenType.Integer && value.Type != JTokenType.Float) ||
(other.Type != JTokenType.Integer && other.Type != JTokenType.Float))) return false;
switch (value.Type)
{
case JTokenType.Null:
case JTokenType.Undefined:
return true;
case JTokenType.Integer:
case JTokenType.Float:
return value.Value<float>() == other.Value<float>();
case JTokenType.String:
return string.Equals(value.Value<string>(), other.Value<string>(), StringComparison.Ordinal);
case JTokenType.Boolean:
return value.Value<bool>() == other.Value<bool>();
case JTokenType.Date:
return value.Value<DateTime>() == other.Value<DateTime>();
// How the heck could this happen? Ain't no guids in ecmascript.
case JTokenType.Guid:
return new Guid(value.Value<string>()) == new Guid(other.Value<string>());
case JTokenType.TimeSpan:
return new TimeSpan(value.Value<long>()) == new TimeSpan(other.Value<long>());
// unsure of Uri; it appears (from ad hoc testing) that javascript does not consider equivalent URLs to be equal no matter what
// new URL("http://lol.com") === new URL("http://lol.com") returns false always
// also, JSON.stringify renders {} for urls. JSON.stringify({lol: new URL("http://lol.com")}) renders "{"lol":{}}"
// so I don't know how we could get this kind of token type
case JTokenType.Uri:
default:
throw new InvalidOperationException($"Unexpected or unsupported JTokenType {value.Type}");
}
}
}
}