Reading and writing JSON

This commit is contained in:
Brian Lima 2016-09-18 13:39:36 -03:00
parent 03b7fff6ce
commit 1c52ba21ad
8 changed files with 10 additions and 21 deletions

Binary file not shown.

View File

@ -18,7 +18,10 @@ public class GameModel
public GameModel()
{
games = new ObservableCollection<Game>();
var y = JsonConvert.DeserializeObject<GameModel>("games.json");
using (StreamReader read = new StreamReader("games.json"))
{
games = JsonConvert.DeserializeObject<ObservableCollection<Game>>(read.ReadToEnd());
}
}
private ObservableCollection<Game> _games;
@ -37,8 +40,10 @@ public void Add(Game game)
public void Store()
{
JsonSerializer serializer = new JsonSerializer();
using (StreamWriter sw = new StreamWriter(@"games.json")) {
using (JsonWriter writer = new JsonTextWriter(sw)){
using (StreamWriter sw = new StreamWriter(@"games.json"))
{
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, _games);
}
}

Binary file not shown.

Binary file not shown.

View File

@ -1,15 +1 @@
[
{
"game_alias": "teste",
"game_path": "teste"
},
{
"game_alias": "teste",
"game_path": "teste"
},
{
"game_alias": "teste",
"game_path": "teste"
}
]
[{"game_alias":"teste","game_path":"teste"},{"game_alias":"teste","game_path":"teste"},{"game_alias":"teste","game_path":"teste"},{"game_alias":"teste","game_path":"teste"},{"game_alias":"teste","game_path":"teste"}]

View File

@ -1,3 +1 @@
{
}
[{"game_alias":"teste","game_path":"teste"},{"game_alias":"teste","game_path":"teste"},{"game_alias":"teste","game_path":"teste"},{"game_alias":"teste","game_path":"teste"},{"game_alias":"teste","game_path":"teste"}]

Binary file not shown.

Binary file not shown.