-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserialize dictionary key with TypeDescriptor #2568
base: master
Are you sure you want to change the base?
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.
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
BsonDocument
only supportsstring
keys. SinceDictionary
is 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-f19f254576ec
can now be deserialized as aGuid
.This isn't really ideal but it does the job.
Fixes #546 and #2161. Replaces #588.