1

A class (used later as datacontext) :

[Serializable]
public class CMiXData : INotifyPropertyChanged
{
    public CMiXData() { } 


    private ObservableCollection<string> _ChannelsSpriteCount = new ObservableCollection<string>(new[] { "1", "1", "1", "1", "1", "1" });
    public ObservableCollection<string> ChannelsSpriteCount
    {
        get { return _ChannelsSpriteCount; }
        set { _ChannelsSpriteCount = value; }
    }

Some usercontrol bind to the property ChannelsSpriteCount of this class and the saved json once serialized may look like this :

{"ChannelsSpriteCount":["32","4","8","64","2","4"]}

When I load and deserialized the json file this way :

    private void Load_Click(object sender, RoutedEventArgs e)
    {
        this.DataContext = JsonConvert.DeserializeObject<CMiXData>(File.ReadAllText(@"C:\Users\BabyClone\Documents\cmix.json"));
MessageBox.Show(cmixdata.ChannelsSpriteCount[0]);

Whatever the values I can see inside the saved file, what's loaded is always the default value from the original class : in this case "1"...

What am I missing here ?

lecloneur
  • 424
  • 5
  • 20
  • 1
    How does Deserialize knows the object type? Try `JsonConvert.DeserializeObject(json);` – Artemy Vysotsky Oct 02 '17 at 19:04
  • No I mean DeserializeObject needs class name as template parameter – Artemy Vysotsky Oct 02 '17 at 19:37
  • The comment from @ArtemyVysotsky is the best approach to deserializing. However, with your current code you will always end up with the first 6 elements of the array being "1" and the last 6 the content of your file. – SpruceMoose Oct 02 '17 at 19:58
  • My point was that this code doesn't deserialize into the `CMiXData` type but into an `object` (indeed because of the missing generic argument), so I wouldn't expect `this.DataContext` to hold the claimed "1"s (OP: _how_ do you observe that? Introduce more intermediate variables to hold both the json string read from the file and the deserialized object), but be a non-observable binding at best on an object generated by Json.NET. So either this isn't all the code, or the file the OP is reading from actually contains "1"s. – CodeCaster Oct 02 '17 at 20:03
  • Can you add your xaml? – Valerii Oct 02 '17 at 20:42
  • @ArtemyVysotsky I'm still having the same problem even with the template parameters – lecloneur Oct 03 '17 at 00:57
  • @Cal279 why that would happen ? – lecloneur Oct 03 '17 at 00:57
  • 1
    Seems to be mostly a duplicate of [How to apply ObjectCreationHandling.Replace to selected properties when deserializing JSON?](https://stackoverflow.com/q/33744236/3744182) and [Why are all the collections in my POCO are null when deserializing some valid json with the .NET Newtonsoft.Json component](https://stackoverflow.com/q/32491966) and [Clear collections before adding items when populating existing objects](https://stackoverflow.com/q/35482896/3744182). Do those answer your question or do you need something that works specifically for `ObservableCollection`? – dbc Oct 03 '17 at 01:55
  • 2
    Indeed best way at the moment was to use : ` [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)] ` – lecloneur Oct 03 '17 at 03:06

0 Answers0