forked from BattletechModders/Timeline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializeUtil.cs
More file actions
55 lines (48 loc) · 1.43 KB
/
SerializeUtil.cs
File metadata and controls
55 lines (48 loc) · 1.43 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
namespace Timeline
{
public static class SerializeUtil
{
public static T FromJSON<T>(string json)
{
try
{
return JsonConvert.DeserializeObject<T>(json);
}
catch (Exception e)
{
Main.HBSLog?.LogError($"SerializerHelper.FromJSON for class {typeof(T).Name} tossed exception");
Main.HBSLog?.LogException(e);
return default;
}
}
public static T FromPath<T>(string path)
{
if (!File.Exists(path))
{
Main.HBSLog?.LogWarning($"Could not find file at: {path}");
return default;
}
return FromJSON<T>(File.ReadAllText(path));
}
public static List<T> FromPaths<T>(IEnumerable<string> paths)
{
var list = new List<T>();
foreach (var path in paths)
{
var resource = FromPath<T>(path);
if (resource == null)
{
Main.HBSLog?.LogError($"{typeof(T).Name} did not parse at {path}");
break;
}
Main.HBSLog?.Log($"Parsed {typeof(T).Name} at path {path}");
list.Add(resource);
}
return list;
}
}
}