Closed benjamin-achouche closed 2 weeks ago
It looks like you are one of many who got surprised by Newtonsoft.Json's default behavior regarding the ObjectCreationHandling
setting (https://www.newtonsoft.com/json/help/html/SerializationSettings.htm#ObjectCreationHandling). (Just search this issue tracker here for any open or closed issue that mentions ObjectCreationHandling, and you'll see how many people got suprised about this like you did.)
For a more detailed explanation of what's going on here, see this comment: https://github.com/JamesNK/Newtonsoft.Json/issues/2706#issuecomment-1183252408
Essentially, to get the desired outcome, you will have to set Newtonsoft.Json's ObjectCreationHandling
setting to ObjectCreationHandling.Replace
.
(Side note: It's probably a good idea to read about all of the serialization settings and their default values/behaviors in the Newtonsoft.Json documentation, because the defaults can sometimes be different from what many would intuitively expect them to be. It would potentially save you from being surprised again by some other default behavior of Newtonsoft.Json.)
Yes that seemed to have fixed it, thank you so much !
Hello,
I'm facing a weird issue with JsonConvert.DeserializeObject.
I'm using it in the context of loading/saving game save files. The issue occurs with this :
dataToLoad and loadedData are supposed to contain the exact same thing, except that one is a json and the other an instance of GameData.
If I make it simple, before the deserialization, the data is :
And after :
Can you see the 3 items in the actionsStages.Shadow.Act.ShadowAct1.missions that have been added before the ones that were already there ? (See how the 4th item's values are the previous's first item's values)
Here's my GameData class (I purposefully removed the other methods, as they are commented anyway) :
And now here's the ActionStageSaveData :
And finally the ActionStageSaveMission class :
Well can you see the
For some reason I can't explain, the quantity of added items is the quantity of "new()" in the new() { }
For example, if I change it to
only 1 item would be added.
EDIT: Here's the full Load method too :
Also, it only happens with this "actionStages.Character.ActionStageType.SceneName.missions" data (here being "actionStages.Shadow.Act.ShadowAct1.missions"). The others work fine. Though this one is the only 3-level nested dictionaries. No idea why it would matter though, nor why it would use the new(), new() thing either...