I have class like this:
public class Pussy {
public readonly int Meows;
[JsonConstructor]
private Pussy() { }
public Pussy(int meows)
{
this.Meows = meows;
}
}
When I'm trying to serialize it with Json.NET, it working fine:
{"Meows":3}
But when deserialize, it's just creating class with Meows
set to 0
.
What's wrong? How to fix it?
Try to use JsonProperty
attribute for readonly fields
[JsonProperty]
public readonly int Meows;
Or JsonConstructor
attribute for non-default ctor.
[JsonConstructor]
public Pussy(int meows)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With