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

Added array length support #2689

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
115 changes: 115 additions & 0 deletions Src/Newtonsoft.Json.Tests/Issues/Issue2688.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#region License
// Copyright (c) 2022 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using Newtonsoft.Json.Linq;

#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif

namespace Newtonsoft.Json.Tests.Issues
{
[TestFixture]
public class Issue2688
{
[Test]
public void ArrayLength()
{
string json = @"{
""firstName"": ""John"",
""lastName"": ""doe"",
""age"": 26,
""address"": {
""streetAddress"": ""naist street"",
""city"": ""Nara"",
""postalCode"": ""630-0192""
},
""phoneNumbers"": [
{
""type"": ""iPhone"",
""number"": ""0123-4567-8888""
},
{
""type"": ""home"",
""number"": ""0123-4567-8910""
}
]
}";
JObject jobj = JObject.Parse(json);
string jsonPath = "$.phoneNumbers.length";
JToken token = jobj.SelectToken(jsonPath);
string count = token.ToString();
string expected = "2";
Assert.AreEqual(expected, count);
}

[Test]
public void ArrayLengthInBooleanExpression()
{
string json = @"{
""store"" : {
""book"" : [{
""category"" : ""reference"",
""author"" : ""Nigel Rees"",
""title"" : ""Sayings of the Century"",
""price"" : 8.68
}, {
""category"" : ""fiction"",
""author"" : ""Evelyn Waugh"",
""title"" : ""Sword of Honour"",
""price"" : 12.99
}, {
""category"" : ""fiction"",
""author"" : ""Herman Melville"",
""title"" : ""Moby Dick"",
""isbn"" : ""0-553-21311-3"",
""price"" : 2.56
}, {
""category"" : ""fiction"",
""author"" : ""J. R. R. Tolkien"",
""title"" : ""The Lord of the Rings"",
""isbn"" : ""0-395-19395-8"",
""price"" : 22.99
}
],
""bicycle"" : {
""color"" : ""red"",
""price"" : 19.95
}
}
}";
string jsonPath = "$.store.book[?(@.price < $.store.book.length)].author";
JObject jobj = JObject.Parse(json);
JToken token = jobj.SelectToken(jsonPath);
string actual = token.ToString();
string expected = "Herman Melville";
Assert.AreEqual(expected, actual);
}
}
}
1 change: 1 addition & 0 deletions Src/Newtonsoft.Json/Linq/JArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace Newtonsoft.Json.Linq
public partial class JArray : JContainer, IList<JToken>
{
private readonly List<JToken> _values = new List<JToken>();
static internal readonly string LengthProperty = "length";

/// <summary>
/// Gets the container's children tokens.
Expand Down
4 changes: 4 additions & 0 deletions Src/Newtonsoft.Json/Linq/JsonPath/FieldFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public override IEnumerable<JToken> ExecuteFilter(JToken root, IEnumerable<JToke
}
}
}
else if (Name == JArray.LengthProperty && t is JArray arr)
{
yield return arr.Count;
}
else
{
if (settings?.ErrorWhenNoMatch ?? false)
Expand Down