Newtonsoft Json not deserializing with some property/constructor names
Hi, I'm using newtonsoft json to serialize and deserialize my classes. Everything works perfect until I put the name ID, Identifier, UniqueIdentifier anywhere in my class or Constructor which is to be deserialized. For example:
This serializes perfectly but when I try to deserialize it, the Identifier is null. If I change the name to something else, for example
key
It will work and deserialize perfectly...
Anyone knows the reason for it?
Here's what I receive and try to deserialize:
And here's how I'm deserializing it:
If anyone have any good explanation for it, please let me know.29 Replies
probably need a default constructor for newtonsoft to use
Any specific explanation for it? Since position and rotation is perfectly deserialized just that id is null...
Any particular reason you're using Newtonsoft in the first place, btw?
To Serialize and Deserialize for sending over the network... 🤔
Yeah, but .NET has built-in Json serialization now
Unless you're using an old version of the framework or something
I can't change it now, I've published 13 Minigames with this, changing it now would be a headache
Ah, it's Unity?
Yes
Then it falls under "old version of the framework"
Carry on
😅
Your constructor parameters are named differently from your properties, and there is no parameterless constructor
yup, serializers like System.Text.Json and JSON.Net are looking for a
public parameter-less constructor
https://stackoverflow.com/questions/23017716/json-net-how-to-deserialize-without-using-the-default-constructorStack Overflow
JSON.net: how to deserialize without using the default constructor?
I have a class that has a default constructor and also an overloaded constructor that takes in a set of parameters. These parameters match to fields on the object and are assigned on construction....
So just adding a empty constructor would fix the problem? What about position, rotation? They are being deserialized as intended...
respectfully, that aint true
there are no properties or fields here for rotation
so it cant possibly deserialize properly
It was an example I just write in Discord, Lemme show the actual class:
assuming all your properties have public setters, it should be fine with a blank parameterless constructor, yes
or you can specify the constructor with the
JsonConstructorAttribute
I've changed the name to
key
here because it was working 😅
I was actually trying to find the answer for this behaviour. I didn't find any answer for it on google that if we name our properties like this, it won't work.If you changed
to
then you might be looking for
CamelCasePropertyNamesContractResolver
private set
isnt the same as set
thats why Key
isnt deserializing properly
if it worked, its because your constructor takes a key
parameter and newtonsoft figures out that probably maps to the Key
propertyHmmm, I'll try this. I guess I tried it with public setter too.
I'll come again with more data : )
If you have a really good reason why Key is
private set
you can use PrivateSetterContractResolver()
, to deserilize privateswith a public setter and a parameterless constructor, I see no reason why this shouldnt work
No, don't have any reason for it. I guess I Just accidently set it private while I was trying different things to find any answer
been there. Auto-complete will do that to ya.
Yeah : )
I'll try it again. I'm sure it didn't work with public setter too. Will come here after confirming 🙂
Anyways, Thank You!
IMO all you had to do was place [JsonProperty("UniqueID")] on the constructor id parameter 😄
Could workaround but wanted to know "Why" 😅
Guys I just confirmed, it was an issue with constructor and had nothing to do with name as I was thinking.
This worked perfectly fine:
Thank You so much 🫶