-
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
Class static field of same type is overridden during de-serialization #3011
Comments
Solution to your problem is pretty simple: Configure the deserializer with the You will find quite a few similar issue reports here that are caused by the same default value/behavior of Newtonsoft.Json regarding its On top of that, Newtonsoft.Json isn't really aware of the So, if you are deserializing a It's technically not a bug but still very unfortunate default behavior that quite a few unsuspecting users were and are surprised of. I am not involved in maintaining Newtonsoft.Json, but in my frank personal opinion, the default behavior of the deserializer should have been "Replace" from the get go, but considering Newtonsoft.Json's age and wide-spread install base, it's unfortunately much too late for a change of such a default behavior to not risk breaking countless deployments out there in the world. |
hello @elgonzo if I got it right this behavior emerged due to this line which has already assigned some instance to that property before even de-serialization kicks in: public Encoding Encoding { get; init; } = Encoding.Default; so de-serializer just reuse that instance (in our case |
Yup, exactly like that. |
Hello,
I've encountered a strange behavior when de-serializing an instance of a type which has a static field of type same as the enclosing type.
Note I haven't applied any attributes or fancy settings, I only serialize -> deserialize.
Below is simple code demonstrating the issue.
Any help or workaround is appreciated,
Thanks,
The text was updated successfully, but these errors were encountered: