-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJsonElementExtensions.cs
More file actions
78 lines (68 loc) · 3.05 KB
/
Copy pathJsonElementExtensions.cs
File metadata and controls
78 lines (68 loc) · 3.05 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
using System;
using System.Linq;
using System.Text.Json;
namespace PowerCSharp.Extensions.Json;
/// <summary>
/// Extension methods for JsonElement with case-insensitive property access and convenient getters
/// </summary>
public static class JsonElementExtensions
{
/// <summary>
/// Looks for a property with the specified name using case-insensitive comparison.
/// </summary>
/// <param name="element">The JSON element to search within.</param>
/// <param name="propertyName">The name of the property to find (case-insensitive).</param>
/// <param name="value">When this method returns, contains the JsonElement value if the property was found; otherwise, the default value.</param>
/// <returns>True if the property exists; otherwise, false.</returns>
/// <exception cref="ArgumentNullException">Thrown when propertyName is null.</exception>
public static bool TryGetPropertyCaseInsensitive(this JsonElement element, string propertyName, out JsonElement value)
{
if (propertyName == null)
{
throw new ArgumentNullException(nameof(propertyName));
}
var result = false;
value = default;
var property = element
.EnumerateObject()
.FirstOrDefault(p => string.Compare(p.Name, propertyName, StringComparison.OrdinalIgnoreCase) == 0);
if (property.Value.ValueKind != JsonValueKind.Undefined)
{
value = property.Value;
result = true;
}
return result;
}
/// <summary>
/// Looks for a property named <paramref name="name"/> in the current JSON object.
/// Returns null if the element is null, undefined, or the property doesn't exist.
/// </summary>
/// <param name="element">The JSON element to search within.</param>
/// <param name="name">The name of the property to find (case-sensitive).</param>
/// <returns>The JsonElement value if found; otherwise, null.</returns>
public static JsonElement? Get(this JsonElement element, string name)
{
var result = (element.ValueKind != JsonValueKind.Null) && element.ValueKind != JsonValueKind.Undefined && element.TryGetProperty(name, out var value)
? value
: (JsonElement?)null;
return result;
}
/// <summary>
/// Looks for a property by index in the current JSON array.
/// Returns null if the element is null, undefined, not an array, or the index is out of bounds.
/// </summary>
/// <param name="element">The JSON element to search within.</param>
/// <param name="index">The zero-based index of the element to retrieve.</param>
/// <returns>The JsonElement value if found; otherwise, null.</returns>
public static JsonElement? Get(this JsonElement element, int index)
{
if (element.ValueKind == JsonValueKind.Null || element.ValueKind == JsonValueKind.Undefined)
{
return null;
}
// Return null if index is out of bounds
return index < element.GetArrayLength()
? element[index]
: null;
}
}