Comments (2)
In case you're wondering why I'm assigning an empty default value in the first place:
- Enable nullable-feature
- Enable all warnings
- Enable "treat all warnings as errors"
- We definitely don't want anyone to instantiate the class manually; Therefore, make the only constructor private without arguments.
- Build fails due to
CS8618
: Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
from newtonsoft.json.
Disclaimer: I am just a user of the library and not associated with the project nor its author/maintainer.
Not a bug.
It's the default for the ObjectCreationHandling setting in action. It's a bit unfortunate that this particular behavior has been chosen the default behavior, because it (unsurprisingly) catches quite a few users by surprise, but that choice had been made quite long ago, and with so many software projects relying on Newtonsoft.Json it is rather unlikely that a default setting will change... :(
In your case, both the FirstContained
and DifferentValue
properties are initialized with the same Contained.Empty
instance. And when Newtonsoft.Json deserializes the json objects for FirstContained
and DifferentValue
, it uses the assigned Contained
instance(s) to assign the value for the DifferentValue
property to it.
In other words, you see the same value for result.FirstContained.DifferentValue
and result.SecondContained.DifferentValue
because result.FirstContained
and result.SecondContained
refer to the same Contained.Empty
instance, and Newtonsoft.Json is reusing (populating) this Contained.Empty
instance instead of creating and assigning new Contained
instances to the FirstContained/SecondContained properties. This means, not only did you see a result you didn't expect, but the Contained.Empty
instance itself got modified by the deserializer, too.
To instruct Newtonsoft.Json to not reuse object instances but replace existing object instances with new ones, either set the [JsonProperty]
attribute's ObjectCreationHandling
property to ObjectCreationHandling.Replace
. Or, if this setting should affect the entire deserialization job, set the ObjectCreationHandling
property in an JsonSerializerSettings
instance and pass this JsonSerializerSettings instance to the (de)serializer.
P.S.: It is probably a good idea to spend a little time studying the default values used for the serialization settings used by the (de)serializer. Because there might be other default settings (and thus default behavior) of Newtonsoft.Json that you might or might not intuitively expect...
from newtonsoft.json.
Related Issues (20)
- MissingMemberHandling.Error doesn't throw when property is set with jsonignore HOT 1
- Object duplication when deserializing class with initial static value assigned. HOT 1
- v13.0.3 Memory Leak due to Reference Loop on .NET 8 HOT 1
- Deserializing JSON with JsonProperty("$type") not working HOT 2
- Deserialization of derived type works only when "$type" is first attribute in JSON object HOT 2
- .NET 9.0 and Binary Formatter being removed HOT 1
- Documentation web site is down HOT 1
- JsonConverter for struct not used for nullable fields when passed through JsonSerializerSettings HOT 2
- Custom Converter fails when both decimal and doubles are present HOT 3
- Json data parsing in MAUI .NET 8
- StringEnumConverter.AllowIntegerValues confusing documentation
- ToString method with Formatting.None returns "null" instead of empty string for null values HOT 2
- Nullable DateTime in Dictionary can't use custom converter to convert into empty string HOT 3
- Can't de-serialise decimals having no leading zero (regression between NewtonSoft.Json 9.0.1 and 10.0.1) HOT 2
- Incorrect parsing of JSONPath query HOT 2
- Opt-out of JTokenType.Guid (and other "custom" JTokenTypes)
- Serialized C# class numeric string is partially converted to float on serialization and deserialization (used to be fine) version 13.0.0.0 HOT 2
- DefaultContractResolver and fluent configuration
- JsonProperty required allowed but not respected on record parameters HOT 2
- Deserializing unclosed object to JObject
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from newtonsoft.json.