-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathJsonElementExtensions.cs
More file actions
88 lines (79 loc) · 3.03 KB
/
JsonElementExtensions.cs
File metadata and controls
88 lines (79 loc) · 3.03 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Text.Json;
namespace autoShell;
/// <summary>
/// Extension methods for <see cref="JsonElement"/> to simplify reading optional properties.
/// Provides null-safe accessors similar to Newtonsoft's <c>Value<T></c> method.
/// </summary>
internal static class JsonElementExtensions
{
/// <summary>
/// Returns the string value of <paramref name="propertyName"/>,
/// or <paramref name="defaultValue"/> if the property is missing or not a string.
/// </summary>
public static string GetStringOrDefault(this JsonElement element, string propertyName, string defaultValue = null)
{
return element.TryGetProperty(propertyName, out JsonElement value) && value.ValueKind == JsonValueKind.String
? value.GetString()
: defaultValue;
}
/// <summary>
/// Returns the integer value of <paramref name="propertyName"/>,
/// or <paramref name="defaultValue"/> if the property is missing or not a number.
/// </summary>
public static int GetIntOrDefault(this JsonElement element, string propertyName, int defaultValue = 0)
{
return element.TryGetProperty(propertyName, out JsonElement value) && value.ValueKind == JsonValueKind.Number
? value.GetInt32()
: defaultValue;
}
/// <summary>
/// Returns the boolean value of <paramref name="propertyName"/>,
/// or <paramref name="defaultValue"/> if the property is missing or not a boolean.
/// </summary>
public static bool GetBoolOrDefault(this JsonElement element, string propertyName, bool defaultValue = false)
{
if (element.TryGetProperty(propertyName, out JsonElement value))
{
if (value.ValueKind == JsonValueKind.True)
{
return true;
}
if (value.ValueKind == JsonValueKind.False)
{
return false;
}
}
return defaultValue;
}
/// <summary>
/// Returns the integer value of <paramref name="propertyName"/>,
/// or <c>null</c> if the property is missing or not a number.
/// </summary>
public static int? GetNullableInt(this JsonElement element, string propertyName)
{
return element.TryGetProperty(propertyName, out JsonElement value) && value.ValueKind == JsonValueKind.Number
? value.GetInt32()
: null;
}
/// <summary>
/// Returns the boolean value of <paramref name="propertyName"/>,
/// or <c>null</c> if the property is missing or not a boolean.
/// </summary>
public static bool? GetNullableBool(this JsonElement element, string propertyName)
{
if (element.TryGetProperty(propertyName, out JsonElement value))
{
if (value.ValueKind == JsonValueKind.True)
{
return true;
}
if (value.ValueKind == JsonValueKind.False)
{
return false;
}
}
return null;
}
}