| | | 1 | | using System.Text.Encodings.Web; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | |
| | | 4 | | namespace ToolCore; |
| | | 5 | | |
| | | 6 | | public static class JsonUtils |
| | | 7 | | { |
| | 2 | 8 | | private static readonly JsonSerializerOptions _jsonOptions = new() |
| | 2 | 9 | | { |
| | 2 | 10 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 2 | 11 | | WriteIndented = true, |
| | 2 | 12 | | PropertyNameCaseInsensitive = true, |
| | 2 | 13 | | Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| | 2 | 14 | | }; |
| | | 15 | | |
| | 2 | 16 | | private static readonly JsonSerializerOptions _compactOptions = new() |
| | 2 | 17 | | { |
| | 2 | 18 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 2 | 19 | | WriteIndented = false, |
| | 2 | 20 | | PropertyNameCaseInsensitive = true, |
| | 2 | 21 | | Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| | 2 | 22 | | }; |
| | | 23 | | |
| | 4 | 24 | | public static string Serialize(object value) => JsonSerializer.Serialize(value, _jsonOptions); |
| | | 25 | | |
| | 32 | 26 | | public static string SerializeCompact(object value) => JsonSerializer.Serialize(value, _compactOptions); |
| | | 27 | | |
| | | 28 | | public static T? Deserialize<T>(string json) |
| | | 29 | | { |
| | | 30 | | try |
| | | 31 | | { |
| | 8 | 32 | | return JsonSerializer.Deserialize<T>(json, _jsonOptions); |
| | | 33 | | } |
| | 2 | 34 | | catch |
| | | 35 | | { |
| | 2 | 36 | | return default; |
| | | 37 | | } |
| | 8 | 38 | | } |
| | | 39 | | |
| | | 40 | | public static IReadOnlyDictionary<string, object> DeserializeParameters(string json) |
| | | 41 | | { |
| | | 42 | | try |
| | | 43 | | { |
| | 10 | 44 | | return JsonSerializer.Deserialize<Dictionary<string, object>>(json, _jsonOptions) ?? []; |
| | | 45 | | } |
| | 2 | 46 | | catch |
| | | 47 | | { |
| | 2 | 48 | | return new Dictionary<string, object>(); |
| | | 49 | | } |
| | 10 | 50 | | } |
| | | 51 | | |
| | | 52 | | public static object? GetValue(this IReadOnlyDictionary<string, object> parameters, string key) |
| | | 53 | | { |
| | 56 | 54 | | return parameters.TryGetValue(key, out var value) ? value : null; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public static string? GetString(this IReadOnlyDictionary<string, object> parameters, string key) |
| | | 58 | | { |
| | 6 | 59 | | return parameters.GetValue(key)?.ToString(); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public static bool GetBool(this IReadOnlyDictionary<string, object> parameters, string key, bool defaultValue = fals |
| | | 63 | | { |
| | 20 | 64 | | var value = parameters.GetValue(key); |
| | 20 | 65 | | return value?.ToString()?.ToLowerInvariant() switch |
| | 20 | 66 | | { |
| | 8 | 67 | | "true" or "1" or "yes" => true, |
| | 8 | 68 | | "false" or "0" or "no" => false, |
| | 4 | 69 | | _ => defaultValue |
| | 20 | 70 | | }; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public static int GetInt(this IReadOnlyDictionary<string, object> parameters, string key, int defaultValue = 0) |
| | | 74 | | { |
| | 20 | 75 | | var value = parameters.GetValue(key); |
| | 26 | 76 | | if (value == null) return defaultValue; |
| | | 77 | | |
| | 14 | 78 | | if (int.TryParse(value.ToString(), out int result)) |
| | 14 | 79 | | return result; |
| | | 80 | | |
| | 0 | 81 | | return defaultValue; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public static Dictionary<string, string>? GetDictionary(this IReadOnlyDictionary<string, object> parameters, string |
| | | 85 | | { |
| | 2 | 86 | | var value = parameters.GetValue(key); |
| | 2 | 87 | | if (value == null) return null; |
| | | 88 | | |
| | | 89 | | try |
| | | 90 | | { |
| | 2 | 91 | | var json = JsonSerializer.Serialize(value, _jsonOptions); |
| | 2 | 92 | | return JsonSerializer.Deserialize<Dictionary<string, string>>(json, _jsonOptions); |
| | | 93 | | } |
| | 0 | 94 | | catch |
| | | 95 | | { |
| | 0 | 96 | | return null; |
| | | 97 | | } |
| | 2 | 98 | | } |
| | | 99 | | |
| | | 100 | | public static T? GetObject<T>(this IReadOnlyDictionary<string, object> parameters, string key) where T : class |
| | | 101 | | { |
| | 2 | 102 | | var value = parameters.GetValue(key); |
| | 4 | 103 | | if (value == null) return null; |
| | | 104 | | |
| | | 105 | | try |
| | | 106 | | { |
| | 0 | 107 | | return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(value, _jsonOptions), _jsonOptions); |
| | | 108 | | } |
| | 0 | 109 | | catch |
| | | 110 | | { |
| | 0 | 111 | | return null; |
| | | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | public static T? GetObject<T>(this JsonElement jsonElement) where T : class |
| | | 116 | | { |
| | | 117 | | try |
| | | 118 | | { |
| | 20 | 119 | | return jsonElement.Deserialize<T>(_jsonOptions); |
| | | 120 | | } |
| | 0 | 121 | | catch |
| | | 122 | | { |
| | 0 | 123 | | return null; |
| | | 124 | | } |
| | 20 | 125 | | } |
| | | 126 | | } |