-
Notifications
You must be signed in to change notification settings - Fork 1
/
Guard.cs
55 lines (51 loc) · 1.72 KB
/
Guard.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
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.IO;
using System.Net;
namespace BTD_Backend
{
public class Guard
{
/// <summary>
/// Checks if string is empty or null.
/// </summary>
/// <param name="text">String to check</param>
/// <returns>bool whether or not string is valid</returns>
public static bool IsStringValid(string text) => !String.IsNullOrEmpty(text);
/// <summary>
/// Check if url is valid/exists
/// </summary>
/// <param name="URL"></param>
/// <returns></returns>
public static bool IsWebsiteValid(string url)
{
WebClient wc = new WebClient();
try
{
string HTMLSource = wc.DownloadString(url);
return true;
}
catch (Exception) { return false; }
}
/// <summary>
/// Check if FileInfo file contains valid json
/// </summary>
/// <param name="file">FileInfo to check</param>
/// <returns>Whether or not FileInfo file contains valid json</returns>
public static bool IsJsonValid(FileInfo file) => IsJsonValid(File.ReadAllText(file.FullName));
/// <summary>
/// Check if text is valid json
/// </summary>
/// <param name="text">Text to check</param>
/// <returns>Whether or not text is valid json</returns>
public static bool IsJsonValid(string text)
{
try
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
dynamic result = serializer.DeserializeObject(text);
return true;
}
catch { return false; }
}
}
}