Example #1
0
    public static void UsingTheParentOperator()
    {
        string jsonString = @"
[
    {
      ""title"": ""A Wild Sheep Chase"",
      ""reviews"": [{""rating"": 4, ""reviewer"": ""Nan""}]
    },
    {
      ""title"": ""The Night Watch"",
      ""reviews"": [{""rating"": 5, ""reviewer"": ""Alan""},
                  {""rating"": 3,""reviewer"": ""Anne""}]
    },
    {
      ""title"": ""The Comedians"",
      ""reviews"": [{""rating"": 4, ""reviewer"": ""Lisa""},
                  {""rating"": 5, ""reviewer"": ""Robert""}]
    }
]
        ";

        using JsonDocument doc = JsonDocument.Parse(jsonString);

        var serializerOptions = new JsonSerializerOptions()
        {
            WriteIndented = true
        };

        Console.WriteLine("Retrieve selected nodes");
        IList <JsonElement> results = JsonSelector.Select(doc.RootElement, "$[*].reviews[?(@.rating == 5)]");

        Console.WriteLine(JsonSerializer.Serialize(results, serializerOptions));
        Console.WriteLine();

        Console.WriteLine("Retrieve parents of selected nodes");
        IList <JsonElement> results1 = JsonSelector.Select(doc.RootElement, "$[*].reviews[?(@.rating == 5)]^");

        Console.WriteLine(JsonSerializer.Serialize(results1, serializerOptions));
        Console.WriteLine();

        Console.WriteLine("Retrieve grandparents of selected nodes");
        IList <JsonElement> results2 = JsonSelector.Select(doc.RootElement, "$[*].reviews[?(@.rating == 5)]^^");

        Console.WriteLine(JsonSerializer.Serialize(results2, serializerOptions));
        Console.WriteLine();
    }
Example #2
0
    public static void UnionOfSeparateJsonPathExpressions()
    {
        string jsonString = @"
{
  ""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""
    }
  ]
}    
        ";

        using JsonDocument doc = JsonDocument.Parse(jsonString);

        var serializerOptions = new JsonSerializerOptions()
        {
            WriteIndented = true
        };

        Console.WriteLine("Union of separate JSONPath expressions");
        IList <JsonElement> results1 = JsonSelector.Select(doc.RootElement, @"$..[@.firstName,@.address.city]");

        Console.WriteLine(JsonSerializer.Serialize(results1, serializerOptions));
        Console.WriteLine();
    }
Example #3
0
    public static void KeyOfInterest()
    {
        string jsonString = @"
{
    ""Data"":[
        {
            ""KeyOfInterest"":true,
            ""AnotherKey"":true
        },
        {
            ""KeyOfInterest"":false,
            ""AnotherKey"":true
        },
        {
            ""KeyOfInterest"":true,
            ""AnotherKey"":true
        }
    ]
}
        ";

        using JsonDocument doc = JsonDocument.Parse(jsonString);

        IList <JsonElement> results1 = JsonSelector.Select(doc.RootElement,
                                                           "$.Data[*].KeyOfInterest");

        IList <JsonElement> results2 = JsonSelector.Select(doc.RootElement,
                                                           "$.Data[*]['KeyOfInterest', 'AnotherKey']");

        Console.WriteLine("Key of Interest");
        Console.WriteLine("(1)");
        Console.WriteLine(JsonSerializer.Serialize(results1));
        Console.WriteLine("(2)");
        Console.WriteLine(JsonSerializer.Serialize(results2));
        Console.WriteLine();
    }
        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;
                    }
                }
            }
        }
Example #6
0
    public static void StoreExample()
    {
        string jsonString = @"
{ ""store"": {
    ""book"": [ 
      { ""category"": ""reference"",
        ""author"": ""Nigel Rees"",
        ""title"": ""Sayings of the Century"",
        ""price"": 8.95
      },
      { ""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"": 8.99
      },
      { ""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
    }
  }
}
        ";

        using JsonDocument doc = JsonDocument.Parse(jsonString);

        var serializerOptions = new JsonSerializerOptions()
        {
            WriteIndented = true
        };

        Console.WriteLine(@"(1) The authors of all books in the store");
        IList <JsonElement> results1 = JsonSelector.Select(doc.RootElement, "$.store.book[*].author");

        Console.WriteLine(JsonSerializer.Serialize(results1, serializerOptions));
        Console.WriteLine();

        Console.WriteLine(@"(2) All authors");
        IList <JsonElement> results2 = JsonSelector.Select(doc.RootElement, "$..author");

        Console.WriteLine(JsonSerializer.Serialize(results2, serializerOptions));
        Console.WriteLine();

        Console.WriteLine(@"(3) All things in store - some books and a red bicycle");
        IList <JsonElement> results3 = JsonSelector.Select(doc.RootElement, "$.store.*");

        Console.WriteLine(JsonSerializer.Serialize(results3, serializerOptions));
        Console.WriteLine();

        Console.WriteLine(@"(4) The price of everything in the store.");
        IList <JsonElement> results4 = JsonSelector.Select(doc.RootElement, "$.store..price");

        Console.WriteLine(JsonSerializer.Serialize(results4, serializerOptions));
        Console.WriteLine();

        Console.WriteLine(@"(5) The third book");
        IList <JsonElement> results5 = JsonSelector.Select(doc.RootElement, "$..book[2]");

        Console.WriteLine(JsonSerializer.Serialize(results5, serializerOptions));
        Console.WriteLine();

        Console.WriteLine(@"(6) The last book");
        IList <JsonElement> results6 = JsonSelector.Select(doc.RootElement, "$..book[-1]");

        Console.WriteLine(JsonSerializer.Serialize(results6, serializerOptions));
        Console.WriteLine();

        Console.WriteLine(@"(7) The first two books");
        IList <JsonElement> results7 = JsonSelector.Select(doc.RootElement, "$..book[:2]");

        Console.WriteLine(JsonSerializer.Serialize(results7, serializerOptions));
        Console.WriteLine();

        Console.WriteLine(@"(8) Filter all books with isbn number");
        IList <JsonElement> results8 = JsonSelector.Select(doc.RootElement, "$..book[?(@.isbn)]");

        Console.WriteLine(JsonSerializer.Serialize(results8, serializerOptions));
        Console.WriteLine();

        Console.WriteLine(@"(9) Filter all books with prices between 5 and 10");
        IList <JsonElement> results9 = JsonSelector.Select(doc.RootElement, "$..book[?(@.price >= 5 && @.price < 10)]");

        Console.WriteLine(JsonSerializer.Serialize(results9, serializerOptions));
        Console.WriteLine();

        Console.WriteLine(@"(10) All books with authors that match ""evelyn"" (ignore case)");
        IList <JsonElement> results10 = JsonSelector.Select(doc.RootElement, "$..book[?(@.author =~ /evelyn.*?/i)]");

        Console.WriteLine(JsonSerializer.Serialize(results10, serializerOptions));
        Console.WriteLine();
    }
Example #7
0
    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();
    }
Example #8
0
    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();
    }
Example #9
0
    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();
    }
Example #10
0
    public static void UsingFunctionsInFilters()
    {
        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"": ""fiction"",
          ""author"": ""J. R. R. Tolkien"",
          ""title"": ""The Lord of the Rings""
        }
    ]
}
        ";

        using JsonDocument doc = JsonDocument.Parse(jsonString);

        var serializerOptions = new JsonSerializerOptions()
        {
            WriteIndented = true
        };

        Console.WriteLine("(1) All books whose author's last name is 'Tolkien'");
        IList <JsonElement> results1 = JsonSelector.Select(doc.RootElement, @"$.books[?(tokenize(@.author,'\\s+')[-1] == 'Tolkien')]");

        Console.WriteLine(JsonSerializer.Serialize(results1, serializerOptions));
        Console.WriteLine();

        Console.WriteLine("(2) All titles whose price is greater than the average price");
        IList <JsonElement> results2 = JsonSelector.Select(doc.RootElement, @"$.books[?(@.price > avg($.books[*].price))].title");

        Console.WriteLine(JsonSerializer.Serialize(results2, serializerOptions));
        Console.WriteLine();

        Console.WriteLine("(3) All titles whose price is greater than the average price (alternative)");
        IList <JsonElement> results3 = JsonSelector.Select(doc.RootElement, @"$.books[?(@.price > sum($.books[*].price)/length($.books[*].price))].title");

        Console.WriteLine(JsonSerializer.Serialize(results3, serializerOptions));
        Console.WriteLine();

        Console.WriteLine("(4) All books that don't have a price");
        IList <JsonElement> results4 = JsonSelector.Select(doc.RootElement, @"$.books[?(!contains(keys(@),'price'))]");

        Console.WriteLine(JsonSerializer.Serialize(results4, serializerOptions));
        Console.WriteLine();

        Console.WriteLine("(5) All books that have a price that rounds up to 23.6");
        IList <JsonElement> results5 = JsonSelector.Select(doc.RootElement, @"$.books[?(ceil(@.price*10) == 236)]");

        Console.WriteLine(JsonSerializer.Serialize(results5, serializerOptions));
        Console.WriteLine();

        Console.WriteLine("(6) All books that have a price that rounds down to 22.7");
        IList <JsonElement> results6 = JsonSelector.Select(doc.RootElement, @"$.books[?(floor(@.price*10) == 227)]");

        Console.WriteLine(JsonSerializer.Serialize(results6, serializerOptions));
        Console.WriteLine();
    }