From 031222d4f4bd541e66f236329591f2427f835174 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 21 Mar 2016 21:00:44 +1300 Subject: [PATCH] -Fixed JSON Path not equal null with non-primative values --- .../Linq/JsonPath/JPathExecuteTests.cs | 38 ++++++++ .../Linq/JsonPath/QueryExpression.cs | 93 +++++++++++-------- 2 files changed, 92 insertions(+), 39 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathExecuteTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathExecuteTests.cs index 49f473d0..bd2ad725 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathExecuteTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathExecuteTests.cs @@ -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 result = a.SelectTokens("$.[?(@.value!=null)]").ToList(); + Assert.AreEqual(4, result.Count); + + result = a.SelectTokens("$.[?(@.value)]").ToList(); + Assert.AreEqual(4, result.Count); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JsonPath/QueryExpression.cs b/Src/Newtonsoft.Json/Linq/JsonPath/QueryExpression.cs index 3b5c44e4..4e4cfbfb 100644 --- a/Src/Newtonsoft.Json/Linq/JsonPath/QueryExpression.cs +++ b/Src/Newtonsoft.Json/Linq/JsonPath/QueryExpression.cs @@ -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(); + } } }