JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.64k stars 3.24k forks source link

Json.NET deserializes nested types using only the last entry in the nested array #2955

Closed serialseb closed 1 month ago

serialseb commented 2 months ago

ObjectA -> Results[] -> Result -> Vaiable

Only the last variable in the series is taken into account, causing absolue havoc.

I added a full repro of the wrong assertion that passes (all variables are the same) followed by the correct assertions (which fail).

We've had to hotfix by removing json.net alltogether unfortunately, but I hope this helps teh next version fix it, or help me understand where i did something wrong.

Archive.zip

elgonzo commented 2 months ago

Not a bug. However, you are not the first and will not be the last whose intuitive assumptions about Newtonsoft.Json's serialization behavior is in conflict with the actual default behavior of Newtonsoft.Json. See https://github.com/JamesNK/Newtonsoft.Json/issues/2923#issuecomment-1850339803 and also the documentation for JsonSerializerSettings regarding the default serialization setting values (https://www.newtonsoft.com/json/help/html/SerializationSettings.htm#ObjectCreationHandling or https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm).

In essence, you would have to configure the serialization setting ObjectCreationHandling.Replace to match the deserializer behavior with your expectations.


I hope this helps teh next version fix it

That is astronomically unlikely to happen, considering that the solution to this problem is to configure the (de)serializer appropriately, which is easy to do. Changing the default behavior of the (de)serializer, one that is already established for a long time, would be an breaking change; basically a no-go, considering how many (legacy) projects and deployments use Newtonsoft.Json.


We've had to hotfix by removing json.net alltogether unfortunately.

If this implies you migrated to System.Text.Json, then that's in my opinion the best solution by far, and there should be no reason to go back to Newtonsoft.Json.

serialseb commented 1 month ago

I'm very confused becasue this started happening without us realising it, and after so many years i had no idea about the behaviour of ObjectCreationHandling. To be honest we're only switching away because first I never knew that behaviour would express itself in such a way, and i hadn't read https://www.newtonsoft.com/json/help/html/SerializationSettings.htm in as long as i started using your library! My fault on this one. Secondly, we have a custom json-ld serialializer we built on top of Utt8Writer (the non MS one but I understand the current MS one is based on it and maybe has the same author), so it made sense to make the move.

May I suggest that, at least for me as a non-english speaker, https://www.newtonsoft.com/json/help/html/SerializationSettings.htm did not quite make me expect what the bheaviour was, and that I'd be happpy to contribute some content if you have FAQs or elsewhere so it is more obvious that on a nested object reuse would be used even if the content is different? I got also extremely confused by the fact that it used the last one rather than the first one it found.

Either way let me know if I can help. I'll close the bug as invalid as it is properly documented.