Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 4086577

Browse files
committed
Extend ToStringDictionary to support Object Dictionaries
1 parent ef0d0dc commit 4086577

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/ServiceStack.Text/TypeSerializer.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,21 @@ public static object DeserializeFromStream(Type type, Stream stream)
204204
/// Useful extension method to get the Dictionary[string,string] representation of any POCO type.
205205
/// </summary>
206206
/// <returns></returns>
207-
public static Dictionary<string, string> ToStringDictionary<T>(this T obj)
207+
public static Dictionary<string, string> ToStringDictionary(this object obj)
208208
{
209+
if (obj == null)
210+
return new Dictionary<string, string>();
211+
212+
if (obj is Dictionary<string, object> objDictionary)
213+
{
214+
var to = new Dictionary<string,string>();
215+
foreach (var entry in objDictionary)
216+
{
217+
to[entry.Key] = entry.Value?.ToString();
218+
}
219+
return to;
220+
}
221+
209222
var jsv = SerializeToString(obj);
210223
var map = DeserializeFromString<Dictionary<string, string>>(jsv);
211224
return map;

0 commit comments

Comments
 (0)