Skip to content
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

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

Closed
serialseb opened this issue May 18, 2024 · 2 comments

Comments

@serialseb
Copy link

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
Copy link

elgonzo commented May 18, 2024

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 #2923 (comment) 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
Copy link
Author

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.

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

No branches or pull requests

2 participants