Skip to content

Commit

Permalink
Fix Dictionary deserialization when TKey is Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
lbnascimento committed Apr 15, 2020
1 parent b79a856 commit 4fe3afc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions LiteDB.Tests/Mapper/Dictionary_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,17 @@ public void Serialize_Hashtable()
Assert.Single(result);
Assert.Equal(1, result["x"].AsInt32);
}

[Fact]
public void Deserialize_Uri()
{
var dict = new Dictionary<Uri, string>();
dict.Add(new Uri("http://www.litedb.org/"), "LiteDB website");
var doc = _mapper.Serialize(dict).AsDocument;
var dict2 = _mapper.Deserialize<Dictionary<Uri, string>>(doc);

Assert.Single(dict2);
Assert.Equal(dict.Keys.Single(), dict2.Keys.Single());
}
}
}
2 changes: 1 addition & 1 deletion LiteDB/Client/Mapper/BsonMapper.Deserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private void DeserializeDictionary(Type K, Type T, IDictionary dict, BsonDocumen
var isKEnum = K.GetTypeInfo().IsEnum;
foreach (var el in value.GetElements())
{
var k = isKEnum ? Enum.Parse(K, el.Key) : Convert.ChangeType(el.Key, K);
var k = isKEnum ? Enum.Parse(K, el.Key) : K == typeof(Uri) ? new Uri(el.Key) : Convert.ChangeType(el.Key, K);
var v = this.Deserialize(T, el.Value);

dict.Add(k, v);
Expand Down

1 comment on commit 4fe3afc

@lbnascimento
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for #1630

Please sign in to comment.