Closed pipoon21 closed 5 months ago
Not a bug.
As you already yourself mentioned, to get the desired behavior the ObjectCreationHandling setting has to be set to ObjectCreationHandling.Replace.
It's a bit unfortunate that the default setting for ObjectCreationHandling is to reuse objects and add items to existing collections (as documented) instead of replacing them. If you look around in the issue tracker here, you will find quite a number of issue reports of users being surprised by this default behavior like you, but it is what it is, sadly. This default behavior has been the default since a long time, and changing it would constitute a major breaking change that quite likely is not going to happen because of this reason. (Additionally, over the recent years, development activities regarding this library have been minor now that STJ has become the modern go-to json serializer in .NET)
Possibly related to https://github.com/JamesNK/Newtonsoft.Json/issues/2451 The problem disappear if ObjectCreationHandling = ObjectCreationHandling.Replace is set.
Source/destination types
Expected behavior
First and second time deserializing from the same json string should return the same result.
Actual behavior
The second time OuterClass was deserialized (from the exact same json), multiple NestedClass will appear in NestedClasses property.
Steps to reproduce