-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathJsonElementHelper.cs
More file actions
32 lines (28 loc) · 1.1 KB
/
JsonElementHelper.cs
File metadata and controls
32 lines (28 loc) · 1.1 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
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace WSLAttachSwitch
{
internal static class JsonElementHelper
{
public static JsonElement GetPropertyCaseInsensitive(this JsonElement jsonElement, string propertyName)
{
if (jsonElement.TryGetProperty(propertyName, out JsonElement jsonProperty))
{
return jsonProperty;
}
else
{
//On some machines (not sure depending on what factor exactly), the property names within the Hyper-V API response come entirely upper cased (opposed to the provided schema), therefore the fallback here:
foreach (JsonProperty property in jsonElement.EnumerateObject())
{
if (property.Name.Equals(propertyName, StringComparison.InvariantCultureIgnoreCase))
{
return property.Value;
}
}
}
throw new KeyNotFoundException($"Property '{propertyName}' not found in JSON element.");
}
}
}