-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathJson.cs
More file actions
38 lines (32 loc) · 967 Bytes
/
Json.cs
File metadata and controls
38 lines (32 loc) · 967 Bytes
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
using System.IO;
using Newtonsoft.Json;
namespace FFBitrateViewer
{
public class Json
{
public static T? FileRead<T>(string fs)
{
// todo@ read from stream (as in IsValid)
string? text = File.ReadAllText(fs);
return JsonConvert.DeserializeObject<T>(text); // todo@ add options
}
public static void FileWrite<T>(string fs, T obj)
{
File.WriteAllText(fs, JsonConvert.SerializeObject(obj, Formatting.Indented));
}
public static bool IsValid<T>(string fs)
{
try
{
using StreamReader file = File.OpenText(fs);
var serializer = new JsonSerializer();
var data = (T?)serializer.Deserialize(file, typeof(T?));
return data != null;
} catch
{
// just catch any error
}
return false;
}
}
}