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