-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExtensionMethods.cs
136 lines (110 loc) · 4.7 KB
/
ExtensionMethods.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
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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PrimeView.Entities;
using System;
using System.Text.Json;
namespace PrimeView.JsonFileReader
{
public static class ExtensionMethods
{
private static readonly JsonSerializerOptions serializerOptions = new()
{
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true
};
public static int GetStableHashCode(this string str)
{
unchecked
{
int hash1 = 5381;
int hash2 = hash1;
for (int i = 0; i < str.Length && str[i] != '\0'; i += 2)
{
hash1 = ((hash1 << 5) + hash1) ^ str[i];
if (i == str.Length - 1 || str[i + 1] == '\0')
{
break;
}
hash2 = ((hash2 << 5) + hash2) ^ str[i + 1];
}
return hash1 + (hash2 * 1566083941);
}
}
public static IServiceCollection AddJsonFileReportReader(this IServiceCollection serviceCollection, string baseAddress, IConfiguration configuration)
{
return serviceCollection.AddScoped<IReportReader>(sp => new ReportReader(baseAddress, configuration));
}
public static T? Get<T>(this JsonElement element)
{
try
{
return JsonSerializer.Deserialize<T>(element.GetRawText(), serializerOptions);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return default;
}
public static T? Get<T>(this JsonElement element, string propertyName) where T : class
{
return GetElement(element, propertyName)?.Get<T>();
}
public static T? Get<T>(this JsonElement? element, string propertyName) where T : class
{
return element.HasValue ? Get<T>(element.Value, propertyName) : null;
}
public static int? GetInt32(this JsonElement? element, string propertyName)
{
return element.HasValue ? GetInt32(element.Value, propertyName) : null;
}
public static int? GetInt32(this JsonElement element, string propertyName)
{
var childElement = GetElement(element, propertyName);
return childElement.HasValue && childElement.Value.TryGetInt32(out int value) ? value : null;
}
public static long? GetInt64(this JsonElement? element, string propertyName)
{
return element.HasValue ? GetInt64(element.Value, propertyName) : null;
}
public static long? GetInt64(this JsonElement element, string propertyName)
{
var childElement = GetElement(element, propertyName);
return childElement.HasValue && childElement.Value.TryGetInt64(out long value) ? value : null;
}
public static double? GetDouble(this JsonElement? element, string propertyName)
{
return element.HasValue ? GetDouble(element.Value, propertyName) : null;
}
public static double? GetDouble(this JsonElement element, string propertyName)
{
var childElement = GetElement(element, propertyName);
return childElement.HasValue && childElement.Value.TryGetDouble(out double value) ? value : null;
}
public static string? GetString(this JsonElement? element, string propertyName)
{
return element.HasValue ? GetString(element.Value, propertyName) : null;
}
public static string? GetString(this JsonElement element, string propertyName)
{
return GetElement(element, propertyName)?.GetString();
}
public static DateTime? GetDateFromUnixTimeSeconds(this JsonElement? element, string propertyName)
{
return element.HasValue ? GetDateFromUnixTimeSeconds(element.Value, propertyName) : null;
}
public static DateTime? GetDateFromUnixTimeSeconds(this JsonElement element, string propertyName)
{
int? seconds = GetInt32(element, propertyName);
return seconds.HasValue ? DateTimeOffset.FromUnixTimeSeconds(seconds.Value).DateTime : null;
}
public static JsonElement? GetElement(this JsonElement element, string propertyName)
{
return element.TryGetProperty(propertyName, out var childElement) ? childElement : null;
}
public static JsonElement? GetElement(this JsonElement? element, string propertyName)
{
return element.HasValue ? GetElement(element.Value, propertyName) : null;
}
}
}