Reputation: 1746
I'm using the json deserialiser by newtonsoft, and I am deserialising this object inside a wrapper:
public class User
{
public string Username;
public byte[] HashedPassword;
public byte[] Salt;
private bool admin;
public bool Admin
{
get { return admin; }
}
public User(string UsernameArg, byte[] PasswordArg, byte[] SaltArg, bool AdminArg = false)
{
Username = UsernameArg;
HashedPassword = PasswordArg;
Salt = SaltArg;
admin = AdminArg;
}
public override string ToString()
{
return Username;
}
}
This is my json string:
{"Users":[{"Username":"admin","HashedPassword":"password","Salt":"salt","Admin":true}]}
(I've edited the hashed password and the salt for readability)
So whenever I read this using JsonConvert.DeserializeObject<UserDatabaseClass>(jsonRead)
the admin field is returning false.
Is it just that I have misunderstood what this is doing here or am I doing it incorrectly?
Upvotes: 2
Views: 3081
Reputation: 100545
Since your Admin
property is read-only - {get {...}}
- serializer can't set it to any value and will skip it.
Fix: make field read-write, i.e. using automatic properties
public bool Admin {get;set;}
Also you may need no-argument constructor for deserialization to work or use [JsonConstructor]
attribute as shown in JSON.net: how to deserialize without using the default constructor?
Upvotes: 8