-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathJsonDataReader.cs
More file actions
434 lines (373 loc) · 18.6 KB
/
Copy pathJsonDataReader.cs
File metadata and controls
434 lines (373 loc) · 18.6 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
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
namespace CoreEx.UnitTesting.Data;
/// <summary>
/// Provides a hierarchical mutating reader for JSON or YAML data with dynamic property substitution support using the venerable <see cref="JsonNode"/>.
/// </summary>
/// <remarks>This is <i>not</i> intended for high-volume or high-performance use; more for the likes of basic dynamic data seeding scenarios in unit tests.</remarks>
public sealed partial class JsonDataReader
{
private static readonly Regex _regex = EmbeddedDynamicParametersRegex();
private readonly JsonNode _rootNode;
/// <summary>
/// Initializes a new instance of the <see cref="JsonDataReader"/> class.
/// </summary>
private JsonDataReader(JsonNode root, JsonDataReaderOptions? options)
{
_rootNode = root.ThrowIfNull().ThrowWhen(root => root is not JsonObject, "JSON root node must be a JsonObject.");
Options = options ?? new JsonDataReaderOptions();
}
/// <summary>
/// Gets the <see cref="JsonDataReaderOptions"/>.
/// </summary>
public JsonDataReaderOptions Options { get; }
/// <summary>
/// Gets the root <see cref="JsonNode"/>.
/// </summary>
public JsonNode RootNode => _rootNode;
/// <summary>
/// Tries to get the <see cref="JsonNode"/> for the specified <paramref name="path"/>.
/// </summary>
/// <param name="path">The qualified path to support child navigation.</param>
/// <param name="jsonNode">The <see cref="JsonNode"/> found.</param>
/// <returns><see langword="true"/> indicates found; otherwise, false.</returns>
public bool TryGetPath(string path, out JsonNode? jsonNode)
{
jsonNode = JsonFilter.GetMatched(_rootNode.DeepClone(), path);
return jsonNode is not null;
}
/// <summary>
/// Tries to create the <see cref="JsonNode"/> replacing any dynamic parameters for the specified <paramref name="path"/>.
/// </summary>
/// <param name="path">The qualified path to support child navigation.</param>
/// <param name="jsonNode">The resulting <see cref="JsonNode"/> with all dynamic parameters replaced where possible.</param>
/// <returns><see langword="true"/> indicates created; otherwise, false.</returns>
public bool TryCreateData(string path, [NotNullWhen(true)] out JsonNode? jsonNode)
{
jsonNode = null;
if (!TryGetPath(path, out var jn))
return false;
jsonNode = CopyAndReplace(jn, new JsonDataReaderArgs(Options.Parameters) { Root = jn, CurrentPropertyName = null, CurrentNode = null, Properties = Options.Properties, ApplyProperties = true });
return jsonNode is not null;
}
/// <summary>
/// Copies the JSON and replaces any dynamic parameters.
/// </summary>
private JsonNode? CopyAndReplace(JsonNode? jn, JsonDataReaderArgs args)
{
if (jn is null)
return null;
switch (jn)
{
case JsonArray ja:
var newArray = new JsonArray();
for (int i = 0; i < ja.Count; i++)
{
var item = ja[i];
var newItem = CopyAndReplace(item, new JsonDataReaderArgs(args.Parameters) { Root = args.Root, CurrentPropertyName = null, CurrentNode = null, Index = i, Properties = args.Properties, ApplyProperties = args.ApplyProperties });
newArray.Add(newItem);
}
return newArray;
case JsonObject jo:
if (args.ApplyProperties)
Options.RootNodePreProcessor?.Invoke(new JsonDataReaderArgs(args.Parameters) { Root = args.Root, CurrentPropertyName = args.CurrentPropertyName, CurrentNode = jo, Properties = args.Properties, Index = args.Index });
var newObject = new JsonObject();
foreach (var kvp in jo)
{
var newValue = CopyAndReplace(kvp.Value, new JsonDataReaderArgs(args.Parameters) { Root = args.Root, CurrentPropertyName = kvp.Key, CurrentNode = kvp.Value, Properties = args.Properties, Index = args.Index });
newObject[kvp.Key] = newValue;
}
if (args.ApplyProperties && Options.Properties.Count > 0)
ApplyPropertiesWhereNotFound(jo, newObject, new JsonDataReaderArgs(args.Parameters) { Root = args.Root, CurrentPropertyName = null, CurrentNode = null, Properties = args.Properties, Index = args.Index });
args.ApplyProperties = false;
return newObject;
case JsonValue jv:
return ReplaceDynamicParameter(jv, args);
default:
return jn.DeepClone();
}
}
/// <summary>
/// Apply properties where not found in the source JSON.
/// </summary>
private void ApplyPropertiesWhereNotFound(JsonObject sourceObject, JsonObject targetObject, JsonDataReaderArgs args)
{
// Apply the args properties first.
if (args.Properties is not null)
{
foreach (var ap in args.Properties)
{
if (sourceObject.ContainsKey(ap.Key))
continue;
object? val = ap.Value;
if (ap.Value is string str && !string.IsNullOrEmpty(str) && str.Length > 1 && str[0] == '^')
{
if (TryGetDynamicValue(str[1..], args, out var v))
val = v;
}
if (val is not null)
targetObject[ap.Key] = CreateJsonValue(val);
}
}
// Apply the options properties second.
foreach (var p in Options.Properties)
{
if (sourceObject.ContainsKey(p.Key) || (args.Properties is not null && args.Properties.ContainsKey(p.Key)))
continue;
object? val = p.Value;
if (p.Value is string str && !string.IsNullOrEmpty(str) && str.Length > 1 && str[0] == '^')
{
if (TryGetDynamicValue(str[1..], args, out var v))
val = v;
}
if (val is not null)
targetObject[p.Key] = CreateJsonValue(val);
}
}
/// <summary>
/// Replace any '^xxx' dynamic placeholders.
/// </summary>
private static JsonNode? ReplaceDynamicParameter(JsonValue jv, JsonDataReaderArgs args)
{
if (jv.GetValueKind() != JsonValueKind.String)
return jv.DeepClone();
var str = jv.GetValue<string>();
if (!string.IsNullOrEmpty(str) && str.Length > 1 && str[0] == '^')
{
if (TryGetDynamicValue(str[1..], args, out var val))
{
if (val is string str2 && str2 is not null && ReplaceEmbeddedDynamicParameters(ref str2!, args))
val = str2;
return CreateJsonValue(val);
}
}
if (ReplaceEmbeddedDynamicParameters(jv, args, out var replacedNode))
return replacedNode;
return jv.DeepClone();
}
/// <summary>
/// Replace any embedded '(^xxx)' dynamic placeholders.
/// </summary>
private static bool ReplaceEmbeddedDynamicParameters(JsonValue jv, JsonDataReaderArgs args, out JsonNode? result)
{
result = null;
if (jv.TryGetValue<string>(out var str))
{
if (ReplaceEmbeddedDynamicParameters(ref str, args))
{
result = CreateJsonValue(str);
return true;
}
}
return false;
}
/// <summary>
/// Replace any embedded '(^xxx)' dynamic placeholders.
/// </summary>
private static bool ReplaceEmbeddedDynamicParameters(ref string? str, JsonDataReaderArgs args)
{
if (string.IsNullOrEmpty(str))
return false;
var sb = new StringBuilder();
int i = 0;
foreach (var match in _regex.EnumerateMatches(str))
{
sb.Append(str.AsSpan(i, match.Index - i));
var key = str.Substring(match.Index, match.Length);
if (TryGetDynamicValue(key[2..^1], args, out var val))
{
var str2 = val?.ToString();
ReplaceEmbeddedDynamicParameters(ref str2, args);
sb.Append(str2);
}
else
sb.Append(key);
i = match.Index + match.Length;
}
if (sb.Length == 0)
return false;
sb.Append(str.AsSpan(i, str.Length - i));
str = sb.ToString();
return true;
}
/// <summary>
/// Tries to get the dynamic value.
/// </summary>
private static bool TryGetDynamicValue(string key, JsonDataReaderArgs args, out object? value)
{
if (args.Parameters.TryGetValue(key, out var func))
{
value = func(args);
if (value is string str && str is not null && ReplaceEmbeddedDynamicParameters(ref str!, args))
value = str;
return true;
}
else if (int.TryParse(key, out var i))
{
value = new Guid(i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
return true;
}
value = null;
return false;
}
/// <summary>
/// Creates a <see cref="JsonValue"/> from a .NET value.
/// </summary>
private static JsonValue? CreateJsonValue(object? val)
{
if (val is null)
return null;
return val switch
{
string sv => JsonValue.Create(sv),
Guid gv => JsonValue.Create(gv),
DateTime dv => JsonValue.Create(dv),
DateTimeOffset ov => JsonValue.Create(ov),
DateOnly dv => JsonValue.Create(dv.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)),
TimeOnly tv => JsonValue.Create(tv.ToString("HH:mm:ss.FFFFFFF", System.Globalization.CultureInfo.InvariantCulture)),
bool bv => JsonValue.Create(bv),
short nsv => JsonValue.Create(nsv),
int niv => JsonValue.Create(niv),
long nlv => JsonValue.Create(nlv),
ushort nusv => JsonValue.Create(nusv),
uint nuiv => JsonValue.Create(nuiv),
ulong nulv => JsonValue.Create(nulv),
decimal ndv => JsonValue.Create(ndv),
double n2v => JsonValue.Create(n2v),
float nfv => JsonValue.Create(nfv),
_ => JsonValue.Create(val.ToString())
};
}
/// <summary>
/// Tries to create a new <see cref="JsonNode"/> replacing any dynamic parameters for the specified <paramref name="path"/> and deserializes to the specified <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> to deserialize to.</typeparam>
/// <param name="path">The qualified path to support child navigation.</param>
/// <param name="options">The optional <see cref="JsonSerializerOptions"/>.</param>
/// <returns>The deserialized value where found; otherwise, <see langword="default"/>.</returns>
/// <remarks>Where specifying the <paramref name="options"/> then the <see cref="JsonSerializerOptions.PropertyNamingPolicy"/> should be aligned with the <see cref="JsonDataReaderOptions.NamingConvention"/>.
/// Where not specified (i.e. <see langword="null"/>), then the <see cref="JsonDataReaderOptions.NamingConvention"/> will automatically be used.</remarks>
public T? Deserialize<T>(string path, JsonSerializerOptions? options = null)
{
if (!TryCreateData(path, out var jsonNode))
return default;
if (options is null)
{
if (Options.SerializerOptions is not null)
options = Options.SerializerOptions;
else
options = new JsonSerializerOptions(JsonDefaults.SerializerOptions)
{
PropertyNamingPolicy = Options.NamingConvention switch
{
JsonPropertyNamingConvention.CamelCase => JsonNamingPolicy.CamelCase,
JsonPropertyNamingConvention.SnakeCase => JsonNamingPolicy.SnakeCaseLower,
JsonPropertyNamingConvention.KebabCase => JsonNamingPolicy.KebabCaseLower,
_ => null
}
};
}
return JsonSerializer.Deserialize<T>(jsonNode, options);
}
#region ParseYaml+Json
/// <summary>
/// Reads and parses the YAML from the named embedded resource <see cref="Stream"/> within the <see name="Assembly"/> inferred from the <typeparamref name="TResource"/> <see cref="Type"/>.
/// </summary>
/// <typeparam name="TResource">The <see cref="Type"/> to infer the <see cref="System.Reflection.Assembly"/> to find manifest resources (see <see cref="System.Reflection.Assembly.GetManifestResourceStream(string)"/>).</typeparam>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualified resource name).</param>
/// <param name="options">The optional <see cref="JsonDataReaderOptions"/>.</param>
/// <returns>The <see cref="JsonDataReader"/>.</returns>
public static JsonDataReader ParseYaml<TResource>(string resourceName, JsonDataReaderOptions? options = null) => ParseYaml(CoreEx.Abstractions.Resource.GetStream<TResource>(resourceName), options);
/// <summary>
/// Reads and parses the YAML <see cref="string"/>.
/// </summary>
/// <param name="yaml">The YAML <see cref="string"/>.</param>
/// <param name="options">The optional <see cref="JsonDataReaderOptions"/>.</param>
/// <returns>The <see cref="JsonDataReader"/>.</returns>
public static JsonDataReader ParseYaml(string yaml, JsonDataReaderOptions? options = null)
{
using var sr = new StringReader(yaml);
return ParseYaml(sr, options);
}
/// <summary>
/// Reads and parses the YAML <see cref="Stream"/>.
/// </summary>
/// <param name="s">The YAML <see cref="Stream"/>.</param>
/// <param name="options">The optional <see cref="JsonDataReaderOptions"/>.</param>
/// <returns>The <see cref="JsonDataReader"/>.</returns>
public static JsonDataReader ParseYaml(Stream s, JsonDataReaderOptions? options = null) => ParseYaml(new StreamReader(s), options);
/// <summary>
/// Reads and parses the YAML <see cref="TextReader"/>.
/// </summary>
/// <param name="tr">The YAML <see cref="TextReader"/>.</param>
/// <param name="options">The optional <see cref="JsonDataReaderOptions"/>.</param>
/// <returns>The <see cref="JsonDataReader"/>.</returns>
public static JsonDataReader ParseYaml(TextReader tr, JsonDataReaderOptions? options = null)
{
var yaml = new DeserializerBuilder().WithNodeTypeResolver(new YamlNodeTypeResolver()).Build().Deserialize(tr);
var json = new SerializerBuilder().JsonCompatible().Build().Serialize(yaml!);
return new(JsonNode.Parse(json) ?? throw new InvalidOperationException("JsonNode.Parse resulted in a null."), options);
}
/// <summary>
/// Reads and parses the JSON from the named embedded resource <see cref="Stream"/> within the <see name="Assembly"/> inferred from the <typeparamref name="TResource"/> <see cref="Type"/>.
/// </summary>
/// <typeparam name="TResource">The <see cref="Type"/> to infer the <see cref="System.Reflection.Assembly"/> to find manifest resources (see <see cref="System.Reflection.Assembly.GetManifestResourceStream(string)"/>).</typeparam>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualified resource name).</param>
/// <param name="options">The optional <see cref="JsonDataReaderOptions"/>.</param>
/// <returns>The <see cref="JsonDataReader"/>.</returns>
public static JsonDataReader ParseJson<TResource>(string resourceName, JsonDataReaderOptions? options = null) => ParseJson(CoreEx.Abstractions.Resource.GetStream<TResource>(resourceName), options);
/// <summary>
/// Reads and parses the JSON <see cref="string"/>.
/// </summary>
/// <param name="json">The JSON <see cref="string"/>.</param>
/// <param name="options">The optional <see cref="JsonDataReaderOptions"/>.</param>
/// <returns>The <see cref="JsonDataReader"/>.</returns>
public static JsonDataReader ParseJson([StringSyntax(StringSyntaxAttribute.Json)] string json, JsonDataReaderOptions? options = null)
=> new(JsonNode.Parse(json) ?? throw new InvalidOperationException("JsonNode.Parse resulted in a null."), options);
/// <summary>
/// Reads and parses the JSON <see cref="Stream"/>.
/// </summary>
/// <param name="s">The JSON <see cref="Stream"/>.</param>
/// <param name="options">The optional <see cref="JsonDataReaderOptions"/>.</param>
/// <returns>The <see cref="JsonDataReader"/>.</returns>
public static JsonDataReader ParseJson(Stream s, JsonDataReaderOptions? options = null) => new(JsonNode.Parse(s) ?? throw new InvalidOperationException("JsonNode.Parse resulted in a null."), options);
/// <summary>
/// Reads and parses the <see cref="JsonNode"/>.
/// </summary>
/// <param name="jsonNode">The <see cref="JsonNode"/>.</param>
/// <param name="options">The optional <see cref="JsonDataReaderOptions"/>.</param>
/// <returns>The <see cref="JsonDataReader"/>.</returns>
public static JsonDataReader ParseJson(JsonNode jsonNode, JsonDataReaderOptions? options = null) => new(jsonNode, options);
#endregion
/// <summary>
/// A custom <see cref="INodeTypeResolver"/> to support the YAML to JSON conversion of boolean and number types.
/// </summary>
private sealed class YamlNodeTypeResolver : INodeTypeResolver
{
private static readonly string[] boolValues = ["true", "false"];
/// <inheritdoc/>
bool INodeTypeResolver.Resolve(NodeEvent? nodeEvent, ref Type currentType)
{
if (nodeEvent is Scalar scalar && scalar.Style == YamlDotNet.Core.ScalarStyle.Plain)
{
if (decimal.TryParse(scalar.Value, out _))
{
if (scalar.Value.Length > 1 && scalar.Value.StartsWith('0')) // Valid JSON does not support a number that starts with a zero.
currentType = typeof(string);
else
currentType = typeof(decimal);
return true;
}
if (boolValues.Contains(scalar.Value))
{
currentType = typeof(bool);
return true;
}
}
return false;
}
}
/// <summary>
/// Provides the generated <see cref="Regex"/> for <see cref="ReplaceEmbeddedDynamicParameters(ref string?, JsonDataReaderArgs)"/>.
/// </summary>
[GeneratedRegex(@"\(\^(.*?)\)", RegexOptions.Compiled)]
private static partial Regex EmbeddedDynamicParametersRegex();
}