| | | 1 | | using System.Collections; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Utilities; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Utility for converting arbitrary .NET objects to a <see cref="Dictionary{TKey, TValue}"/> with string keys and value |
| | | 8 | | /// Handles dictionaries, enumerables, and objects with public properties. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class ObjectToDictionaryConverter |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Converts an arbitrary object to a dictionary with string keys and string values. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="data">The object to convert. Can be a dictionary, enumerable, or any object with public properties. |
| | | 16 | | /// <returns>A <see cref="Dictionary{TKey, TValue}"/> with string keys and string values.</returns> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// Conversion behavior: |
| | | 19 | | /// - <see cref="IDictionary"/>: Converts keys and values to strings. |
| | | 20 | | /// - <see cref="IEnumerable"/> (excluding strings): Creates indexed keys like "item[0]", "item[1]", etc. |
| | | 21 | | /// - Other objects: Extracts public properties as key/value pairs. |
| | | 22 | | /// </remarks> |
| | | 23 | | public static Dictionary<string, string> ToDictionary(object? data) |
| | | 24 | | { |
| | 17 | 25 | | return data switch |
| | 17 | 26 | | { |
| | 1 | 27 | | null => [], |
| | 8 | 28 | | IDictionary dictionary => FromDictionaryToString(dictionary), |
| | 7 | 29 | | IEnumerable enumerable when data is not string => FromEnumerableToString(enumerable), |
| | 5 | 30 | | _ => FromObjectPropertiesToString(data) |
| | 17 | 31 | | }; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Converts an arbitrary object to a dictionary with string keys and object values (without stringification). |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="data">The object to convert. Can be a dictionary, enumerable, or any object with public properties. |
| | | 38 | | /// <returns>A <see cref="Dictionary{TKey, TValue}"/> with string keys and object values.</returns> |
| | | 39 | | /// <remarks> |
| | | 40 | | /// Conversion behavior mirrors <see cref="ToDictionary"/>, but preserves original types instead of converting to st |
| | | 41 | | /// </remarks> |
| | | 42 | | public static Dictionary<string, object?> ToDictionaryObject(object? data) |
| | | 43 | | { |
| | 4 | 44 | | return data switch |
| | 4 | 45 | | { |
| | 0 | 46 | | null => [], |
| | 1 | 47 | | IDictionary dictionary => FromDictionaryToObject(dictionary), |
| | 2 | 48 | | IEnumerable enumerable when data is not string => FromEnumerableToObject(enumerable), |
| | 2 | 49 | | _ => FromObjectPropertiesToObject(data) |
| | 4 | 50 | | }; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private static Dictionary<string, string> FromDictionaryToString(IDictionary source) |
| | | 54 | | { |
| | 8 | 55 | | var dict = new Dictionary<string, string>(); |
| | | 56 | | |
| | 54 | 57 | | foreach (DictionaryEntry entry in source) |
| | | 58 | | { |
| | 19 | 59 | | var key = entry.Key?.ToString(); |
| | 19 | 60 | | if (string.IsNullOrWhiteSpace(key)) |
| | | 61 | | { |
| | | 62 | | continue; |
| | | 63 | | } |
| | | 64 | | |
| | 19 | 65 | | dict[key] = entry.Value?.ToString() ?? string.Empty; |
| | | 66 | | } |
| | | 67 | | |
| | 8 | 68 | | return dict; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | private static Dictionary<string, string> FromEnumerableToString(IEnumerable source) |
| | | 72 | | { |
| | 3 | 73 | | var dict = new Dictionary<string, string>(); |
| | 3 | 74 | | var index = 0; |
| | | 75 | | |
| | 18 | 76 | | foreach (var item in source) |
| | | 77 | | { |
| | 6 | 78 | | dict[$"item[{index}]"] = item?.ToString() ?? string.Empty; |
| | 6 | 79 | | index++; |
| | | 80 | | } |
| | | 81 | | |
| | 3 | 82 | | return dict; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private static Dictionary<string, string> FromObjectPropertiesToString(object source) |
| | | 86 | | { |
| | 5 | 87 | | var dict = new Dictionary<string, string>(); |
| | 5 | 88 | | var properties = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Ignor |
| | | 89 | | |
| | 36 | 90 | | foreach (var prop in properties) |
| | | 91 | | { |
| | 13 | 92 | | dict[prop.Name] = GetPropertyStringValue(prop, source); |
| | | 93 | | } |
| | | 94 | | |
| | 5 | 95 | | return dict; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | private static string GetPropertyStringValue(PropertyInfo property, object source) |
| | | 99 | | { |
| | | 100 | | try |
| | | 101 | | { |
| | 13 | 102 | | return property.GetValue(source)?.ToString() ?? string.Empty; |
| | | 103 | | } |
| | 2 | 104 | | catch |
| | | 105 | | { |
| | 2 | 106 | | return string.Empty; |
| | | 107 | | } |
| | 13 | 108 | | } |
| | | 109 | | |
| | | 110 | | private static Dictionary<string, object?> FromDictionaryToObject(IDictionary source) |
| | | 111 | | { |
| | 1 | 112 | | var dict = new Dictionary<string, object?>(); |
| | | 113 | | |
| | 10 | 114 | | foreach (DictionaryEntry entry in source) |
| | | 115 | | { |
| | 4 | 116 | | var key = entry.Key?.ToString(); |
| | 4 | 117 | | if (string.IsNullOrWhiteSpace(key)) |
| | | 118 | | { |
| | | 119 | | continue; |
| | | 120 | | } |
| | | 121 | | |
| | 4 | 122 | | dict[key] = entry.Value; |
| | | 123 | | } |
| | | 124 | | |
| | 1 | 125 | | return dict; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | private static Dictionary<string, object?> FromEnumerableToObject(IEnumerable source) |
| | | 129 | | { |
| | 1 | 130 | | var dict = new Dictionary<string, object?>(); |
| | 1 | 131 | | var index = 0; |
| | | 132 | | |
| | 10 | 133 | | foreach (var item in source) |
| | | 134 | | { |
| | 4 | 135 | | dict[$"item[{index}]"] = item; |
| | 4 | 136 | | index++; |
| | | 137 | | } |
| | | 138 | | |
| | 1 | 139 | | return dict; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | private static Dictionary<string, object?> FromObjectPropertiesToObject(object source) |
| | | 143 | | { |
| | 2 | 144 | | var dict = new Dictionary<string, object?>(); |
| | 2 | 145 | | var properties = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Ignor |
| | | 146 | | |
| | 14 | 147 | | foreach (var prop in properties) |
| | | 148 | | { |
| | 5 | 149 | | dict[prop.Name] = GetPropertyObjectValue(prop, source); |
| | | 150 | | } |
| | | 151 | | |
| | 2 | 152 | | return dict; |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | private static object? GetPropertyObjectValue(PropertyInfo property, object source) |
| | | 156 | | { |
| | | 157 | | try |
| | | 158 | | { |
| | 5 | 159 | | return property.GetValue(source); |
| | | 160 | | } |
| | 1 | 161 | | catch |
| | | 162 | | { |
| | 1 | 163 | | return null; |
| | | 164 | | } |
| | 5 | 165 | | } |
| | | 166 | | } |