I'm using an API that returns a key-value collection as a Dictionary<string, string>
. I need to convert that to a Dictionary<string, object>
. I have a sense that there should be a way to do this conversion/mapping without "manually" looping through each key-value pair, but Googling or the C# object reference didn't immediately yield a solution.
To convert a string to dictionary, we have to ensure that the string contains a valid representation of dictionary. This can be done by eval() function. Abstract Syntax Tree (ast) module of Python has literal_eval() method which safely evaluates valid Python literal structure.
Method 1: Splitting a string to generate key:value pair of the dictionary In this approach, the given string will be analysed and with the use of split() method, the string will be split in such a way that it generates the key:value pair for the creation of a dictionary. Below is the implementation of the approach.
To convert a dictionary to string in Python, use the json. dumps() function. The json. dumps() is a built-in function in json library that can be used by importing the json module into the head of the program.
I found it easy to json serialize the object and deserialize as a dictionary. var json = JsonConvert. SerializeObject(obj); var dictionary = JsonConvert. DeserializeObject<Dictionary<string, string>>(json);
Try the following
var newMap = oldMap.ToDictionary(pair => pair.Key, pair=>(object)pair.Value);
No looping, maps a Dictionary{T, U}
to Dictionary{T, object}
in constant time:
class DictionaryWrapper<T, U> : IDictionary<T, object>
{
readonly Dictionary<T, U> inner;
public DictionaryWrapper(Dictionary<T, U> wrapped)
{
this.inner = wrapped;
}
#region IDictionary<T,object> Members
public void Add(T key, object value) { inner.Add(key, (U)value); }
public bool ContainsKey(T key) { return inner.ContainsKey(key); }
public ICollection<T> Keys { get { return inner.Keys; } }
public bool Remove(T key) { return inner.Remove(key); }
public bool TryGetValue(T key, out object value)
{
U temp;
bool res = inner.TryGetValue(key, out temp);
value = temp;
return res;
}
public ICollection<object> Values { get { return inner.Values.Select(x => (object)x).ToArray(); } }
public object this[T key]
{
get { return inner[key]; }
set { inner[key] = (U)value; }
}
#endregion
#region ICollection<KeyValuePair<T,object>> Members
public void Add(KeyValuePair<T, object> item) { inner.Add(item.Key, (U)item.Value); }
public void Clear() { inner.Clear(); }
public bool Contains(KeyValuePair<T, object> item) { return inner.Contains(new KeyValuePair<T, U>(item.Key, (U)item.Value)); }
public void CopyTo(KeyValuePair<T, object>[] array, int arrayIndex) { throw new NotImplementedException(); }
public int Count { get { return inner.Count; } }
public bool IsReadOnly { get { return false; } }
public bool Remove(KeyValuePair<T, object> item) { return inner.Remove(item.Key); }
#endregion
#region IEnumerable<KeyValuePair<T,object>> Members
public IEnumerator<KeyValuePair<T, object>> GetEnumerator()
{
foreach (var item in inner)
{
yield return new KeyValuePair<T, object>(item.Key, item.Value);
}
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
foreach (var item in inner)
{
yield return new KeyValuePair<T, object>(item.Key, item.Value);
}
}
#endregion
}
With a few more generic params, you can generalize this class further so that it maps a Dictionary{A, B}
to a Dictionary{C, D}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With