| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | using System.Collections; |
| | 13 | | using System.Globalization; |
| | 14 | | using System.Text.Json; |
| | 15 | |
|
| | 16 | | namespace LeetCode.Core.Helpers; |
| | 17 | |
|
| | 18 | | public static class JsonHelper<T> |
| | 19 | | { |
| 19 | 20 | | private static readonly Type TargetType = typeof(T); |
| | 21 | |
|
| | 22 | | public static T Parse(string json) |
| 3861 | 23 | | { |
| 3861 | 24 | | using var jsonDocument = JsonDocument.Parse(json, JsonHelperOptions.JsonDocumentOptions); |
| | 25 | |
|
| 3861 | 26 | | var result = DeserializeElement(jsonDocument.RootElement, TargetType); |
| | 27 | |
|
| 3861 | 28 | | return result switch |
| 3861 | 29 | | { |
| 4 | 30 | | null when !typeof(T).IsValueType || Nullable.GetUnderlyingType(typeof(T)) is not null => default!, |
| 3859 | 31 | | T t => t, |
| 0 | 32 | | _ => throw new JsonException($"JsonHelper<{TargetType.Name}>: could not convert JSON to {TargetType}.") |
| 3861 | 33 | | }; |
| 3861 | 34 | | } |
| | 35 | |
|
| | 36 | | private static object? DeserializeElement(JsonElement jsonElement, Type type) |
| 25820 | 37 | | { |
| 25820 | 38 | | if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) |
| 3523 | 39 | | { |
| 3523 | 40 | | if (jsonElement.ValueKind == JsonValueKind.Null) |
| 787 | 41 | | { |
| 787 | 42 | | return null; |
| | 43 | | } |
| | 44 | |
|
| 2736 | 45 | | type = Nullable.GetUnderlyingType(type)!; |
| 2736 | 46 | | } |
| | 47 | |
|
| 25033 | 48 | | if (type == typeof(object)) |
| 249 | 49 | | { |
| 249 | 50 | | return ConvertElement(jsonElement); |
| | 51 | | } |
| | 52 | |
|
| 24784 | 53 | | if (IsPrimitiveType(type)) |
| 19164 | 54 | | { |
| 19164 | 55 | | return ConvertToPrimitive(jsonElement, type); |
| | 56 | | } |
| | 57 | |
|
| 5620 | 58 | | if (type.IsArray) |
| 5326 | 59 | | { |
| 5326 | 60 | | return DeserializeArray(jsonElement, type.GetElementType()!); |
| | 61 | | } |
| | 62 | |
|
| 294 | 63 | | if (IsListType(type, out var itemType)) |
| 0 | 64 | | { |
| 0 | 65 | | return DeserializeList(jsonElement, type, itemType); |
| | 66 | | } |
| | 67 | |
|
| 294 | 68 | | if (IsDictionaryType(type, out var valueType)) |
| 0 | 69 | | { |
| 0 | 70 | | return DeserializeDictionary(jsonElement, type, valueType); |
| | 71 | | } |
| | 72 | |
|
| 294 | 73 | | return JsonSerializer.Deserialize(jsonElement.GetRawText(), type, JsonHelperOptions.JsonSerializerOptions) ?? |
| 294 | 74 | | throw new JsonException($"Unable to deserialize JSON to {type}."); |
| 25820 | 75 | | } |
| | 76 | |
|
| | 77 | | private static bool IsPrimitiveType(Type type) |
| 24784 | 78 | | { |
| 24784 | 79 | | return type == typeof(string) || |
| 24784 | 80 | | type == typeof(bool) || |
| 24784 | 81 | | type == typeof(int) || |
| 24784 | 82 | | type == typeof(long) || |
| 24784 | 83 | | type == typeof(double) || |
| 24784 | 84 | | type == typeof(decimal); |
| 24784 | 85 | | } |
| | 86 | |
|
| | 87 | | private static object? ConvertToPrimitive(JsonElement jsonElement, Type type) |
| 19164 | 88 | | { |
| 19164 | 89 | | return type switch |
| 19164 | 90 | | { |
| 20390 | 91 | | _ when type == typeof(string) => jsonElement.GetString(), |
| 18006 | 92 | | _ when type == typeof(bool) => jsonElement.GetBoolean(), |
| 35649 | 93 | | _ when type == typeof(int) => jsonElement.GetInt32(), |
| 92 | 94 | | _ when type == typeof(long) => jsonElement.GetInt64(), |
| 180 | 95 | | _ when type == typeof(double) => jsonElement.GetDouble(), |
| 0 | 96 | | _ when type == typeof(decimal) => jsonElement.GetDecimal(), |
| 0 | 97 | | _ => Convert.ChangeType(ConvertElement(jsonElement), type, CultureInfo.InvariantCulture) |
| 19164 | 98 | | }; |
| 19164 | 99 | | } |
| | 100 | |
|
| | 101 | | private static Array DeserializeArray(JsonElement jsonElement, Type type) |
| 5326 | 102 | | { |
| 5326 | 103 | | if (jsonElement.ValueKind != JsonValueKind.Array) |
| 0 | 104 | | { |
| 0 | 105 | | throw new JsonException($"Expected JSON array for {type}[]"); |
| | 106 | | } |
| | 107 | |
|
| 5326 | 108 | | var items = jsonElement.EnumerateArray() |
| 21959 | 109 | | .Select(item => DeserializeElement(item, type)) |
| 5326 | 110 | | .ToArray(); |
| | 111 | |
|
| 5326 | 112 | | var array = Array.CreateInstance(type, items.Length); |
| | 113 | |
|
| 54570 | 114 | | for (var i = 0; i < items.Length; i++) |
| 21959 | 115 | | { |
| 21959 | 116 | | array.SetValue(items[i], i); |
| 21959 | 117 | | } |
| | 118 | |
|
| 5326 | 119 | | return array; |
| 5326 | 120 | | } |
| | 121 | |
|
| | 122 | | private static IList DeserializeList(JsonElement jsonElement, Type listType, Type itemType) |
| 0 | 123 | | { |
| 0 | 124 | | if (jsonElement.ValueKind != JsonValueKind.Array) |
| 0 | 125 | | { |
| 0 | 126 | | throw new JsonException($"Expected JSON array for {listType.Name}"); |
| | 127 | | } |
| | 128 | |
|
| 0 | 129 | | var list = (IList)Activator.CreateInstance(listType)!; |
| | 130 | |
|
| 0 | 131 | | foreach (var item in jsonElement.EnumerateArray()) |
| 0 | 132 | | { |
| 0 | 133 | | list.Add(DeserializeElement(item, itemType)); |
| 0 | 134 | | } |
| | 135 | |
|
| 0 | 136 | | return list; |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | private static IDictionary DeserializeDictionary(JsonElement jsonElement, Type dictionaryType, Type valueType) |
| 0 | 140 | | { |
| 0 | 141 | | if (jsonElement.ValueKind != JsonValueKind.Object) |
| 0 | 142 | | { |
| 0 | 143 | | throw new JsonException($"Expected JSON object for {dictionaryType.Name}"); |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | var dictionary = (IDictionary)Activator.CreateInstance(dictionaryType)!; |
| | 147 | |
|
| 0 | 148 | | foreach (var property in jsonElement.EnumerateObject()) |
| 0 | 149 | | { |
| 0 | 150 | | var value = DeserializeElement(property.Value, valueType); |
| | 151 | |
|
| 0 | 152 | | dictionary.Add(property.Name, value); |
| 0 | 153 | | } |
| | 154 | |
|
| 0 | 155 | | return dictionary; |
| 0 | 156 | | } |
| | 157 | |
|
| | 158 | | private static object? ConvertElement(JsonElement jsonElement) |
| 249 | 159 | | { |
| 249 | 160 | | return jsonElement.ValueKind switch |
| 249 | 161 | | { |
| 0 | 162 | | JsonValueKind.Object => jsonElement.EnumerateObject() |
| 0 | 163 | | .ToDictionary(p => p.Name, p => ConvertElement(p.Value)), |
| 0 | 164 | | JsonValueKind.Array => jsonElement.EnumerateArray() |
| 0 | 165 | | .Select(ConvertElement) |
| 0 | 166 | | .ToArray(), |
| 43 | 167 | | JsonValueKind.String => jsonElement.GetString(), |
| 174 | 168 | | JsonValueKind.Number => jsonElement.TryGetInt32(out var i) ? i |
| 174 | 169 | | : jsonElement.TryGetInt64(out var l) ? l |
| 174 | 170 | | : jsonElement.TryGetDecimal(out var d) ? d |
| 174 | 171 | | : jsonElement.GetDouble(), |
| 22 | 172 | | JsonValueKind.True => true, |
| 10 | 173 | | JsonValueKind.False => false, |
| 0 | 174 | | JsonValueKind.Null => null, |
| 0 | 175 | | JsonValueKind.Undefined => throw new JsonException( |
| 0 | 176 | | "Encountered undefined JSON element—likely a missing property or uninitialized JsonElement"), |
| 0 | 177 | | _ => throw new NotSupportedException($"Unsupported JSON kind: {jsonElement.ValueKind}") |
| 249 | 178 | | }; |
| 249 | 179 | | } |
| | 180 | |
|
| | 181 | | private static bool IsListType(Type type, out Type itemType) |
| 294 | 182 | | { |
| 294 | 183 | | var iListType = type.GetInterfaces() |
| 7716 | 184 | | .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>)); |
| | 185 | |
|
| 294 | 186 | | if (iListType != null) |
| 0 | 187 | | { |
| 0 | 188 | | itemType = iListType.GetGenericArguments()[0]; |
| | 189 | |
|
| 0 | 190 | | return true; |
| | 191 | | } |
| | 192 | |
|
| 294 | 193 | | itemType = null!; |
| | 194 | |
|
| 294 | 195 | | return false; |
| 294 | 196 | | } |
| | 197 | |
|
| | 198 | | private static bool IsDictionaryType(Type type, out Type valueType) |
| 294 | 199 | | { |
| 294 | 200 | | var iDictionaryType = type.GetInterfaces() |
| 7716 | 201 | | .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)); |
| | 202 | |
|
| 294 | 203 | | if (iDictionaryType != null) |
| 0 | 204 | | { |
| 0 | 205 | | var args = iDictionaryType.GetGenericArguments(); |
| | 206 | |
|
| 0 | 207 | | valueType = args[1]; |
| | 208 | |
|
| 0 | 209 | | return true; |
| | 210 | | } |
| | 211 | |
|
| 294 | 212 | | valueType = null!; |
| | 213 | |
|
| 294 | 214 | | return false; |
| 294 | 215 | | } |
| | 216 | | } |