public void Test() { var books = new List <Book>(); for (int i = 0; i < 1000; ++i) { string category; switch (i % 8) { case 0: category = "Fiction"; break; case 1: category = "Poetry"; break; case 2: category = "Fantasy"; break; case 3: category = "ScienceFiction"; break; case 4: category = "Mystery"; break; case 5: category = "Biography"; break; case 6: category = "Drama"; break; default: category = "Nonfiction"; break; } books.Add(new Book($"Title{i}", $"Author{i}", category)); } string jsonString = JsonSerializer.Serialize(books); var doc = JsonDocument.Parse(jsonString); var selector = JsonSelector.Parse(@"$[[email protected]=='Fiction', [email protected]=='Poetry', [email protected]=='Fantasy', [email protected]=='ScienceFiction', [email protected]=='Mystery', [email protected]=='Biography', [email protected]=='Drama', [email protected]=='Nonfiction' ]"); IList <JsonElement> results1 = selector.Select(doc.RootElement, new JsonSelectorOptions { ExecutionMode = PathExecutionMode.Sequential }); var serializerOptions = new JsonSerializerOptions() { WriteIndented = true }; //Debug.WriteLine($"{JsonSerializer.Serialize(doc, serializerOptions)}\n"); IList <JsonElement> results2 = selector.Select(doc.RootElement, new JsonSelectorOptions { ExecutionMode = PathExecutionMode.Parallel }); System.Collections.ArrayList.Adapter((System.Collections.IList)results1).Sort(new JsonElementComparer()); System.Collections.ArrayList.Adapter((System.Collections.IList)results2).Sort(new JsonElementComparer()); //Debug.WriteLine($"{JsonSerializer.Serialize(results2, serializerOptions)}\n"); Assert.IsTrue(Enumerable.SequenceEqual(results1, results2, JsonElementEqualityComparer.Instance)); }
public void RunJsonPathTests(string path) { Debug.WriteLine($"Test {path}"); string text = System.IO.File.ReadAllText(path); var jsonOptions = new JsonDocumentOptions(); jsonOptions.CommentHandling = JsonCommentHandling.Skip; using JsonDocument doc = JsonDocument.Parse(text, jsonOptions); var testsEnumeratable = doc.RootElement.EnumerateArray(); var comparer = JsonElementEqualityComparer.Instance; foreach (var testGroup in testsEnumeratable) { JsonElement given = testGroup.GetProperty("given"); var testCases = testGroup.GetProperty("cases"); var testCasesEnumeratable = testCases.EnumerateArray(); foreach (var testCase in testCasesEnumeratable) { string comment; JsonElement commentElement; if (testCase.TryGetProperty("comment", out commentElement) && commentElement.ValueKind == JsonValueKind.String) { comment = commentElement.GetString(); } else { comment = ""; } var options = new JsonSelectorOptions(); JsonElement element; if (testCase.TryGetProperty("nodups", out element) && element.ValueKind == JsonValueKind.True) { options.NoDuplicates = true; } if (testCase.TryGetProperty("sort", out element) && element.ValueKind == JsonValueKind.True) { options.Sort = true; } var exprElement = testCase.GetProperty("expression"); try { JsonElement expected; if (testCase.TryGetProperty("error", out expected)) { Assert.ThrowsException <JsonPathParseException>(() => JsonSelector.Parse(exprElement.ToString())); } else if (testCase.TryGetProperty("result", out expected)) { var expr = JsonSelector.Parse(exprElement.ToString()); var items = expr.Select(given, options); bool success = items.Count == expected.GetArrayLength(); for (Int32 i = 0; success && i < items.Count; ++i) { if (!comparer.Equals(items[i], expected[i])) { success = false; } } if (!success) { Debug.WriteLine("File: {0}", path); Debug.WriteLine(comment); Debug.WriteLine("Document: " + given.ToString()); Debug.WriteLine("Path: " + exprElement.ToString()); if (options.NoDuplicates) { Debug.WriteLine("nodups"); } if (options.Sort) { Debug.WriteLine("sort"); } Debug.WriteLine("Expected: " + expected.ToString()); Debug.WriteLine("Results: "); foreach (var item in items) { Debug.WriteLine(item.ToString()); } } Assert.AreEqual(expected.GetArrayLength(), items.Count); for (Int32 i = 0; i < items.Count && i < expected.GetArrayLength(); ++i) { Assert.IsTrue(comparer.Equals(items[i], expected[i])); } } } catch (Exception e) { Debug.WriteLine("File: {0}", path); Debug.WriteLine(comment); Debug.WriteLine("Error: {0}", e.Message); throw e; } } } }
public static void ProcessingUnionsSequentiallyAndInParallel() { string jsonString = @" [ { ""Title"": ""Title0"", ""Author"": ""Author0"", ""Category"": ""Fiction"" }, { ""Title"": ""Title1"", ""Author"": ""Author1"", ""Category"": ""Poetry"" }, { ""Title"": ""Title2"", ""Author"": ""Author2"", ""Category"": ""NonFiction"" }, { ""Title"": ""Title3"", ""Author"": ""Author3"", ""Category"": ""Drama"" }, { ""Title"": ""Title4"", ""Author"": ""Author4"", ""Category"": ""Drama"" }, { ""Title"": ""Title5"", ""Author"": ""Author5"", ""Category"": ""Fiction"" }, { ""Title"": ""Title6"", ""Author"": ""Author6"", ""Category"": ""Poetry"" }, { ""Title"": ""Title7"", ""Author"": ""Author7"", ""Category"": ""Poetry"" }, { ""Title"": ""Title8"", ""Author"": ""Author8"", ""Category"": ""Folktale"" }, { ""Title"": ""Title9"", ""Author"": ""Author9"", ""Category"": ""Folktale"" } ] "; using JsonDocument doc = JsonDocument.Parse(jsonString); var serializerOptions = new JsonSerializerOptions() { WriteIndented = true }; var selector1 = JsonSelector.Parse(@"$[[email protected]=='Fiction' || @.Category=='NonFiction' || @.Category=='Drama'] "); IList <JsonElement> results1 = selector1.Select(doc.RootElement); Console.WriteLine("Results with sequential processing:"); Console.WriteLine(JsonSerializer.Serialize(results1, serializerOptions)); Console.WriteLine(); var selector2 = JsonSelector.Parse(@"$[[email protected]=='Fiction', [email protected]=='NonFiction', [email protected]=='Drama' ]"); IList <JsonElement> results2 = selector2.Select(doc.RootElement, new JsonSelectorOptions { ExecutionMode = PathExecutionMode.Parallel }); Console.WriteLine("Results with parallel processing:"); Console.WriteLine(JsonSerializer.Serialize(results2, serializerOptions)); Console.WriteLine(); }
public static void SelectValuesPathsAndNodes() { string jsonString = @" { ""books"": [ { ""category"": ""fiction"", ""title"" : ""A Wild Sheep Chase"", ""author"" : ""Haruki Murakami"", ""price"" : 22.72 }, { ""category"": ""fiction"", ""title"" : ""The Night Watch"", ""author"" : ""Sergei Lukyanenko"", ""price"" : 23.58 }, { ""category"": ""fiction"", ""title"" : ""The Comedians"", ""author"" : ""Graham Greene"", ""price"" : 21.99 }, { ""category"": ""memoir"", ""title"" : ""The Night Watch"", ""author"" : ""David Atlee Phillips"", ""price"" : 260.90 } ] } "; using JsonDocument doc = JsonDocument.Parse(jsonString); var serializerOptions = new JsonSerializerOptions() { WriteIndented = true }; // Selector of titles from union of all books with category 'memoir' // and all books with price > 23 var selector = JsonSelector.Parse("$.books[[email protected]=='memoir',[email protected] > 23].title"); Console.WriteLine("Select values"); IList <JsonElement> values = selector.Select(doc.RootElement); Console.WriteLine(JsonSerializer.Serialize(values, serializerOptions)); Console.WriteLine(); Console.WriteLine("Select paths"); IList <JsonLocation> paths = selector.SelectPaths(doc.RootElement); foreach (var path in paths) { Console.WriteLine(path); } Console.WriteLine(); Console.WriteLine("Select nodes"); IList <PathValuePair> nodes = selector.SelectNodes(doc.RootElement); foreach (var node in nodes) { Console.WriteLine($"{node.Path} => {JsonSerializer.Serialize(node.Value, serializerOptions)}"); } Console.WriteLine(); Console.WriteLine("Remove duplicate nodes"); IList <PathValuePair> uniqueNodes = selector.SelectNodes(doc.RootElement, new JsonSelectorOptions { NoDuplicates = true }); foreach (var node in uniqueNodes) { Console.WriteLine($"{node.Path} => {JsonSerializer.Serialize(node.Value, serializerOptions)}"); } Console.WriteLine(); }
public static void SelectNodesWithVariousOptions() { string jsonString = @" { ""books"": [ { ""category"": ""fiction"", ""title"" : ""A Wild Sheep Chase"", ""author"" : ""Haruki Murakami"", ""price"" : 22.72 }, { ""category"": ""fiction"", ""title"" : ""The Night Watch"", ""author"" : ""Sergei Lukyanenko"", ""price"" : 23.58 }, { ""category"": ""fiction"", ""title"" : ""The Comedians"", ""author"" : ""Graham Greene"", ""price"" : 21.99 }, { ""category"": ""memoir"", ""title"" : ""The Night Watch"", ""author"" : ""David Atlee Phillips"", ""price"" : 260.90 } ] } "; using JsonDocument doc = JsonDocument.Parse(jsonString); var selector = JsonSelector.Parse("$.books[3,1,1].title"); var serializerOptions = new JsonSerializerOptions() { WriteIndented = true }; Console.WriteLine("Allow duplicate nodes"); IList <PathValuePair> nodes = selector.SelectNodes(doc.RootElement); foreach (var node in nodes) { Console.WriteLine($"{node.Path} => {JsonSerializer.Serialize(node.Value, serializerOptions)}"); } Console.WriteLine(); Console.WriteLine("Allow duplicate nodes and sort by paths"); IList <PathValuePair> nodesSort = selector.SelectNodes(doc.RootElement, new JsonSelectorOptions { Sort = true }); foreach (var node in nodesSort) { Console.WriteLine($"{node.Path} => {JsonSerializer.Serialize(node.Value, serializerOptions)}"); } Console.WriteLine(); Console.WriteLine("Remove duplicate nodes"); IList <PathValuePair> nodesNoDups = selector.SelectNodes(doc.RootElement, new JsonSelectorOptions { NoDuplicates = true }); foreach (var node in nodesNoDups) { Console.WriteLine($"{node.Path} => {JsonSerializer.Serialize(node.Value, serializerOptions)}"); } Console.WriteLine(); Console.WriteLine("Remove duplicate nodes and sort by paths"); IList <PathValuePair> nodesNoDupsSort = selector.SelectNodes(doc.RootElement, new JsonSelectorOptions { NoDuplicates = true, Sort = true }); foreach (var node in nodesNoDupsSort) { Console.WriteLine($"{node.Path} => {JsonSerializer.Serialize(node.Value, serializerOptions)}"); } Console.WriteLine(); }