Skip to content

Commit

Permalink
json serializer fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pypy committed Nov 18, 2019
1 parent a55eafb commit 88f9217
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ public static void Serialize<T>(string path, T obj)
}
}

public static bool Deserialize<T>(string path, ref T obj)
public static bool Deserialize<T>(string path, ref T obj) where T : new()
{
try
{
using (var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var stream = new StreamReader(file, Encoding.UTF8))
using (var reader = new JsonTextReader(stream))
{
obj = Newtonsoft.Json.JsonSerializer.CreateDefault().Deserialize<T>(reader);
var o = Newtonsoft.Json.JsonSerializer.CreateDefault().Deserialize<T>(reader);
if (o == null)
{
o = new T();
}
obj = o;
return true;
}
}
Expand Down

0 comments on commit 88f9217

Please sign in to comment.