-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJsonPointer.cs
580 lines (538 loc) · 20.2 KB
/
JsonPointer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
namespace JsonCons.Utilities
{
/// <summary>
/// Represents a JSON Pointer as defined by <see href="https://datatracker.ietf.org/doc/html/rfc6901">RFC 6901</see>
/// </summary>
/// <example>
/// The following example shows how to get a value at a referenced location in a JSON document.
/// <code>
/// using System;
/// using System.Diagnostics;
/// using System.Text.Json;
/// using JsonCons.Utilities;
///
/// public class Example
/// {
/// public static void Main()
/// {
/// using var doc = JsonDocument.Parse(@"
/// [
/// { ""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
/// }
/// ]
/// ");
///
/// var options = new JsonSerializerOptions() { WriteIndented = true };
///
/// JsonPointer pointer = JsonPointer.Parse("/1/author");
///
/// JsonElement element;
///
/// if (pointer.TryGetValue(doc.RootElement, out element))
/// {
/// Console.WriteLine($"{JsonSerializer.Serialize(element, options)}\n");
/// }
/// }
/// }
/// </code>
/// Output:
///
/// <code>
/// "Evelyn Waugh"
/// </code>
/// </example>
public sealed class JsonPointer : IEnumerable<string>, IEquatable<JsonPointer>
{
/// <summary>Gets a singleton instance of a <see cref="JsonPointer"/> to the root value of a JSON document.</summary>
public static JsonPointer Default {get;} = new JsonPointer();
enum JsonPointerState {Start, Escaped, Delim}
/// <summary>
/// Returns a list of (unescaped) reference tokens
/// </summary>
public IReadOnlyList<string> Tokens {get;}
/// <summary>
/// Constructs a JSON Pointer to the root value of a JSON document
/// </summary>
public JsonPointer()
{
Tokens = new List<string>();
}
/// <summary>
/// Constructs a JSON Pointer from a list of (unescaped) reference tokens
/// </summary>
/// <param name="tokens">A list of (unescaped) reference tokens.</param>
public JsonPointer(IReadOnlyList<string> tokens)
{
Tokens = tokens;
}
/// <summary>
/// Parses a JSON Pointer represented as a string value or a
/// fragment identifier (starts with <c>#</c>) into a <see cref="JsonPointer"/>.
/// </summary>
/// <param name="input">A JSON Pointer represented as a string or a fragment identifier.</param>
/// <returns>A <see cref="JsonPointer"/>.</returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="input"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// The <paramref name="input"/> is invalid.
/// </exception>
public static JsonPointer Parse(string input)
{
JsonPointer pointer;
if (!TryParse(input, out pointer))
{
throw new ArgumentException("The provided JSON Pointer is invalid.");
}
return pointer;
}
/// <summary>
/// Parses a JSON Pointer represented as a string value or a
/// fragment identifier (starts with <c>#</c>) into a <see cref="JsonPointer"/>.
/// </summary>
/// <param name="input">A JSON Pointer represented as a string or a fragment identifier.</param>
/// <param name="pointer">The JsonPointer.</param>
/// <returns><c>true</c> if the input string can be parsed into a list of reference tokens, <c>false</c> otherwise.</returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="input"/> is <see langword="null"/>.
/// </exception>
public static bool TryParse(string input, out JsonPointer pointer)
{
if (input == null)
{
throw new ArgumentNullException(nameof(input));
}
var tokens = new List<string>();
if (input.Length == 0 || input.Equals("#"))
{
pointer = new JsonPointer(tokens);
return true;
}
JsonPointerState state = JsonPointerState.Start;
int index = 0;
var buffer = new StringBuilder();
if (input[0] == '#')
{
input = Uri.UnescapeDataString(input);
index = 1;
}
while (index < input.Length)
{
bool done = false;
while (index < input.Length && !done)
{
switch (state)
{
case JsonPointerState.Start:
switch (input[index])
{
case '/':
state = JsonPointerState.Delim;
break;
default:
pointer = JsonPointer.Default;
return false;
};
break;
case JsonPointerState.Delim:
switch (input[index])
{
case '/':
done = true;
break;
case '~':
state = JsonPointerState.Escaped;
break;
default:
buffer.Append(input[index]);
break;
};
break;
case JsonPointerState.Escaped:
switch (input[index])
{
case '0':
buffer.Append('~');
state = JsonPointerState.Delim;
break;
case '1':
buffer.Append('/');
state = JsonPointerState.Delim;
break;
default:
pointer = JsonPointer.Default;
return false;
};
break;
default:
pointer = JsonPointer.Default;
return false;
}
++index;
}
tokens.Add(buffer.ToString());
buffer.Clear();
}
if (buffer.Length > 0)
{
tokens.Add(buffer.ToString());
}
pointer = new JsonPointer(tokens);
return true;
}
/// <summary>
/// Returns an enumerator that iterates through a list of reference tokens.
/// </summary>
/// <returns>An <c>IEnumerator<string></c> for a list of reference tokens.</returns>
public IEnumerator<string> GetEnumerator()
{
return Tokens.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return (System.Collections.IEnumerator) GetEnumerator();
}
/// <summary>
/// Returns a JSON Pointer represented as a string value.
/// </summary>
/// <returns>A JSON Pointer represented as a string value.</returns>
public override string ToString()
{
var buffer = new StringBuilder();
foreach (var token in Tokens)
{
buffer.Append("/");
Escape(token, buffer);
}
return buffer.ToString();
}
/// <summary>
/// Returns a string representing the JSON Pointer as a URI fragment identifier
/// </summary>
/// <returns>A JSON Pointer represented as a fragment identifier.</returns>
public string ToUriFragment()
{
var buffer = new StringBuilder();
buffer.Append("#");
foreach (var token in Tokens)
{
buffer.Append("/");
string s = Uri.EscapeUriString(token);
var span = s.AsSpan();
for (int i = 0; i < span.Length; ++i)
{
char c = span[i];
switch (c)
{
case '~':
buffer.Append('~');
buffer.Append('0');
break;
case '/':
buffer.Append('~');
buffer.Append('1');
break;
default:
buffer.Append(c);
break;
}
}
}
return buffer.ToString();
}
/// <summary>
/// Determines whether two JSONPointer objects have the same value.
/// </summary>
/// <param name="other"></param>
/// <returns><c>true</c> if other is a <see cref="JsonPointer"/> and has exactly the same reference tokens as this instance; otherwise, <c>false</c>.
/// If other is <c>null</c>, the method returns <c>false</c>.</returns>
public bool Equals(JsonPointer other)
{
if (other == null)
{
return false;
}
if (Tokens.Count != other.Tokens.Count)
{
return false;
}
for (int i = 0; i < Tokens.Count; ++i)
{
if (!Tokens[i].Equals(other.Tokens[i]))
{
return false;
}
}
return true;
}
/// <summary>
/// Determines whether this instance and a specified object, which must also be a JSONPointer object, have the same value.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public override bool Equals(Object other)
{
if (other == null)
{
return false;
}
return Equals((JsonPointer)other);
}
/// <summary>
/// Returns the hash code for this <see cref="JsonPointer"/>
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
/// <returns></returns>
public override int GetHashCode()
{
int hashCode = Tokens.GetHashCode();
foreach (var token in Tokens)
{
hashCode += 17*token.GetHashCode();
}
return hashCode;
}
/// <summary>
/// Returns <c>true</c> if the provided <see cref="JsonElement"/> contains a value at the referenced location.
/// </summary>
/// <param name="root">The root <see cref="JsonElement"/> that is to be queried.</param>
/// <returns><c>true</c> if the provided <see cref="JsonElement"/> contains a value at the referenced location, otherwise <c>false</c>.</returns>
public bool ContainsValue(JsonElement root)
{
JsonElement value = root;
foreach (var token in Tokens)
{
if (value.ValueKind == JsonValueKind.Array)
{
if (token == "-")
{
return false;
}
int index = 0;
if (!int.TryParse(token, out index))
{
return false;
}
if (index >= value.GetArrayLength())
{
return false;
}
value = value[index];
}
else if (value.ValueKind == JsonValueKind.Object)
{
if (!value.TryGetProperty(token, out value))
{
return false;
}
}
else
{
return false;
}
}
return true;
}
/// <summary>
/// Returns <c>true</c> if the provided <see cref="JsonElement"/> contains a value at the referenced location.
/// </summary>
/// <param name="root">The root <see cref="JsonElement"/> that is to be queried.</param>
/// <param name="pointer">The JSON string or URI Fragment representation of the JSON pointer.</param>
/// <returns><c>true</c> if the provided <see cref="JsonElement"/> contains a value at the referenced location, otherwise <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="pointer"/> is <see langword="null"/>.
/// </exception>
public static bool ContainsValue(JsonElement root, string pointer)
{
if (pointer == null)
{
throw new ArgumentNullException(nameof(pointer));
}
JsonPointer location;
if (!TryParse(pointer, out location))
{
return false;
}
JsonElement value = root;
foreach (var token in location.Tokens)
{
if (value.ValueKind == JsonValueKind.Array)
{
if (token == "-")
{
return false;
}
int index = 0;
if (!int.TryParse(token, out index))
{
return false;
}
if (index >= value.GetArrayLength())
{
return false;
}
value = value[index];
}
else if (value.ValueKind == JsonValueKind.Object)
{
if (!value.TryGetProperty(token, out value))
{
return false;
}
}
else
{
return false;
}
}
return true;
}
/// <summary>
/// Gets the value at the referenced location in the provided <see cref="JsonElement"/>.
/// </summary>
/// <param name="root">The root <see cref="JsonElement"/> that is to be queried.</param>
/// <param name="value">Contains the value at the referenced location, if found.</param>
/// <returns><c>true</c> if the value was found at the referenced location, otherwise <c>false</c>.</returns>
public bool TryGetValue(JsonElement root, out JsonElement value)
{
value = root;
foreach (var token in Tokens)
{
if (value.ValueKind == JsonValueKind.Array)
{
if (token == "-")
{
return false;
}
int index = 0;
if (!int.TryParse(token, out index))
{
return false;
}
if (index >= value.GetArrayLength())
{
return false;
}
value = value[index];
}
else if (value.ValueKind == JsonValueKind.Object)
{
if (!value.TryGetProperty(token, out value))
{
return false;
}
}
else
{
return false;
}
}
return true;
}
/// <summary>
/// Creates a new JsonPointer by appending a name token to the provided JsonPointer.
/// </summary>
/// <param name="pointer">The provided JsonPointer</param>
/// <param name="token">A name token</param>
/// <returns>A new JsonPointer</returns>
public static JsonPointer Append(JsonPointer pointer, string token)
{
var tokens = new List<string>(pointer.Tokens);
tokens.Add(token);
return new JsonPointer(tokens);
}
/// <summary>
/// Creates a new JsonPointer by appending an index token to the provided JsonPointer.
/// </summary>
/// <param name="pointer">The provided JsonPointer</param>
/// <param name="token">An index token</param>
/// <returns>A new JsonPointer</returns>
public static JsonPointer Append(JsonPointer pointer, int token)
{
var tokens = new List<string>(pointer.Tokens);
tokens.Add(token.ToString());
return new JsonPointer(tokens);
}
/// <summary>
/// Gets the value at the referenced location in the provided <see cref="JsonElement"/>.
/// </summary>
/// <param name="root">The root <see cref="JsonElement"/> that is to be queried.</param>
/// <param name="pointer">The JSON string or URI Fragment representation of the JSON pointer.</param>
/// <param name="value">Contains the value at the referenced location, if found.</param>
/// <returns><c>true</c> if the value was found at the referenced location, otherwise <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="pointer"/> is <see langword="null"/>.
/// </exception>
public static bool TryGetValue(JsonElement root, string pointer, out JsonElement value)
{
if (pointer == null)
{
throw new ArgumentNullException(nameof(pointer));
}
JsonPointer location;
if (!TryParse(pointer, out location))
{
value = root;
return false;
}
return location.TryGetValue(root, out value);
}
/// <summary>
/// Escapes a JSON Pointer token
/// </summary>
/// <returns>
/// </returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="token"/> is <see langword="null"/>.
/// </exception>
public static string Escape(string token)
{
if (token == null)
{
throw new ArgumentNullException(nameof(token));
}
var buffer = new StringBuilder();
Escape(token, buffer);
return buffer.ToString();
}
static void Escape(string token, StringBuilder buffer)
{
if (token == null)
{
throw new ArgumentNullException(nameof(token));
}
foreach (var c in token)
{
switch (c)
{
case '~':
buffer.Append('~');
buffer.Append('0');
break;
case '/':
buffer.Append('~');
buffer.Append('1');
break;
default:
buffer.Append(c);
break;
}
}
}
}
} // namespace JsonCons.Utilities