InvalidCastException for DeserializeDictionary when Key is of type Guid
See original GitHub issueStack trace :
System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at LiteDB.BsonMapper.DeserializeDictionary(Type K, Type T, IDictionary dict, BsonDocument value)
at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
at LiteDB.BsonMapper.DeserializeObject(Type type, Object obj, BsonDocument value)
at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
at LiteDB.BsonMapper.ToObject[T](BsonDocument doc)
at LiteDB.LiteCollection`1.<Find>d__17.MoveNext()
Data Type :
public class QuizData
{
public QuizData()
{ }
public string RollNumber { get; set; }
public Dictionary<Guid,Choice> QuestionChoiceList { get; set; }
}
Method Call :
_db.GetCollection<QuizData>("collectionName").FindAll();
I think it’s throwing because Generic conversion doesn’t support GUIDs.
Platform : ASP.NET Core 1.1.0 on .NET Core 1.1.0 using .NET Core SDK 1.0.0-preview2-1-003177
Edit : I got it working by forking and using a very quick-and-dirty fix in DeserializeDictionary()
Guid temp;
var k = Guid.TryParse(key, out temp) ?
temp : K.GetTypeInfo().IsEnum ?
Enum.Parse(K, key) : Convert.ChangeType(key, K);
So basically right now you can’t have Guid as a TKey in an IDictionary
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
c# - How to serialize a Dictionary<object, guid>?
My main issue is that I want to find a way to return a copy of the Dictionary through the GetProcesses()-method, without returning...
Read more >Deserializing a dictionary with JsonFX
Hi all, I'm trying to serialize a dictionary of type Dictionary ... When I try to type cast these parameters, I get an...
Read more >Migrate from Newtonsoft.Json to System.Text.Json - .NET
Deserializing to a Dictionary<TKey, TValue> where TKey is typed as anything other than string could introduce a security vulnerability in the ...
Read more >Deserialization with System.Text.Json - Marc Roussy
An overview of the many ways of deserializing JSON with System. ... with JSON is as common as working with a language's primitive...
Read more >How can I cast dictionary<string, object> in keyvaluepair ...
You are trying to cast each item from the list to a KeyValuePair<string, object> , which cannot possibly work. There is no way...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Might be better to rename this issue to something like “InvalidCastException for DeserializeDictionary when Key is of non primitive or string type” Since it affects any non string/primitive key for a dictionary
Agree with @prajaybasu here, if some simple modification could expand the usability that would be worth it.
http://stackoverflow.com/questions/393731/generic-conversion-function-doesnt-seem-to-work-with-guids