Skip to content

Deserialize dictionary key with TypeDescriptor#2568

Closed
Joy-less wants to merge 7 commits intolitedb-org:masterfrom
Joy-less:fix-546
Closed

Deserialize dictionary key with TypeDescriptor#2568
Joy-less wants to merge 7 commits intolitedb-org:masterfrom
Joy-less:fix-546

Conversation

@Joy-less
Copy link
Copy Markdown
Collaborator

BsonDocument only supports string keys. Since Dictionary is serialized as BsonDocument, certain types don't work as keys.

This pull request converts the keys to the correct type when deserializing, using the method in #588.

For example, the key d396d0e8-2763-4ad6-a1c0-f19f254576ec can now be deserialized as a Guid.

This isn't really ideal but it does the job.

Fixes #546 and #2161. Replaces #588.

@JKamsker
Copy link
Copy Markdown
Collaborator

JKamsker commented Dec 1, 2024

Hi @Joy-less

Great job on the changes! Would it be possible to also implement and ensure the following works as expected?

public class DataContainer
{
    public Dictionary<(int, string), string> DataDict { get; set; }
}

Additionally, as mentioned earlier on Discord, reviewing a PR becomes challenging when it includes unrelated changes (e.g., refactor-renames). For future submissions, keeping PRs focused on a single purpose would help simplify the review process.

Thank you for your efforts on this!

@Joy-less
Copy link
Copy Markdown
Collaborator Author

Joy-less commented Dec 1, 2024

Would it be possible to also implement and ensure the following works as expected?

public class DataContainer
{
    public Dictionary<(int, string), string> DataDict { get; set; }
}

This does not work as expected. The key should be serialized as JSON for something complex like this. However, that would break compatibility, so it might be a good idea to assume it's a string key if it fails to deserialize as JSON.

@Joy-less
Copy link
Copy Markdown
Collaborator Author

Joy-less commented Dec 1, 2024

I've changed the pull request to serialize the key as JSON if it's not a string.
One of the tests was failing because of how LiteDB truncates DateTimes to the nearest millisecond. To keep JsonSerializer consistent with BufferWriter, I changed the test so now it accepts the truncated value as well.
All tests passed now.

@JKamsker
Copy link
Copy Markdown
Collaborator

JKamsker commented Dec 1, 2024

@Joy-less
Thanks for the changes!
Can you think of an scenario where the deserialization doesnt work on databases created with a prior (released) version of litedb?

var skey = key.ToString();
string stringKey = entry.Key as string
// Serialize key as JSON to support any key type
?? JsonSerializer.Serialize(Serialize(entry.Key));
Copy link
Copy Markdown
Collaborator

@JKamsker JKamsker Dec 2, 2024

Choose a reason for hiding this comment

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

TupleDict = new()
  {
      [(2, "xxx")] = "test",
  },

from the tests produces

System.ValueTuple`2[[System.Int32, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib

Which doesnt contain any value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InvalidCastException for DeserializeDictionary when Key is of type Guid

2 participants