-
Notifications
You must be signed in to change notification settings - Fork 3.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
Inconsistent Json Converter behaviour #3032
Comments
After some debugging I found out that internally you have contract's cache. During very first deserialization of Hashtable you create contract for it with 'Item' and 'Key' contracts set to 'null', although I would expect that they should not be null. After serialization that contract is updated and has Key- and Item- contracts set to JsonObjectContract. after this update my json converter is called as expected. Current workaround for me to call SerializeObject(new Hashtable{......, is there better way to fix the issue? |
Normally the expectation is that a converter's |
well, dictionary is not a goal of this converter. its goal is find json object and convert it to Hashtable with some extra conversion rules. I do not expect that CanConvert will be called many times for same type. |
If i am not mistaken (but i could be), it looks like you attempted to write a converter for the Write your own contract resolver deriving from protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
var contract = base.CreateDictionaryContract(objectType);
if (objectType == typeof(Dictionary<byte, object>))
{
contract.ItemConverter = new ClosedParametersConverter();
}
return contract;
} Assign the custom contract resolver to the Note that by applying the converter as the items converter of the contract, the converter's |
FYI: I made a quick'n'dirty dotnetfiddle demo with the custom contract resolver i mentioned based on your code example: https://dotnetfiddle.net/VBK7n0 |
@elgonzo got it working with using your proposal. Thank you very much. but had to add case for Hashtable like this:
|
@elgonzo unfortunately proposed solution did not work well in all the cases, so, I had to override array contract creation and object contract creation |
We encountered weired behaviour or Json converter on both .net8 and .netframework4.xxx using Newtonsoft 13.0.3
we have simple class like this and json string for it:
if we call
JsonConvert.DeserializeObject<Ticket>
for jsonString then Converter is called only once for evey dictionary entry.Next I call 'JsonConvert.SerializeObject' for ticket instance and then again
JsonConvert.DeserializeObject<Ticket>
for the same jsonString converter is called also for 'key1', 'key2' and 'key3'.I do not know what is right behaviour here. I would like to see second one. For me it is not clear what I do wrong and get different behaviour although input and serialization settings are same
What should I fix to get it working same way eveytime?
Here is json
Here is simple console app with converter that demonstrating the issue
The text was updated successfully, but these errors were encountered: