コード例 #1
0
    static void CreateJsonPointer()
    {
        using var doc = JsonDocument.Parse(@"
[
  {
    ""key"": true,
    ""other~key"": [""foo""]
  },
  {
    ""key"": false,
    ""other~key"": [""bar"", ""baz""]
  },
  {
    ""key"": true,
    ""other~key"": [""qux""]
  }
]
        ");

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

        var tokens = new List <string> {
            "1", "other~key"
        };
        JsonPointer aPointer = new JsonPointer(tokens);

        Console.WriteLine($"(1) {aPointer}");
        //(1) /1/other~0key
        Console.WriteLine();

        JsonPointer anotherPointer = JsonPointer.Append(aPointer, 1);

        Console.WriteLine($"(2) {anotherPointer}");
        //(2) /1/other~0key/1
        Console.WriteLine();

        JsonElement element;

        if (anotherPointer.TryGetValue(doc.RootElement, out element))
        {
            Console.WriteLine($"(3) {JsonSerializer.Serialize(element, options)}\n");
        }
        //(3) "baz"
        Console.WriteLine();
    }