2

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?

Luke4792
  • 455
  • 5
  • 19

1 Answers1

2

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;
//...
mm8
  • 163,881
  • 10
  • 57
  • 88
  • This looks like the best way to go about it. Is there a way I can loop through all the properties of the objects and set them equal to each other rather than having to do it manually? – Luke4792 Jun 02 '17 at 15:29
  • 1
    You should be able to do this using reflection: https://stackoverflow.com/questions/4546381/enumerate-and-copy-properties-from-one-object-to-another-object-of-same-type. It shouldn't really be necessary though, especially since your class doesn't seem to have that many properties. – mm8 Jun 02 '17 at 15:39