Deserialize dictionary key with TypeDescriptor#2568
Deserialize dictionary key with TypeDescriptor#2568Joy-less wants to merge 7 commits intolitedb-org:masterfrom
Conversation
|
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! |
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. |
Otherwise the BufferWriter is not the same as the JsonSerializer.
|
I've changed the pull request to serialize the key as JSON if it's not a string. |
|
@Joy-less |
| var skey = key.ToString(); | ||
| string stringKey = entry.Key as string | ||
| // Serialize key as JSON to support any key type | ||
| ?? JsonSerializer.Serialize(Serialize(entry.Key)); |
There was a problem hiding this comment.
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.CoreLibWhich doesnt contain any value
BsonDocumentonly supportsstringkeys. SinceDictionaryis serialized asBsonDocument, 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-f19f254576eccan now be deserialized as aGuid.This isn't really ideal but it does the job.
Fixes #546 and #2161. Replaces #588.