Add documentation and split JSON Path into its own documentation section (#1925)

This commit is contained in:
James Newton-King
2018-11-26 23:10:30 +13:00
committed by GitHub
parent f869558de5
commit 2171a88e5e
21 changed files with 377 additions and 26 deletions
@@ -0,0 +1,76 @@
#region License
// Copyright (c) 2007 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
using System;
using System.Collections.Generic;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using System.Text;
namespace Newtonsoft.Json.Tests.Documentation.Samples.JsonPath
{
[TestFixture]
public class ErrorWhenNoMatchQuery : TestFixtureBase
{
[Test]
public void Example()
{
#region Usage
JArray items = JArray.Parse(@"[
{
'Name': 'John Doe',
},
{
'Name': 'Jane Doe',
}
]");
// A true value for errorWhenNoMatch will result in an error if the queried value is missing
string result;
try
{
result = (string)items.SelectToken(@"$.[3]['Name']", errorWhenNoMatch: true);
}
catch (JsonException)
{
result = "Unable to find result in JSON.";
}
#endregion
Assert.AreEqual("Unable to find result in JSON.", result);
}
}
}
@@ -41,7 +41,7 @@ using NUnit.Framework;
#endif
namespace Newtonsoft.Json.Tests.Documentation.Samples.Linq
namespace Newtonsoft.Json.Tests.Documentation.Samples.JsonPath
{
[TestFixture]
public class QueryJsonSelectToken : TestFixtureBase
@@ -40,7 +40,7 @@ using System.Linq;
#endif
using System.Text;
namespace Newtonsoft.Json.Tests.Documentation.Samples.Linq
namespace Newtonsoft.Json.Tests.Documentation.Samples.JsonPath
{
[TestFixture]
public class QueryJsonSelectTokenEscaped : TestFixtureBase
@@ -40,7 +40,7 @@ using System.Linq;
#endif
using System.Text;
namespace Newtonsoft.Json.Tests.Documentation.Samples.Linq
namespace Newtonsoft.Json.Tests.Documentation.Samples.JsonPath
{
[TestFixture]
public class QueryJsonSelectTokenJsonPath : TestFixtureBase
@@ -41,7 +41,7 @@ using NUnit.Framework;
#endif
namespace Newtonsoft.Json.Tests.Documentation.Samples.Linq
namespace Newtonsoft.Json.Tests.Documentation.Samples.JsonPath
{
[TestFixture]
public class QueryJsonSelectTokenWithLinq : TestFixtureBase
@@ -0,0 +1,79 @@
#region License
// Copyright (c) 2007 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
using System;
using System.Collections.Generic;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using System.Text;
namespace Newtonsoft.Json.Tests.Documentation.Samples.JsonPath
{
[TestFixture]
public class RegexQuery : TestFixtureBase
{
[Test]
public void Example()
{
#region Usage
JArray packages = JArray.Parse(@"[
{
'PackageId': 'Newtonsoft.Json',
'Version': '11.0.1',
'ReleaseDate': '2018-02-17T00:00:00'
},
{
'PackageId': 'NUnit',
'Version': '3.9.0',
'ReleaseDate': '2017-11-10T00:00:00'
}
]");
// Find Newtonsoft packages
List<JToken> newtonsoftPackages = packages.SelectTokens(@"$.[?(@.PackageId =~ /^Newtonsoft\.(.*)$/)]").ToList();
foreach (JToken item in newtonsoftPackages)
{
Console.WriteLine((string) item["PackageId"]);
}
// Newtonsoft.Json
#endregion
Assert.AreEqual(1, newtonsoftPackages.Count);
Assert.AreEqual("Newtonsoft.Json", (string)newtonsoftPackages[0]["PackageId"]);
}
}
}
@@ -0,0 +1,76 @@
#region License
// Copyright (c) 2007 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
using System;
using System.Collections.Generic;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using System.Text;
namespace Newtonsoft.Json.Tests.Documentation.Samples.JsonPath
{
[TestFixture]
public class StrictEqualsQuery : TestFixtureBase
{
[Test]
public void Example()
{
#region Usage
JArray items = JArray.Parse(@"[
{
'Name': 'Valid JSON',
'Valid': true
},
{
'Name': 'Invalid JSON',
'Valid': 'true'
}
]");
// Use === operator. Compared types must be the same to be valid
List<JToken> strictResults = items.SelectTokens(@"$.[?(@.Valid === true)]").ToList();
foreach (JToken item in strictResults)
{
Console.WriteLine((string)item["Name"]);
}
// Valid JSON
#endregion
Assert.AreEqual(1, strictResults.Count);
}
}
}
@@ -0,0 +1,66 @@
#region License
// Copyright (c) 2007 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;
using System;
using System.Collections.Generic;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using System.Text;
#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif
namespace Newtonsoft.Json.Tests.Documentation.Samples.Linq
{
[TestFixture]
public class QueryJsonIgnoreCase : TestFixtureBase
{
[Test]
public void Example()
{
#region Usage
string json = @"{
'name': 'James Newton-King',
'blog': 'http://james.newtonking.com'
}";
JObject profile = JObject.Parse(json);
string name = (string)profile.GetValue("Name", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(name);
#endregion
Assert.AreEqual("James Newton-King", name);
}
}
}
+3 -1
View File
@@ -53,11 +53,12 @@ namespace Newtonsoft.Json
}
/// <summary>
/// Gets the string containing the same characters as the specified range of characters in the given array.
/// Gets a string containing the same characters as the specified range of characters in the given array.
/// </summary>
/// <param name="key">The character array containing the name to find.</param>
/// <param name="start">The zero-based index into the array specifying the first character of the name.</param>
/// <param name="length">The number of characters in the name.</param>
/// <returns>A string containing the same characters as the specified range of characters in the given array.</returns>
public override string Get(char[] key, int start, int length)
{
if (length == 0)
@@ -96,6 +97,7 @@ namespace Newtonsoft.Json
/// </summary>
/// <param name="key">The string to add.</param>
/// <remarks>This method is not thread-safe.</remarks>
/// <returns>The resolved string.</returns>
public string Add(string key)
{
if (key == null)
+2 -1
View File
@@ -6,11 +6,12 @@
public abstract class JsonNameTable
{
/// <summary>
/// Gets the string containing the same characters as the specified range of characters in the given array.
/// Gets a string containing the same characters as the specified range of characters in the given array.
/// </summary>
/// <param name="key">The character array containing the name to find.</param>
/// <param name="start">The zero-based index into the array specifying the first character of the name.</param>
/// <param name="length">The number of characters in the name.</param>
/// <returns>A string containing the same characters as the specified range of characters in the given array.</returns>
public abstract string Get(char[] key, int start, int length);
}
}