Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing JSON produces default class

I am deserializing an Object using this code

PlayerData = JsonConvert.DeserializeObject<UserData>(response.Content);

My UserData class is as follows

    public class UserData : BindableBase
    {
        public string UID { get; set; }
        public string UDID { get; set; }
        public object liveID { get; set; }
        public int timeLastSeen { get; set; }
        public string country { get; set; }
        public string username { get; set; }
        public string session { get; set; }
        public int serverTime { get; set; }
        public int PremiumCurrency { get; set; }

        public UserData()
        {

        }
    }

When it deserializes I get an object returned as if I had just called new UserData() but if I remove the : BindableBase from the class definition it deserializes correctly. I guess this is because the base class contains some properties that arent in the JSON but I just want those ignored. Is there a setting for this?

The BindableBase class is as follows

[Windows.Foundation.Metadata.WebHostHidden]
[DataContract]
public abstract class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
    {
        if (object.Equals(storage, value)) return false;

        storage = value;
        this.OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

The JSON is as follows

{
    "UID": "4",
    "UDID": "",
    "liveID": null,
    "timeLastSeen": 1392730436,
    "country": "GB",
    "username": "User4",
    "PremiumCurrency": "20",
    "session": "5c8583311732fa816e333dc5e6426d65",
    "serverTime": 1392730437
}
like image 419
Real World Avatar asked Nov 11 '22 12:11

Real World


1 Answers

It's clear enough to answer that the problem is in DataContract attribute of base class. Easy way to fix it is to decorate properties of derived class with DataMember attributes.

like image 53
Sergio Avatar answered Nov 14 '22 21:11

Sergio