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