-
Notifications
You must be signed in to change notification settings - Fork 0
/
Json.cs
28 lines (26 loc) · 852 Bytes
/
Json.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using Newtonsoft.Json;
using System;
using System.IO;
namespace BBQLib
{
public static class Json
{
static JsonSerializerSettings settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
TypeNameHandling = TypeNameHandling.Auto
};
public static void Serialize(object o, string filename)
{
File.WriteAllText(BBQLib.rootDirectory + filename, JsonConvert.SerializeObject(o, settings));
}
public static T Deserialize<T>(string filename)
{
if(!File.Exists(BBQLib.rootDirectory + filename))
{
Serialize(Activator.CreateInstance<T>(), filename);
}
return JsonConvert.DeserializeObject<T>(File.ReadAllText(BBQLib.rootDirectory + filename), settings);
}
}
}