Repository navigation
Expand file tree
/
Copy pathJsonPointer.cs
More file actions
217 lines (193 loc) · 7.43 KB
/
Copy pathJsonPointer.cs
File metadata and controls
217 lines (193 loc) · 7.43 KB
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
namespace ByteBard.AsyncAPI
{
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using ByteBard.AsyncAPI.Exceptions;
/// <summary>
/// JSON pointer as defined by RFC 6901.
/// </summary>
public class JsonPointer
{
/// <summary>
/// Initializes the <see cref="JsonPointer"/> class.
/// </summary>
/// <param name="pointer">Pointer in its JSON string representation (RFC 6901 section 5), e.g. <c>/components/schemas/a~1b</c>.
/// An empty string points to the whole document; <c>/</c> points to the member with an empty key.</param>
/// <exception cref="AsyncApiException">The pointer is malformed.</exception>
public JsonPointer(string pointer)
{
if (pointer == null)
{
throw new ArgumentNullException(nameof(pointer));
}
if (pointer.Length == 0)
{
this.Tokens = new string[0];
return;
}
if (pointer[0] != '/')
{
throw new AsyncApiException($"The JSON pointer '{pointer}' is malformed: it must be empty or start with '/'.");
}
this.Tokens = pointer.Substring(1).Split('/').Select(token => Unescape(token, pointer)).ToArray();
}
/// <summary>
/// Initializes the <see cref="JsonPointer"/> class.
/// </summary>
/// <param name="tokens">Pointer as tokenized string.</param>
private JsonPointer(string[] tokens)
{
this.Tokens = tokens;
}
/// <summary>
/// Tokens.
/// </summary>
public string[] Tokens { get; }
/// <summary>
/// Gets the parent pointer.
/// </summary>
public JsonPointer ParentPointer
{
get
{
if (this.Tokens.Length == 0)
{
return null;
}
return new JsonPointer(this.Tokens.Take(this.Tokens.Length - 1).ToArray());
}
}
/// <summary>
/// Creates a pointer from its URI fragment identifier representation (RFC 6901 section 6), e.g. the part after <c>#</c>
/// in <c>schemas.json#/components/schemas/with%20space</c>. The fragment is percent-decoded before it is parsed.
/// </summary>
/// <param name="fragment">The fragment, without the leading <c>#</c>.</param>
/// <exception cref="AsyncApiException">The fragment is not a valid JSON pointer.</exception>
public static JsonPointer FromUriFragment(string fragment)
{
if (fragment == null)
{
throw new ArgumentNullException(nameof(fragment));
}
return new JsonPointer(Uri.UnescapeDataString(fragment));
}
/// <summary>
/// Tries to create a pointer from its URI fragment identifier representation (RFC 6901 section 6).
/// </summary>
/// <param name="fragment">The fragment, without the leading <c>#</c>.</param>
/// <param name="pointer">The pointer, or <c>null</c> when the fragment is not a valid JSON pointer.</param>
/// <returns><c>true</c> when the fragment is a valid JSON pointer.</returns>
public static bool TryFromUriFragment(string fragment, out JsonPointer pointer)
{
pointer = null;
if (fragment == null)
{
return false;
}
try
{
pointer = FromUriFragment(fragment);
return true;
}
catch (AsyncApiException)
{
return false;
}
}
/// <summary>
/// Escapes a single reference token, so "~" becomes "~0" and "/" becomes "~1".
/// </summary>
/// <param name="token">The unescaped token.</param>
public static string EscapeToken(string token)
{
return token.Replace("~", "~0").Replace("/", "~1");
}
/// <summary>
/// Escapes a single reference token for a URI fragment identifier (RFC 6901 section 6), so "~" becomes "~0",
/// "/" becomes "~1", and the result is percent-encoded, e.g. "a b" becomes "a%20b" and "a%20b" becomes "a%2520b".
/// </summary>
/// <param name="token">The unescaped token.</param>
public static string EscapeUriFragmentToken(string token)
{
return Uri.EscapeDataString(EscapeToken(token));
}
/// <summary>
/// Gets the URI fragment identifier representation of this JSON pointer (RFC 6901 section 6), without the leading <c>#</c>.
/// This is the reverse of <see cref="FromUriFragment"/>.
/// </summary>
public string ToUriFragment()
{
return string.Concat(this.Tokens.Select(token => "/" + EscapeUriFragmentToken(token)));
}
/// <summary>
/// Finds the node this pointer references.
/// </summary>
/// <param name="document">The document to evaluate the pointer against.</param>
/// <returns>The referenced node, or <c>null</c> when it does not exist.</returns>
public JsonNode Find(JsonNode document)
{
var node = document;
foreach (var token in this.Tokens)
{
if (node is JsonArray sequence)
{
// RFC 6901 array indices are "0" or digits without a leading zero.
if ((token.Length > 1 && token[0] == '0')
|| !int.TryParse(token, NumberStyles.None, CultureInfo.InvariantCulture, out var index)
|| index >= sequence.Count)
{
return null;
}
node = sequence[index];
}
else if (node is not JsonObject map || !map.TryGetPropertyValue(token, out node))
{
// The token walks into a scalar or a missing property.
return null;
}
}
return node;
}
/// <summary>
/// Gets the JSON string representation of this JSON pointer.
/// </summary>
public override string ToString()
{
return string.Concat(this.Tokens.Select(token => "/" + EscapeToken(token)));
}
private static string Unescape(string token, string pointer)
{
if (token.IndexOf('~') < 0)
{
return token;
}
var builder = new StringBuilder(token.Length);
for (var i = 0; i < token.Length; i++)
{
if (token[i] != '~')
{
builder.Append(token[i]);
continue;
}
var next = i + 1 < token.Length ? token[i + 1] : '\0';
if (next == '0')
{
builder.Append('~');
}
else if (next == '1')
{
builder.Append('/');
}
else
{
throw new AsyncApiException($"The JSON pointer '{pointer}' is malformed: '~' must be followed by '0' or '1'.");
}
i++;
}
return builder.ToString();
}
}
}