Skip to content

Commit de98a5a

Browse files
committed
update with latest architecture
1 parent 2fc227a commit de98a5a

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

_docs/pointer/basics.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ It also supports a URL format, which is essentially the same thing, except that
5050

5151
## In code {#pointer-in-code}
5252

53-
The `JsonPointer` struct is the model for JSON Pointer.
53+
The `JsonPointer` class is the model for JSON Pointer.
5454

5555
There are three ways create pointers:
5656

@@ -87,6 +87,55 @@ var success = pointer.TryEvaluate(element, out var result);
8787
> The designers of the `JsonNode` API have elected (for [reasons](https://github.com/dotnet/designs/blob/40794be63ecd8b35e9596412050a84dedd575b99/accepted/2020/serializer/WriteableDomAndDynamic.md#missing-vs-null) I [disagree](https://github.com/dotnet/runtime/issues/66948#issuecomment-1080148457) with) to consider JSON null and .Net null to be equivalent. This goes against both my personal experience building Manatee.Json and the `JsonElement` API, both of which maintain a separation between these two null concepts. Because of the unified design, it's impossible to determine whether a returned `JsonNode` value of `null` represents a value that is present but null or it is merely absent from the data. To accommodate this, the evaluation method can only support the familiar `TryParse()` signature. A return of `true` indicates the value was found, and `false` indicates it was not. In the case of a `true` return, `result` may still be null, indicating the value was found and was a JSON null.
8888
{: .prompt-info }
8989

90+
### Pointer math
91+
92+
You can also combine and augment pointers in different ways.
93+
94+
Joining two pointers together:
95+
96+
```c#
97+
var pointer1 = JsonPointer.Parse("/objects/and");
98+
var pointer2 = JsonPointer.Parse("/3/arrays");
99+
var final = pointer1.Combine(pointer2);
100+
```
101+
102+
Appending additional segments to an existing pointer:
103+
104+
```c#
105+
var pointer = JsonPointer.Parse("/objects/and");
106+
var final = pointer1.Combine(3, "arrays");
107+
```
108+
109+
### Access pointer parts and create sub-pointers
110+
111+
You can retrieve the individual segments using the indexer:
112+
113+
```c#
114+
var pointer = JsonPointer.Parse("/objects/and/3/arrays");
115+
var andSegment = pointer[1]; // "and" (string)
116+
```
117+
118+
If you're using .Net 8 or higher, the indexer also supports `Range` values, so you can obtain a new pointer containing a portion of the segments.
119+
120+
Get the immediate parent:
121+
122+
```c#
123+
var pointer = JsonPointer.Parse("/objects/and/3/arrays");
124+
var parent = pointer[..^1]; // /objects/and/3
125+
```
126+
127+
Or get the local pointer (imagine you've navigated to `/objects/and/` and you need the pointer relative to where you are):
128+
129+
```c#
130+
var pointer = JsonPointer.Parse("/objects/and/3/arrays");
131+
var local = pointer[^2..]; // /3/arrays
132+
```
133+
134+
There are also method versions of this functionality, which are also available if you're not yet using .Net 8: `.GetAncestor(int)` and `.GetLocal()`.
135+
136+
> Accessing pointers acts like accessing strings: getting segments has no allocations (like getting a `char` via the string's `int` indexer), but creating a sub-pointer _does_ allocate a new `JsonPointer` instance (like creating a substring via the string's `Range` indexer).
137+
{: .prompt-info }
138+
90139
### Building pointers using Linq expressions {#linq}
91140

92141
When building a pointer using the `Create<T>()` method which takes a Linq expression, there are a couple of things to be aware of.

0 commit comments

Comments
 (0)