question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InvalidCastException for DeserializeDictionary when Key is of type Guid

See original GitHub issue

Stack 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:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
PassiveModdingcommented, Oct 21, 2019

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

1reaction
xied75commented, Mar 24, 2017

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found