-Fixed JSON Path not equal null with non-primative values

This commit is contained in:
James Newton-King
2016-03-21 21:00:44 +13:00
parent 310769995a
commit 031222d4f4
2 changed files with 92 additions and 39 deletions
@@ -902,5 +902,43 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
Assert.AreEqual("Headlight Fluid", firstProductNames[1]);
Assert.AreEqual(149.95m, totalPrice);
}
[Test]
public void NotEqualsNullAndNonPrimativeValues()
{
string json = @"[
{
""name"": ""string"",
""value"": ""aString""
},
{
""name"": ""number"",
""value"": 123
},
{
""name"": ""array"",
""value"": [
1,
2,
3,
4
]
},
{
""name"": ""object"",
""value"": {
""1"": 1
}
}
]";
JArray a = JArray.Parse(json);
List<JToken> result = a.SelectTokens("$.[?(@.value!=null)]").ToList();
Assert.AreEqual(4, result.Count);
result = a.SelectTokens("$.[?(@.value)]").ToList();
Assert.AreEqual(4, result.Count);
}
}
}
@@ -76,48 +76,63 @@ namespace Newtonsoft.Json.Linq.JsonPath
foreach (JToken r in pathResult)
{
JValue v = r as JValue;
switch (Operator)
if (v != null)
{
case QueryOperator.Equals:
if (v != null && EqualsWithStringCoercion(v, Value))
{
switch (Operator)
{
case QueryOperator.Equals:
if (EqualsWithStringCoercion(v, Value))
{
return true;
}
break;
case QueryOperator.NotEquals:
if (!EqualsWithStringCoercion(v, Value))
{
return true;
}
break;
case QueryOperator.GreaterThan:
if (v.CompareTo(Value) > 0)
{
return true;
}
break;
case QueryOperator.GreaterThanOrEquals:
if (v.CompareTo(Value) >= 0)
{
return true;
}
break;
case QueryOperator.LessThan:
if (v.CompareTo(Value) < 0)
{
return true;
}
break;
case QueryOperator.LessThanOrEquals:
if (v.CompareTo(Value) <= 0)
{
return true;
}
break;
case QueryOperator.Exists:
return true;
}
break;
case QueryOperator.NotEquals:
if (v != null && !EqualsWithStringCoercion(v, Value))
{
}
}
else
{
switch (Operator)
{
case QueryOperator.NotEquals:
if (Value.Type == JTokenType.Null)
{
return true;
}
break;
case QueryOperator.Exists:
return true;
}
break;
case QueryOperator.GreaterThan:
if (v != null && v.CompareTo(Value) > 0)
{
return true;
}
break;
case QueryOperator.GreaterThanOrEquals:
if (v != null && v.CompareTo(Value) >= 0)
{
return true;
}
break;
case QueryOperator.LessThan:
if (v != null && v.CompareTo(Value) < 0)
{
return true;
}
break;
case QueryOperator.LessThanOrEquals:
if (v != null && v.CompareTo(Value) <= 0)
{
return true;
}
break;
case QueryOperator.Exists:
return true;
default:
throw new ArgumentOutOfRangeException();
}
}
}