dimanche 16 novembre 2014

JSON.NET ignores all my properties without JsonProperty attribute


Vote count:

0




I have a GameBoard class, which has 2 methods for saving and loading:



public void Save(string pFilePath)
{
var s = JsonConvert.SerializeObject(this);
System.IO.File.WriteAllText(pFilePath, s);
}

public static GameBoard Load(string pFilePath)
{
var s = System.IO.File.ReadAllText(pFilePath);
return JsonConvert.DeserializeObject<GameBoard>(s);
}


The Save method works great, and this is the content:



{"Width":3,"Height":3,"Cells":[[{"Content":0,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":null}],[{"Content":0,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":null}],[{"Content":0,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":null}]],"WallSurrounded":true}



However, when I use Load method with the same file, all my properties are either null or default values (0 for ints).


I have to add [JsonProperty] to each of my property for it to work properly. I have used Json.NET before, and I remember that I didn't have to add any attribute. Is this intent change from Json.NET (I just get 6.0.6 from Nuget for this new project), or I did something wrong?



asked 31 secs ago

DatVM

2,195






JSON.NET ignores all my properties without JsonProperty attribute

Aucun commentaire:

Enregistrer un commentaire