Luke4792
Luke4792

Reputation: 455

JSON Deserialization not calling property methods

I have a JSON class file which contains three classes, all of which follow this structure:

public class ManifestJSON : INotifyPropertyChanged
{
    [JsonProperty("dataType")]
    private string dataType;

    public string DataType
    {
        get
        {
            return dataType;
        }
        set
        {
            if(dataType != value)
            {
                dataType = value;
                RaisePropertyChanged("DataType");
            }
        }
    }

    [JsonProperty("ttl")]
    private int time_to_live;

    public int Time_To_Live
    {
        get
        {
            return time_to_live;
        }
        set
        {
            if (time_to_live != value)
            {
                time_to_live = value;
                RaisePropertyChanged("Time_To_Live");
            }
        }
    }

    [JsonProperty("serial")]
    private long serial;

    public long Serial
    {
        get
        {
            return serial;
        }
        set
        {
            if (serial != value)
            {
                serial = value;
                RaisePropertyChanged("Serial");
            }
        }
    }

    [JsonProperty("modifiedIso8601")]
    private string modifiedIso8601;

    public string ModifiedIso8601
    {
        get
        {
            return modifiedIso8601;
        }
        set
        {
            if (modifiedIso8601 != value)
            {
                modifiedIso8601 = value;
                RaisePropertyChanged("ModifiedIso8601");
            }
        }
    }

    [JsonProperty("modifiedTimestamp")]
    private long modifiedTimestamp;

    public long ModifiedTimestamp
    {
        get
        {
            return modifiedTimestamp;
        }
        set
        {
            if (modifiedTimestamp != value)
            {
                modifiedTimestamp = value;
                RaisePropertyChanged("ModifiedTimestamp");
            }
        }
    }

    [JsonProperty("timezone")]
    private string timezone;

    public string Timezone
    {
        get
        {
            return timezone;
        }
        set
        {
            if (timezone != value)
            {
                timezone = value;
                RaisePropertyChanged("Timezone");
            }
        }
    }

    [JsonProperty("exports")]
    private ObservableCollection<ManifestItem> manifest_Items;

    public ObservableCollection<ManifestItem> Manifest_Items
    {
        get
        {
            return manifest_Items;
        }
        set
        {
            if (manifest_Items != value)
            {
                manifest_Items = value;
                RaisePropertyChanged("Manifest_Items");
            }
        }
    }

    //Event handling
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string property)
    {
        Console.WriteLine("Updated");
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    }
}

In another class, I've created a global instance of type ManifestJSON

public ManifestJSON manifestData;

which is filled by deserializing a JSON string into this object using the DeserializeObject method from the Newtonsoft.json library like so:

manifestData = JsonConvert.DeserializeObject<ManifestJSON>(JSONString).

This fills the ManifestJSON class successfully, but none of my property methods or events are triggering. What am I doing wrong here?

Upvotes: 2

Views: 952

Answers (1)

mm8
mm8

Reputation: 169330

If you want to update your existing data-bound ManifestJSON object, you should not replace this one with a new object but de-serialize the JSON string into new object and then set the properties of the existing manifestData object:

var newData = JsonConvert.DeserializeObject<ManifestJSON>(JSONString);
manifestData.DataType = newData.DataType;
manifestData.Time_To_Live = newData.Time_To_Live;
manifestData.Serial = newData.Serial;
//...

Upvotes: 2

Related Questions