-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
86 lines (61 loc) · 2.74 KB
/
Extensions.cs
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
using System;
using System.Linq;
using System.Text.Json;
namespace TripItExport
{
public static class Extensions
{
public static bool TryGetPropertyAsString(this JsonElement e, string propertyName, out string result)
{
result = string.Empty;
if (!e.TryGetProperty(propertyName, out JsonElement propertyElement)) return false;
string? r = propertyElement.GetString();
if (r is null) return false;
result = r;
return true;
}
public static bool TryGetPropertyAsULong(this JsonElement e, string propertyName, out ulong result)
{
result = ulong.MinValue;
if (!e.TryGetProperty(propertyName, out JsonElement propertyElement)) return false;
if (!ulong.TryParse(propertyElement.GetString(), out result)) return false;
return true;
}
public static bool TryGetPropertyAsDouble(this JsonElement e, string propertyName, out double result)
{
result = double.MinValue;
if (!e.TryGetProperty(propertyName, out JsonElement propertyElement)) return false;
if (!double.TryParse(propertyElement.GetString(), out result)) return false;
return true;
}
public static bool TryGetPropertyAsInt(this JsonElement e, string propertyName, out int result)
{
result = int.MinValue;
if (!e.TryGetProperty(propertyName, out JsonElement propertyElement)) return false;
if (!int.TryParse(propertyElement.GetString(), out result)) return false;
return true;
}
public static bool TryGetPropertyAsGuid(this JsonElement e, string propertyName, out Guid result)
{
result = Guid.Empty;
if (!e.TryGetProperty(propertyName, out JsonElement propertyElement)) return false;
if (!Guid.TryParse(propertyElement.GetString(), out result)) return false;
return true;
}
public static bool TryGetPropertyAsArray(this JsonElement e, string propertyName, out JsonElement[] result)
{
result = Array.Empty<JsonElement>();
if (!e.TryGetProperty(propertyName, out JsonElement propertyElement)) return false;
switch (propertyElement.ValueKind)
{
case JsonValueKind.Array:
result = propertyElement.EnumerateArray().ToArray();
return true;
case JsonValueKind.Object:
result = new JsonElement[] { propertyElement };
return true;
}
return false;
}
}
}