-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
35 lines (27 loc) · 1.27 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
using System.Text.Json;
static public class Extensions
{
static public JsonSerializerOptions defaultSerializerOptions = new JsonSerializerOptions { WriteIndented = false };
static public string GetJsonString<T>(this T o, JsonSerializerOptions options = null)
{
return JsonSerializer.Serialize<T>(o, options ?? defaultSerializerOptions);
}
static public T ParseJson<T>(this string json, JsonSerializerOptions options = null)
{
return JsonSerializer.Deserialize<T>(json, options ?? defaultSerializerOptions);
}
static public string Join<T>(this IEnumerable<T> data, string separator = ",")
{
return string.Join(separator, data.Select(x => x?.ToString()).ToArray());
}
static public string ToUnixEOL(this string line) => line?.Replace("\r\n", "\n");
static public async Task<string> ReadResponse(this Task<HttpResponseMessage> task)
{
return await (await task).Content.ReadAsStringAsync();
}
static public async Task<T> ReadResponse<T>(this HttpResponseMessage message)
{
return (await message.Content.ReadAsStringAsync()).ParseJson<T>();
}
static public TimeSpan TimeSince(this DateTime timestamp) => DateTime.UtcNow - timestamp.ToUniversalTime();
}